Jump to content

  • Log In with Google      Sign In   
  • Create Account

Win32 Double Buffering


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Servant of the Lord   Crossbones+   -  Reputation: 19670

Like
0Likes
Like

Posted 26 August 2006 - 01:03 PM

I'm almost finished with the 2D level editor I'm working on, whihc I've built in win32, but I've hit a snag. The screen flickers so much when I continueally update the screen, I can barely see what's going on. I looked up some double buffering tuts and checked some old threads posted here on gamedev, but I can't seem to get it working correctly. I essentionally am drawing like this: //Not all the drawing code, but the rest is reduntant
HBITMAP hbmOld;
HBITMAP Bitmap;

hdc = BeginPaint(hwnd, &ps);

HDC hdcMem = CreateCompatibleDC(hdc);

hbmOld = (HBITMAP)SelectObject(hdcMem, Bitmap);

BitBlt(hdc, X, Y, 32, 32, hdcMem, 0, 0, SRCCOPY);

SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);

EndPaint(hwnd, &ps);
How can I double buffer it? My attempts leave me with a blank screen. [smile] I think I understand the concept, but I don't get exactly how it's done. Is my buffer supposed to be a HBITMAP or a HDC? How do I 'flip' the buffer to the front? I would much apreciate a code snippet or tutorial so I can see it in action, or a detailed enough explantion for one as thick as me to understand. =) Many thanks,
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


Sponsor:

#2 Colin Jeanne   Members   -  Reputation: 1114

Like
0Likes
Like

Posted 26 August 2006 - 01:27 PM

You never set Bitmap to be anything. You need to add the line

Bitmap = CreateCompatibleBitmap(hdc, some width, some height)

Then you need to draw to this bitmap and then BitBlt() as you are doing now.

#3 tendifo   Members   -  Reputation: 146

Like
0Likes
Like

Posted 26 August 2006 - 01:27 PM

This is how I always did it.

// settings
const int width = 400;
const int height = 300;

// vars
HDC bufDC;
HBITMAP bufBMP;

// set up
bufDC = CreateCompatibleDC(hDC);
bufBMP = CreateCompatibleBitmap(hDC, width, height);
SelectObject(bufDC, bufBMP);

// draw to the buffer
MoveToEx(bufDC, 100, 100, NULL);
LineTo(bufDC, 200, 200);

// present the buffer
BitBlt(hDC, 0, 0, width, height, bufDC, 0, 0, SRCCOPY);


[Edited by - tendifo on August 26, 2006 8:27:15 PM]

#4 Servant of the Lord   Crossbones+   -  Reputation: 19670

Like
0Likes
Like

Posted 26 August 2006 - 03:21 PM

I greatly apreciate the help. I still can't seem to understand it, although I think it's close. I again got a blank white screen, with nothing drawing, and sometimes I just can't get it compiled.

This is what I currently have, and I can't pass 'BufferBitmap' as a DC handle, so I guess I still don't understand.

const int height = 471;
const int width = 648;

HBRUSH Brush;
HBITMAP hbmOld;
HBITMAP Bitmap;
HBITMAP BufferBitmap;

hdc = BeginPaint(hwnd, &ps);

HDC bufferDC = CreateCompatibleDC(hdc);
BufferBitmap = CreateCompatibleBitmap(hdc, width, height);
SelectObject(bufferDC, BufferBitmap);

Bitmap = GetTile();
hbmOld = (HBITMAP)SelectObject(BufferBitmap, Bitmap);
BitBlt(bufferDC, X, Y, 32, 32, (HDC)BufferBitmap, 0, 0, SRCCOPY);

BitBlt(hdc, 0, 0, width, height, bufferDC, 0, 0, SRCCOPY);

SelectObject(bufferDC, hbmOld);
DeleteDC(bufferDC);
DeleteObject(BufferBitmap);
DeleteObject(Brush);

EndPaint(hwnd, &ps);


This is the entire paint code, but I cut out the parts I thought irrelevant, like the way I cycle through tiles to paint to the screen, and the code that gets the location of the screen reletive to the map. The entire paint function is below, if it helps any.

