Advertisement

Game Pausing

Started by April 11, 2000 01:00 AM
4 comments, last by Rasta 24 years, 8 months ago
I have sort of a strange problem with game pausing. My 2D sprite movement is based on timing, and when I pause the game between levels (I use the Sleep function), the timing becomes somewhat corrupted for a few seconds. The problem is that after a pause occurs, any visible sprites that are moving on the screen start to jump around and then go back to normal movement after a second or so. I would like to know what are the better ways to correct or avoid faulty timing when you base your sprite movement on time. Perhaps by dropping frames or try to pause the game without actually stopping the game loop? The problem I see with dropping frames is that my game logic (movement) still gets processed even if I don''t render to the screen. Or perhaps I could throw in a hack that locks the frame rate to a constant 60fps for a few seconds, and then release it to full throttle. Does any of this sound reasonable? Rasta
Well, it sounds like your sprites are jumping around the screen because they don''t know that you paused for a few seconds and they try to update their positions really fast to try to make up for their being behind schedule. I would have to look at the code to really know, but it''s possible that all you need to do is subtract the amount of time you paused from your time var... that would only work if you aren''t working with the actual time, but with a time difference or something... otherwise, I don''t know. By the way, I wouldn''t suggest using Sleep(), because it makes all processing impossible, considering it gives control away for however long you are sleeping for. A more realistic way of pausing would either a.)define an IsPaused flag and divert the program flow to a PauseHandler function or something instead of doing the usual game processing, or b.)Have several game states, (ie. GAME_PLAYING, GAME_START, GAME_PAUSE or whatever ) and simply have a handler function for each state, or something... you''ll figure it out.
-Derek
Advertisement
The reason why I use Sleep() is that I am in the middle of a function
and I want to return exactly where I left off. If I change game states,
I probably couldn''t do that. And all my processing flows in a single loop.
Any real pausing will stop everything, game logic, input, rendering, etc..
I have something like this:

 while(1) {    if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {       if(msg.message==WM_QUIT) break;       TranslateMessage(&msg);       DispatchMessage(&msg);    }    if(active && running) {       // game timing done here       if(game_state == GAME_STATE_START)          Game_Start();       else if(game_state == GAME_STATE_PLAYING)          Game_Playing();       else if(game_state == GAME_STATE_PAUSED)          Game_Paused();       // etc    }    else {        WaitMessage();    } } 


I''m actually using Sleep() inside the function Game_Playing() where I
am loading data for next level. I use Game_Paused() to pause the game
while playing the level. I can successfully change the state of the
game to Game_Paused from Game_Playing and back again without any jumpy
sprite movement, probably because my game flow does not stop. But
Sleep() seems to throw my timing off a bit.
Hey there, in my game the game loop never stops until you quit. The way it works is my main game loop. Just calls other functions like "Update_Units", then "Paint_Units". Well actually it''s a little more comples than that, but the idea is the updating of the units and the painting of the units is seperate so when I pause I just stop updating there position, but still paint. Now the reason I do this is for things like in game menus where I''ll need mouse.

Now this will fix the thing about not using the sleep function. However the thing about them jumping. I think what DerekDay said is about right. In my game the time is all in milliseconds, I think from midnight. But that''s not the point. Timing is always the difference from two points in time. So for your guy if the last timing value was 5000ms after midnight and the next timing val was 5050ms after midnight, you would simply calculate according to that units speed, where it should be right now. The problem comes when it goes from 5000ms to 8000ms (We haven''t updated for 3 sec) so you could caculate the next position. But if you don''t want him to move you could replace the old and new timing var with the current time (So it would calculate no move)(This sound like my personal favorite) or if let''s say you paused for three seconds then subtract 3000ms from the origanal timing variable.
Anyhow I hoped that helped.
See ya,
Ben

P.S. I''m a programmer not an english teacher, expect spelling errors!
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Hi Rasta,

I agree with cyberben. The way I would do it is continue to draw your screen (with maybe a "Paused" thingy overlayed), but not update game state. When the game is unpaused, simply set the previousTime (the var you compare to current time calculate the change in time between updates) to the current system time or whatever. Then when updates resume, it will, as cyberben said, be none the wiser that your game even stopped.

thankyoubye



-------------
squirrels are a remarkable source of protein...

Thanks, I fixed it! All I had to do is reset my timing variables
that hold the count difference between the frames like:

QueryPerformanceCounter( (LARGE_INTEGER* )&(high_start) );
high_end = high_start;

And it seems like no time has passed. This works better than
passing a fake frame_time like zero to my game functions. Thanks!.

This topic is closed to new replies.

Advertisement