Sign in to follow this  
crazykid48x

Win32 api flickering

Recommended Posts

crazykid48x    100
Im making a tetris clone as my first real game but I have a problem with the screen flickering when I draw things to it. Does windows api have a double buffer option? If so how do I use it? If not is there anyway to stop the flickering?

Share this post


Link to post
Share on other sites
Vanke    100
It's really easy to do double buffering with GDI.

What you do is create a bitmap in memory get a device context for that bitmap and draw to that DC and then at the very end you blit that to the screen and voila double buffering.


GetClientRect(hwnd,&clientRect);
hdc = BeginPaint (hwnd, &ps) ;
hdcBackBuffer = CreateCompatibleDC(hdc);
hMemBmp = CreateCompatibleBitmap(hdc, cxClient, cyClient);
hOldBmp = (HBITMAP)SelectObject(hdcBackBuffer, hMemBmp);
BitBlt(hdc, -clientRect.right, clientRect.bottom, clientRect.right*2, 2*-clientRect.bottom, hdcBackBuffer, -clientRect.right, clientRect.bottom, SRCCOPY);
SelectObject(hdcBackBuffer, hOldBmp);
DeleteObject(hMemBmp);
DeleteDC(hdcBackBuffer);



I just cut and pasted this from an old school project so it might be missing some things but should be enough to get you started

Share this post


Link to post
Share on other sites
zappernapper    186
even though everyone else here likes to make you find answers on your own, and Vanke provided a nice skeleton, here's a breakdown of two functions that i'm using right now in the win32 games i'm creating (incidentally, even though the beginner tutorial recommends Tetris, i found getting the blocks to rotate right were a pain in the ass, breakout is much easier - to bounce a ball you just need to change the sign (+/-) of its velocity. anyway here they are and where you need to put them:

//place directly after you've created your window
void OffscreenCreation()
{
m_hOffscreenDC = CreateCompatibleDC(GetDC(m_hWindow)); //client window
m_hOffscreenBitmap = CreateCompatibleBitmap(GetDC(m_hWindow), m_iWidth, m_iHeight); //client window's width and height
SelectObject(m_hOffscreenDC, m_hOffscreenBitmap);
}

//place at the end of your game cycle (NOT in your rendering function)
void RepaintScreen()
{
GamePaint(m_hOffscreenDC); //GamePaint is my render function
HDC hDC = GetDC(m_hWindow);
BitBlt(hDC, 0, 0, m_iWidth, m_iHeight, m_hOffscreenDC,
0, 0, SRCCOPY);
ReleaseDC(m_hWindow, hDC);
}



I have a GameEngine class that keeps track of all those handles, and subsequently call those functions THROUGH my GameEngine. it would also be a good idea to make them inline since they're so small and the RepaintScreen() will be getting called every game loop

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this