void PaintScreen()
{
if(MapX < 0){MapX = 0;}
if(MapX > 85){MapX = 85;}
if(MapY < 0){MapY = 0;}
if(MapY > 88){MapY = 88;}
GetScreen(MapX,MapY);

const int height = 471;
const int width = 648;

HBRUSH Brush;
HBITMAP hbmOld;
HBITMAP Bitmap;
HBITMAP BufferBitmap;

hdc = BeginPaint(hwnd, &ps);

HDC bufferDC = CreateCompatibleDC(hdc);
BufferBitmap = CreateCompatibleBitmap(hdc, width, height);
SelectObject(bufferDC, BufferBitmap);

//Fill background
Brush = CreateSolidBrush(RGB(0, 105, 130));
SelectObject(bufferDC, Brush);
Rectangle(bufferDC, 126, 0, 624, 400);

current = 0; //Current tile being drawn to screen
int X = 128;
int Y = 2;
int ColCount = 0;
for(int i = 0; i < 180; i++)
{
Bitmap = GetTile();
hbmOld = (HBITMAP)SelectObject(BufferBitmap, Bitmap);
BitBlt(bufferDC, X, Y, 32, 32, (HDC)BufferBitmap, 0, 0, SRCCOPY);
ColCount++;
current++;
X += 33;
if(ColCount >= 15)
{
ColCount = 0;
X = 128;
Y += 33;
}
}


BitBlt(hdc, 0, 0, width, height, bufferDC, 0, 0, SRCCOPY);

SelectObject(bufferDC, hbmOld);
DeleteDC(bufferDC);
DeleteObject(BufferBitmap);
DeleteObject(Brush);

EndPaint(hwnd, &ps);
}


Any idea what I'm doing wrong? I tried making the buffer a HDC instead, and that's when I get the white screen.

#5 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 26 August 2006 - 03:48 PM

Here are some notes on double buffering that I took from the Windows 2000 Graphics API Black Book

Quote:

/* Windows 2000 Graphics API Black Book

Double-Buffer Multiple or Expensive Rendering Operations

To implement a double-buffered solution, you perform the following steps:

1. Use the CreateCompatibleDC function to create a memory device context
that is compatible with the target display device.

2. Create an appropriately sized device-dependent bitmap or DIB section bitmap
that will serve as the "memory" of the memory device context.
You can use the CreateCompatibleBitmap function to create a device-dependent bitmap
or the CreateDIBSection function to create a DIB section bitmap.

3. Use the SelectObject function to select the bitmap into the memory device context.

4. Direct any rendering to the memory device context. You can simply modify your existing
code by replacing all references to the original target device context with references
to the memory device context.

5. When the rendering is complete, use the BitBlt function to transfer the bits of the bitmap
to the original target device context.
*/


The section from the book that I took that from used to be available online but the link to it is bad now.

#6 yadango   Members   -  Reputation: 559

Like
0Likes
Like

Posted 26 August 2006 - 04:00 PM

your code, in particular "hbmOld = (HBITMAP)SelectObject(BufferBitmap, Bitmap);" doesn't make sense. SelectObject object takes as the 1st parameter an HDC, not a bitmap.

in your case, what you need is two compatible DCs. one to select the buffer, and one to select the tile. then you want to copy the contents of tile bitmap (which is selected into one of the contexts), into the contents of the buffer bitmap (which is selected into the other context).

something like this:

HDC bufferDC = CreateCompatibleDC(paintDC);
HDC tileDC = CreateCompatibleDC(paintDC);
SelectObject(bufferDC, BufferBitmap);
SelectObject(tileDC, TileBitmap);
BitBlt(bufferDC, x, y, w, h, tileDC, 0, 0, SCRCOPY); // copy contetns of tile bitmap into buffer bitmap
// etc. cleanup code, DeleteObject

