Win32 Question regarding game loops & painting

Started by
1 comment, last by hplus0603 19 years, 4 months ago
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.
Advertisement
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.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
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.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement