Sign in to follow this  
phil05

Win32 Question regarding game loops & painting

Recommended Posts

I'm still a beginner with window game programming, so be warned :) I've been wondering lately about the game loop in PeekMessage(). Usually I'd go for init() above it, shutdown() below it. While the game loop is going on between, it often needs to repaint everything on the screen aka a ball bouncing back and forth from left to right. This is fine and dandy, but what would I use WM_PAINT for then in the window procedure? Is it better just to leave it alone and indirectly access it with hDC = GetDC(hWnd);? Just seems odd to have both running aka hDC = BeginPaint(hWnd, &ps); and hDC = getDC(hWnd). I wouldn't know which one to use. Clearing this up would be appreciated. It's been one of my longest and confusing questions.

Share this post


Link to post
Share on other sites
If I'm reading your question right, you're asking how to make the window repaint from elsewhere within your code, right? What I'd recommend is using InvalidateRect(hWnd, NULL, true);

This will tell your window that the whole window needs to be repainted, and it will call your WM_PAINT procedure, where you can redraw the game. You can also refine things by only invalidating the part of the window that needs to be redrawn, but this should be sufficient for a game of Pong. You can always refer to MSDN for more detail on this sort of thing.

On second look, if you're asking about how to write your WM_PAINT procedure, you should be using the BeginPaint and EndPaint functions to get the device context, unless my Win32 memory has gone completely foul.

Share this post


Link to post
Share on other sites
Typically, you'll call a single function to re-paint your screen. You'll call this both from the main loop (after you've updated game world state) and from within WM_PAINT.

Relying only on WM_PAINT for redraws may be sluggish once you start taxing the machine, as WM_PAINT is only generated when the queue is otherwise empty. Meanwhile, you don't want to continually re-paint the screen (or step the game) when you're switched out. Thus, you would set a flag when receiving deactivate, and use GetMessage() instead of PeekMessage(), and not update or step everytime through the loop, but instead let WM_PAINT take care of it when you're in the background.

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