as for the concept, think of it like this:
your buffer bitmap is kinda like a back buffer... it's what you render your tiles to. the paint device context is kinda like the front buffer... when you're done rendering to the back buffer, you send it to the screen (window context) using another BitBlt call (though techincally i'm not sure this can truly be called double-buffering because there is no swapping involved... i prefer to call it just buffering).

#7 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 26 August 2006 - 04:00 PM

Looking at the code for void PaintScreen() I see a call to

hdc = BeginPaint(hwnd, &ps);

That is only supposed to be called from inside the WM_PAINT handler. There's not much point to drawing on a double buffer from there, just draw right on the hdc.

The point of the double buffer is to have it available to draw on everywhere else in the program. Create it once, either after CreateWindow or from inside WM_CREATE. You'll likely need to make it a global variable. Don't forget to clean it up afterwards. A helpful tool to ensure that you aren't leaking gdi objects is the free GDIObj tool.

Have your rendering code draw on the buffer. When it completes, blit the buffer to the actual hdc. If you need the drawing to be updated frequently, put the call to the rendering function inside the window message loop as the code to execute when there are no messages. If you do that, you won't need a WM_PAINT handler. If you don't need the drawing to be updated frequently, put the blit into the WM_PAINT handler.

#8 Servant of the Lord   Crossbones+   -  Reputation: 19670

Like
0Likes
Like

Posted 26 August 2006 - 04:06 PM

Thanks LessBread. Let me see if I get this, now:


HDC hdc;
HBITMAP Bitmap;
PAINTSTRUCT ps;

...

HDC memHdc;
HDC bufHdc;
HBITMAP buffer;

hdc = BeginPaint(hwnd, &ps);

bufHdc = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(bufHdc);
buffer = CreateCompatibleBitmap(hdc);

//make buffer be my images
buffer = Bitmap;

SelectObject(hdcMem, buffer);

//Do my blitting to the backbuffer
BltBit(bufHdc, 0, 0, 32, 32, hdcMem, 0, 0, SRCCOPY);

//And finally paint to the screen
BltBit(hdc, 0, 0, width, hieght, bufHdc, 0, 0, SRCCOPY);

/*Clean up*/


#9 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 26 August 2006 - 04:27 PM


HDC hdc;
HDC memHdc;
HBITMAP bitmap;
// instead of BeginPaint use GetDC or GetWindowDC
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
// always create the bitmap for the memdc from the window dc
bitmap = CreateCompatibleBitmap(hdc,width,height);

SelectObject(hdcMem, bitmap);

// only execute the code up to this point one time
// that is, you only need to create the back buffer once
// you can reuse it over and over again after that

// draw on hdcMem
// for example ...
Rectangle(hdcMem, 126, 0, 624, 400);

// when finished drawing blit the hdcMem to the hdc
BltBit(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);

// note, height is not spelled i before e

// Clean up - only need to do this one time as well
DeleteDC(hdcMem);
DeleteObject(bitmap);
ReleaseDC(hwnd, hdc);



Here is another important note

Quote:

When the entire client area of your window is to be covered by a drawing,
you should instruct the default window procedure not to fill the background with
the class background-brush object. You can do this by assigning NULL
(or the NULL_BRUSH stock object) to the WNDCLASS::hbrBackground data member
before the window’s class is registered:

An equivalent technique is to always return non-zero in response to the WM_ERASEBKGND message:


#10 Servant of the Lord   Crossbones+   -  Reputation: 19670

Like
0Likes
Like

Posted 26 August 2006 - 07:50 PM

Thanks for the help thus far.

It works somewhat now, but apparently something is happening that is stopping all my child windows from drawing. (Which is all the control panels, and the dialog boxes. The menu bar is still there)

NM. Many thanks for all the help guys!

[Edited by - Servant of the Lord on August 27, 2006 2:50:29 PM]
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#11 Servant of the Lord   Crossbones+   -  Reputation: 19670

Like
0Likes
Like

Posted 28 August 2006 - 06:04 AM

Hmm, I have another question, and think it doesn't warrant a new thread being made.

The drawing itself double buffers fine, but the dialog boxes I made into scroll bars and a toolbar still flicker at redraw. I tried several things like using a RNG to limit what I redraw, but I'm still having issues with it. Is there a way to double buffer dialog boxes through a simple command without using .NET frame?

If not, I'll have to manually draw the WM_PAINT of the dialog boxes and double buffer it myself. I'd use the basics of DefWindowProc, to not recreate the wheel, but I'm not sure what DefWindowProc looks like inside. I can't find it online or offline. (But apparenly it's a 'windows2' file) Anyone know how I can access it?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS