Win32 api flickering
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?
You might want to perform a search first and have a look at previous responses to similar questions: Gamedev search: Win32 api flickering.
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.
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
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
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:
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
//place directly after you've created your windowvoid 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
this is of course assuming you HAVE a rendering function, otherwise sandwich it between BeginPaint() and EndPaint()
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement