I am building a Windows MFC application. During some animations where objects collide at high speeds, my physics engine behaves unpredictably. I believe it has something to do with me dropping frames somehow. I was told that I'm not using double buffering. I thought I was, but I am still fairly new to this. Here is how I draw to the screen in OnPaint:
#include "pch.h"
#include "framework.h"
#include "ChildView.h"
#include "DoubleBufferDC.h"
void CChildView::OnPaint()
{
CPaintDC paintDC(this); // device context for painting
CDoubleBufferDC dc(&paintDC); // device context for painting
Graphics graphics(dc.m_hDC); // Create GDI+ graphics context
mGame.OnDraw(&graphics);
if (mFirstDraw)
{
mFirstDraw = false;
SetTimer(1, FrameDuration, nullptr);
/*
* Initialize the elapsed time system
*/
LARGE_INTEGER time, freq;
QueryPerformanceCounter(&time);
QueryPerformanceFrequency(&freq);
mLastTime = time.QuadPart;
mTimeFreq = double(freq.QuadPart);
}
/*
* Compute the elapsed time since the last draw
*/
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
long long diff = time.QuadPart - mLastTime;
double elapsed = double(diff) / mTimeFreq;
mLastTime = time.QuadPart;
mGame.Update(elapsed);
}
void CChildView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
RedrawWindow(NULL, NULL, RDW_UPDATENOW);
Invalidate();
CWnd::OnTimer(nIDEvent);
}
When I create the Graphics object from the CDoubleBufferDC object, is this not creating a back buffer? I then pass this Graphics object to OnDraw where it is drawn on. If it is creating a back buffer, I'm confused about where the front buffer is created and when it is drawn on the screen.
Here are my current thoughts on how I think this works:
- The CPaintDC object is the front buffer
- The CDoubleBufferDC object is the back buffer
- A graphics object is created from the CDoubleBufferDC object which I draw the current state of the game on
If this is the case, when is the front buffer ever replaced with the new buffer created in the back? Can someone help me understand, and use double buffering if I'm not already?