• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Servant of the Lord

Win32 Double Buffering

10 posts in this topic

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,
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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*/
0

Share this post


Link to post
Share on other sites

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:
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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?
0

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  
Followers 0