PSP Hacks - Forums

Go Back   PSP Hacks - Forums > PSP Community > PSP Programming & Development

Notices

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-15-2009, 11:36 PM
lokiare1's Avatar
lokiare1 lokiare1 is offline
Programmer
PSP Enthusiast
 

Join Date: Jul 2005
Location: Arkansas, USA
Posts: 466
lokiare1 is on a distinguished road
Default [TUTORIAL] How to use game states in c++

What are game states? game states are the different phases a game can be put in. Examples are the splash screen, menu, game play, and credits or ending of a game. There is a simple and easy way to implement game states within a game.

First you must create a game state super class for other states to inherit from:

Code:
// Class declaration in GameState.h
class GameState
{
     // Constructor
     GameState();

     // Before render (the = 0 means it must be overridden in a derived class
     virtual bool BeforeRender() = 0;

     // After render
     virtual bool AfterRender() = 0;

     // Destructor (virtual to force the use of derived classes functions when calling from a GameState * object)
     virtual bool ~GameState();
};
// Class implementation in GameState.cpp
GameState::GameState()
{
     // Set up your variables and stuff here
}

GameState::~GameState()
{
     // Delete pointers and let go of assets here
}
After you have this class you make each of your derived classes which must include the functions BeforeRender and AfterRender implemented in them.

Your main code will look like this:

Code:
// where play state is an inherited class from GameState
#include <stack>
#include "PlayState.h"

// For <stack>
using namespace std;

// Our stack of states (if you don't understand this look up templates)
stack<GameState *> stateStack;

main()
{
     // Create a new play state
     PlayState * play = new PlayState();

     // Add the play state to the stack
     stateStack.push(play);

     // Create and add another play state
     PlayState * play2 = new PlayState();
     stateStack.push(play2);

     // Main game loop
     bool end = false;

     // Loop until we set end to true
     while (end == false)
     {
          // Get our top state, and do the before render
          stateStack.top()->BeforeRender();

          // Do whatever rendering your render engine uses
          Render();

          // Get our top state, and do the after render stuff
          stateStack.top()->AfterRender();
     }
}
That is how you use game states. You can have as many states as your memory allows, and you can pretty much do whatever you want different in each state.
__________________
A wise man once said "...what was I saying..."

Last edited by lokiare1; 03-15-2009 at 11:38 PM.
Reply With Quote
 

 



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:41 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©