School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,477 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,767 people online right now. Registration is fast and FREE... Join Now!




pausing a game

 

pausing a game, save state of the game

cybernaut09

11 Jun, 2009 - 12:38 AM
Post #1

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 38


My Contributions
I have just started learning to program games using c++ and SDL , hence I have some doubts reguarding some concepts .

I am using a stack to manage states in a game . Now what I want is when a user presses ESC key then the game pauses and a small dialog box is shown which has two options to resume or move to the main menu . If the user presses the resume button the dialog box disappears and the focus is again set on the game.

So my question is , what is the best way to save the states of the game so that when I refresh the display screen after pressing resume , the game starts from the point from where it was paused.

Considering an example of tic tac toe , should I save the data of marked grids in some data structure and make them appear along with the background when the user presses resume button so that dialog box disappears and user can again carry on with playing tic tac toe , or is there any other better way to resume to the game ?

Moreover is stack based approach to handle the states is what professional game programmers use ? Is is capable of handling states in bigger games also or it has some limitations when comes to big games ?

This post has been edited by cybernaut09: 11 Jun, 2009 - 12:41 AM

User is offlineProfile CardPM
+Quote Post


stayscrisp

RE: Pausing A Game

11 Jun, 2009 - 12:54 AM
Post #2

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,380



Thanked: 53 times
Dream Kudos: 300
My Contributions

Hi

This isn't as complicated as you may think, especially as you are already using a state manager.

Could you post some of your code, mainly your main game logic class and your state manager, I have implemented this before yet it's hard to help without knowing how you have your game set up.



User is online!Profile CardPM
+Quote Post

cybernaut09

RE: Pausing A Game

11 Jun, 2009 - 09:23 AM
Post #3

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 38


My Contributions
QUOTE(stayscrisp @ 11 Jun, 2009 - 12:54 AM) *

Hi

This isn't as complicated as you may think, especially as you are already using a state manager.

Could you post some of your code, mainly your main game logic class and your state manager, I have implemented this before yet it's hard to help without knowing how you have your game set up.


I followed this tutorial to create them .
http://gamedevgeek.com/tutorials/managing-game-states-in-c/
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: Pausing A Game

11 Jun, 2009 - 10:29 AM
Post #4

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,380



Thanked: 53 times
Dream Kudos: 300
My Contributions

Those tutorials have everything you need to do pausing and resuming

in your playstate where you handle events
CODE

void PlayState::HandleEvents(Game* game)
{
    SDL_Event event;
    
    if (SDL_PollEvent(&event)) {
        switch (event.type) {
            case SDL_QUIT:
                game->Quit();
                break;
}


add some code that pushes your pause state onto the stack, this is not the same as changing the state as it can be removed using the PopState() function.

for example

CODE
            
case SDL_KEYDOWN:
    switch (event.key.keysym.sym) {
        case SDLK_ESCAPE:
        game->PushState(PauseState::Instance());
        break;


You will have to create a pause state of course, this could be an image saying paused, in this pause state you can create a place in the handle events that pops the current state, this removes it from the stack then the state the game was previously in will resume.

CODE

case SDL_KEYDOWN:
        switch(event.key.keysym.sym) {
        case SDLK_ESCAPE:
        game->PopState();
        break;
}


That is all you need, you can make your pause state as complicated as you like.

Does that make sense?


User is online!Profile CardPM
+Quote Post

cybernaut09

RE: Pausing A Game

14 Jun, 2009 - 12:38 AM
Post #5

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 38


My Contributions
Thanks stayscrisp!
I did what you suggested and it worked .

I wanted in pause dialog box 2 buttons: resume and main menu . Pressing ESC pauses the game and shows this dialog box . Resume button is working as intended . For main menu I had to make certain changes because I have 2 states already in stack : PlayState and PauseState . Hence in GameEngine class

CODE

void GameEngine::changeState(GameState* state){
    if(state == MenuState::instance()){
        while(!states.empty()){
        states.back()->clean();
        states.pop_back();
        }
    }
    //clean current state
    if(!states.empty()){
        states.back()->clean();
        states.pop_back();
    }
    //store and init new state
    states.push_back(state);
    states.back()->init(this);
}



But now when I check in task manager for memory usage , it increases (does not decrease even after reaching MenuState) for every change between PauseState to MenuState .
On pressing main menu button in PauseState , the stack is emptied and MenuState is pushed in the stack . So memory usage should be equal to when the apllication initially started with MenuState .

For other changes between states I can understand the reason for increase or decrease in memory usage but I can not figure out why this is happening in this case .
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: Pausing A Game

14 Jun, 2009 - 04:51 AM
Post #6

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,380



Thanked: 53 times
Dream Kudos: 300
My Contributions

You shouldn't really need to make any changes to the state manager, when you create the pause state you simply push the state onto the stack, there are only the previous state plus the pause state, the pause state can then be popped back which removes it.

To go to the main menu state you can simple pop the pause state and then change state to the main menu state.

Try to put some printf calls when you release and init states so that you know they have been dealt with.

hope this helps icon_up.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:37AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month