![]() |
|
#1
|
||||
|
||||
|
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
}
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();
}
}
__________________
A wise man once said "...what was I saying..." Last edited by lokiare1; 03-15-2009 at 11:38 PM. |
|
|
|||
|
|||
|
|
| Thread Tools | |
| Display Modes | |
|
|