Public Group

# Another Win32 API question

This topic is 4927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What is the easiest way to get a window to redraw itself automatically every so often? Up until now, I have been using InvalidateRect(hwnd,NULL,FALSE) everytime I need the window to be redrawn. I don't even know what to search for to find this answer. Any help is greatly appreciated.

##### Share on other sites
InvalidateRect is the standard GDI way of forcing a redraw. For most GUI apps, it's the correct thing to use, although you can refine it somewhat by only redrawing the parts that need to be redrawn, rather than the entire window.

If you need it called periodically every so often, you can use the windows timer to do it, although this is rarely good enough for updating realtime stuff like games.

##### Share on other sites
Quote:
 although this is rarely good enough for updating realtime stuff like games

What would be good enough?

##### Share on other sites
Quote:
Original post by mrtie_dye
Quote:
 although this is rarely good enough for updating realtime stuff like games

What would be good enough?

If your frame rate matters, you shouldn't be using GDI for your drawing - you should be using something like OpenGL or DirectGraphics (or a library that wraps one of these up, like SDL) to get the benefit of hardware acceleration.

These APIs generally come with their own methods for updating the screen, which are generally called as often as possible.

##### Share on other sites
I do most of my programming at work. My job is to watch security cameras and take a phone call every now and then, so I have plenty of time there. The problem is, the computer at work runs Windows2000, and I don't have the admin password. So, I can't install anything like DirectX or SDL. So, I am pretty much stuck with MinGW Studio and the Win32 API.

You mentioned the windows timer. If this is the best I can do, then it will have to do. I am not trying to make a first person shooter or anything like that. Just a simple rpg. So, I guess the frame rate doesn't matter as much.

Any info on the windows timer, or an alternate method that would work better given my constraints at work, would be greatly appreciated.

##### Share on other sites
The windows timer is really easy to setup:
const UINT TIMER_ID = 1001;SetTimer(WindowHandle, TIMER_ID, 1000 /*time in milliseconds*/, 0 /*timer proc*/);And then in your WndProc for WindowHandle:LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam){    switch(nMsg)    {    case WM_TIMER:        {            switch(wParam)            {            case TIMER_ID:                {                    //update here                    break; // do not return here if you have more than one timer                    // if you return here instead of breaking, the second timer                    // might be up and you will pass right over it.                }            }            return 0;        }    }    return DefWindowProc(hWnd, nMsg, wParam, lParam);}

##### Share on other sites
In the windows message loop, whenever your not processing a message call a function called OnIdle or GameLoop. This will be your game's main loop that can process information every frame. Don't invalidate the rect either, I would suggest getting the back buffer directly and blitting your drawing onto it. It is a lot faster.

if( PeekMessage )
{
// process message
}
else
{
OnIdle( );
}

##### Share on other sites
Thanks so much for all your help. It is almost my bed time (graveyard shift), but I will be sure to try each of your suggestions in the morning at work. They might both come in handy. One quick question before I go though.

Ph33r, you mentioned getting the back buffer. How do I get the back buffer? I know how to use BitBlt. Is this what you are talking about when you say blitting?

Thanks again for all your help. Good night.

##### Share on other sites
Quote:
 Original post by mrtie_dyeI do most of my programming at work. My job is to watch security cameras...

Let me guess, you have stuff come up missing alot? Man, you guys should get security cameras or something [wink].

##### Share on other sites
Quote:
 Original post by mrtie_dyeThanks so much for all your help. It is almost my bed time (graveyard shift), but I will be sure to try each of your suggestions in the morning at work. They might both come in handy. One quick question before I go though.Ph33r, you mentioned getting the back buffer. How do I get the back buffer? I know how to use BitBlt. Is this what you are talking about when you say blitting?Thanks again for all your help. Good night.

A back buffer is just an offscreen surface. It can improve performance to draw to a backbuffer and then blit* it's contents to the display buffer in one go, rather than drawing everything to the display buffer in dribs and drabs.

If you're using basic win32 GDI commands, you don't really have a backbuffer unless you specifically create one. You'd have to create a DC to do all your drawing to, and then use something like BitBlt to copy it's contents to the DC of your game window.

As for more powerful graphics API's, I'd be surprised if you didn't have some OpenGL libraries you could use already installed with your system, although if you feel that win32 GDI is powerful enough for your purposes then it may be a bit excessive.

*or page flip, if possible, although this isn't really an option with GDI.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633662
• Total Posts
3013231
×