How to make windowed games share the CPU

Started by
3 comments, last by darookie 19 years, 4 months ago
hey, i have a windowed top down shooter but when i switch to another window, i think my game is still eating up too much CPU time, does anyone have any ideas of how to fix this?
Advertisement
Catch the WM_ACTIVATE message in your window proc and pause the game loop, e.g. by using GetMessage() instead of PeekMessage() if the pause-flag is set.
There are probably many ways to do this, but one way is to listen for the WM_ACTIVATE event to determine when your window becomes inactive. When it does, call whatever code you need to pause your game, and then set a flag. That flag should trigger your main loop to call WaitMessage() instead of calling your game logic. WaitMessage will cause your program to stop running until your window recieves a message. When you get another WM_ACTIVATE message that indicates your window is active, unpause the game and reset the flag.
So are you saying i recieve a WM_ACTIVATE message everytime the window is activated OR inactivated, or do i recieve a WM_INACTIVATE message when I go to another window and a WM_ACTIVATE when it is activated?

if possible, i dont want to pause this window myself, i notice windows does a nice job of pausing while i move or resize the window. It pauses the window clock and as far as i can see basically does nothing untill you are done your moving or resizing. I was wondering if there was some way i could tell windows to pause it like that when it isnt being used. This allows me to have even less unnecesary cpu time used and it makes it easier for me to code :D

thanks.
You will only receive WM_ACTIVATE. The WParam tells whether the window is being activated or not: see MSDN.
[edit]
WaitMessage() reference.
[/edit]

This topic is closed to new replies.

Advertisement