Jump to content
  • Advertisement
Sign in to follow this  
phil05

Win32 Question regarding game loops & painting

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

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!