Using SwapBuffers() slows down the program

Hello!

I make a small Win32GUI program, but the screen are flickering when I redraw it with InvalidateRect(hwnd, NULL, true); in WM_TIMER.
To avoid this, I have inserted the SwapBuffers(mygraphhandler); command into first line of WM_PAINT.
Now, the screen aren't flickering, but my program is slowed down.
Soembody can help me to avoid screen flickering and slowness? Using SwapBuffers() isn't the right way to perform double buffering?
The normal way to do double buffering is to create an offscreen bitmap, draw everything on the bitmap and finally draw the bitmap.
If you show us the full code someone might be able to help.
"The normal way to do double buffering is to create an offscreen bitmap, draw everything on the bitmap and finally draw the bitmap."

I don't know how can I realize it. I thought SwapBuffers() solve my problem simply and without large amount of coding.

"If you show us the full code someone might be able to help."

Here is the important pieces from my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC mygraphhandler;
PAINTSTRUCT ps;
(...)
 switch (message)                  /* handle the messages */
    {
        case WM_PAINT:
mygraphhandler = BeginPaint(hwnd, &ps);
... Drawing stuff ...
SwapBuffers(mygraphhandler);
            EndPaint(hwnd, &ps);
break;
case WM_TIMER:
if (GetAsyncKeyState(VK_LEFT)) playerx-=4;
(...)
InvalidateRect(hwnd, NULL, true);
Last edited on
Win32 GDI double buffering in C++:
http://www.robertelder.ca/doublebuffering/

If you are using different drawing technology such as GDI+ or Direct2D a 'net search will find examples.

Here's the meta-search I used to find the above link:
https://duckduckgo.com/?q=gdi+single+buffer+win32&t=ffsb&ia=web
Thank you, Furry Guy; I will try it, but it seems to be good for my code.
I used Google to search on the net, but focusing onto SwapBuffers() command and I didn't get useful tips about it.
Hello again!

I have tried the double buffering example from robertelder website and fortunatelly it works well.
But I have two notices:
1. You can see this in the example:
"NOTE: you may need to set the window background not to draw itself before you register the window class:
[NAME_OF_YOUR_WNDCLASSEX].hbrBackground = NULL;"
I rewrote my code to set this to NULL, because the screen is flickering without this, but what if I would like use another background color than black?
2. When I use these lines:
1
2
3
4
RECT Client_Rect;
GetClientRect(hwnd,&Client_Rect);
int win_width = Client_Rect.right - Client_Rect.left;
int win_height = Client_Rect.bottom + Client_Rect.left;

I get the following error message:
"F:\progsetup\codeblocks-17.12mingw-nosetup\sajatok\ablakosrajz\Jatekom\main.cpp|245|note: crosses initialization of 'int win_height'|"
and similar to int win_width.
Therefore I must skip these lines and use numbers directly to add the width and height of the window. But it is not good when the player resize the window.
How can I use the width and height variables of the window correctly?
Last edited on
Topic archived. No new replies allowed.