When to draw and swap buffers

Started by
12 comments, last by XTAL256 15 years, 11 months ago
I am currently making the graphical user interface (GUI) for my game using OpenGL (C++, VS2005). I have Window objects which have an array of Component objects. These can be Buttons, CheckBoxes, etc. Each component (Window is also a subclass of Component) has a draw() function which draws the images. For testing, i just call the draw function for my window in the WinMain loop:

while(!done) {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
        if (msg.message==WM_QUIT) {
            done = TRUE;
        }
        else {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    else {
        clearScreen();
        // Test
        screen.draw();
        SwapBuffers(hdc);
    }
}

Don't worry about what that code does, it's not really important. What i want to know is where to put SwapBuffers(hdc);. If i draw everything in the loop, it will use 100% CPU. I want to only draw when an event occurs such as moving the mouse over a button or pressing a key. I have made an event system which using Windows' WNDPROC function to get events then passes those events to all Components that listen for events. Now, i only want to call draw() for each component when it gets an event. But how do i know when to swap buffers? If this cannot be done i could have two separate graphics contexts; one double buffered for drawing my game scenes, the other single buffered for drawing the GUI.
[Window Detective] - Windows UI spy utility for programmers
Advertisement
It can be a bit hard to update the buffer as you'll need a pretty good knowedge of damaged area so that when the UI is updated, you can erase the dirty area (by replacing it by "game" content), and then draw the updated area. In terms of code complexity, this approach needs careful attention as it can easily lead to dirty code (yes, I've been there :-)

An easier approach is to use offscreen buffer (fbo) and render the game content in one of them and the UI is the other. That way you are making the update event much coarser: "update game" or "update UI".
The entire scene is rendered by compositing the two layers.
That's more expensive than the first solution but so much easier to implement.

Another drawback of the second approach is the "double-buffer" issue. Basically you'll be tempted by duplicating ressources to hide more of the hideous latency. For instance, you might want to double buffer the "game content fbo".
That might turn out to be a good idea but I suggest to start with everything thing singled-buffered (expect the main buffer) and eventually optimize afterward.
I don't really know how to use offscreen buffers but would it be just as good to create two separate rendering contexts (HDC and HGLRC). 'cos that's easy, i can just make my graphics class non-static and create two objects of the class.
[Window Detective] - Windows UI spy utility for programmers
I would keep that code and just add sleep(howmuch) and continously measure the FPS and adjust howmuch until I get 60FPS.
That will reduce the CPU usage to 1-5%.
I don't recommend that while the game is running! Only do this for your game menu.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
I thought i would try using a single buffer for the UI just to see what it's like but i can't get it to work. I removed the PFD_DOUBLEBUFFER flag from the PIXELFORMATDESCRIPTOR and removed the call to SwapBuffers() but nothing gets drawn. How do i do it? btw, I am using Windows and OpenGL.
[Window Detective] - Windows UI spy utility for programmers
I suggest you redraw the GUI when something happened. Just redraw it completely. So whenever the mouse moves, or keys get hit. You can also filter events that have no effect to the GUI, but thats up to you. Only drawing when events are thrown reduces CPU dramatically (except if the user is moving the mouse insanely much)

This is what I do with SDL combined with OpenGL.

I'm working on the GUI aswell, and my code became LONG. Now I decided to make external text files which will decide the GUI's content, only need a parser and display functions, instead of endless code hardcoding button positions etc. The GUI is taking more time already than my game engine ^^ (using a 2D tile-based game).

PS: whats so bad in delaying 1 ms every frame in-game? I made it an option in this manner:

Normal - Low CPU - VSync

where users can choose one of them...I prefer the latter 2 though...
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
I suggest you redraw the GUI when something happened. Just redraw it completely.

Yes, that's what i am trying to achieve. Except i was only going to draw the component that changes, not because it better but actually because it's easier. My event code calls a function in each component that listens for events. So when such an event occurs (say, clicking the mouse), each button gets that event and then calls it's draw() function. Now if i were to completely redraw everything, i would have to know what components are currently on the screen. It can be done but i would have to change my code a bit.

Quote:Original post by Decrius
Now I decided to make external text files which will decide the GUI's content...

I'm just hardcoding my button positions and stuff but i am using an XML file to describe the GUI styles, fonts, etc. Some people told me that it's overkill and that i can just hardcode all that stuff in but i find it much easier. Not only can i easily change something without the need to re-compile but other users can create their own custom GUI theme/style, which is a feature that i personally like about a program.

Quote:Original post by Decrius
PS: whats so bad in delaying 1 ms every frame in-game?

Well, i haven't even started coding the actual game (i haven't even though about how i will do it). But i would like to have a function that does all my game rendering and stuff, and that function gets called by a timer 30 times per second (for 30fps). I don't want to draw my GUI in that function because it won't even be running if game hasn't started and the user is just in the menu. I want my GUI and game to be separate.
But if that is too hard then i will just do it all in one place and delay 1ms or something.
[Window Detective] - Windows UI spy utility for programmers
Hi again. I have decided that could swap the buffers after everything i draw, and if i don't clear the screen i only have to redraw the components that need redrawing. At first i didn't think this would work but double buffer or not i can still draw only when an event occurs, i just can't draw after i swap buffers cos that's when you get flickering. So all i have to do is put SwapBuffers(hdc) after i draw a component.
Also, is it ok to do drawing in two separate threads? Is there a good way of doing this? Because i am planning on using a timer to update the drawing of the game and GUI drawing will be done in the event thread.

EDIT: doesn't work. All i get is a black screen (from initially clearing the screen). Event functions are called, which draw stuff and call SwapBuffers, but nothing. If it's possible to do it this way (which i want to do), i can upload the full source so you can check it out.

[Edited by - XTAL256 on May 9, 2008 8:27:03 PM]
[Window Detective] - Windows UI spy utility for programmers
Can anyone help me, i'm really stuck here.
(sorry for the double post)
[Window Detective] - Windows UI spy utility for programmers
Quote:Original post by XTAL256
I thought i would try using a single buffer for the UI just to see what it's like but i can't get it to work. I removed the PFD_DOUBLEBUFFER flag from the PIXELFORMATDESCRIPTOR and removed the call to SwapBuffers() but nothing gets drawn. How do i do it? btw, I am using Windows and OpenGL.


haven't you forgot to glFlush(); when you deleted buffer swapping?

This topic is closed to new replies.

Advertisement