Advertisement

SDL Pausing game help?

Started by November 26, 2014 05:43 PM
10 comments, last by rip-off 10 years, 1 month ago

I know this might sound stupid but what is the best way to go about pausing my game by pressing esc. I need it to pause everything then resume once i repress that key. Many thanks!

I know this might sound stupid but what is the best way to go about pausing my game by pressing esc. I need it to pause everything then resume once i repress that key. Many thanks!

set paused = true when esc is pressed, don't update your game state if paused is true.

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Advertisement

I know this might sound stupid but what is the best way to go about pausing my game by pressing esc. I need it to pause everything then resume once i repress that key. Many thanks!

set paused = true when esc is pressed, don't update your game state if paused is true.

is paused already in the sdl library?


is paused already in the sdl library?

No.

His suggestion is to create a bool variable called "paused".

Toggle the variable when Escape is pressed.

Check the state of the variable in your update loop to determine what updates.

Hello to all my stalkers.

is paused already in the sdl library?

Most people think of pausing in the simple sense, it freezes the game, but in reality it never does. That may or may not be obvious, if you think about a game like the old Zelda games, pausing would take you to some kind of inventory screen. Obviously the game world was still paused in that case but many other elements of the screen had to still function.

Basically pausing doesn't simply halt the game, it halts the ingame logic. How you do that depends completely on the way you've structured your code, if you have your game calling some kind of tick function you probably want it to continue calling it, but maybe a layer under there where it is calling tick on the game objects like the AI, movement, etc, you would either designate some variable and basically stop updating them altogether if it was true, or you could just pass them a delta time of zero to get the same basic behavior.

A library is unlikely to have any kind of pause function unless it has some access to the game loop and actually "knows" where the logic is going to happen so it can stop calling updates on it.

is paused already in the sdl library?

Most people think of pausing in the simple sense, it freezes the game, but in reality it never does. That may or may not be obvious, if you think about a game like the old Zelda games, pausing would take you to some kind of inventory screen. Obviously the game world was still paused in that case but many other elements of the screen had to still function.

Basically pausing doesn't simply halt the game, it halts the ingame logic. How you do that depends completely on the way you've structured your code, if you have your game calling some kind of tick function you probably want it to continue calling it, but maybe a layer under there where it is calling tick on the game objects like the AI, movement, etc, you would either designate some variable and basically stop updating them altogether if it was true, or you could just pass them a delta time of zero to get the same basic behavior.

A library is unlikely to have any kind of pause function unless it has some access to the game loop and actually "knows" where the logic is going to happen so it can stop calling updates on it.

ok so if i had my paused bool and resume bool, how would i actually go about pausing the game? I tried SDL_Delay() but it doesnt work as expected. It kinda just slows it down

Advertisement

ok so if i had my paused bool and resume bool, how would i actually go about pausing the game? I tried SDL_Delay() but it doesnt work as expected. It kinda just slows it down


It's slowing down because SDL_Delay actually freezes your program for a specified amount of time. You only want to freeze your logic time.
You probably have a float elapsedTime or something like that. When paused is true you need to set this elapsedTime variable to zero. That way your application is able to do work (processing input, handle incoming events from the OS, ...) while your game logic does not progress.

You know the part where you update() your game objects? When the game is paused, simply don't do that. You may wish to continue drawing them or running some animations, depending on the game.

You know the part where you update() your game objects? When the game is paused, simply don't do that. You may wish to continue drawing them or running some animations, depending on the game.

i see many thanks

No worries. It is easy to over-think some of these things sometimes.

This topic is closed to new replies.

Advertisement