Original Post
Hello. Finnished with my first non "big switch statement in main.cpp" gamestate manager. Would like some comments. The main idea is that the main part should be pGamestateManager->run(); and pGamestateManager->process_events( event );, the active state runs. So this is a basic game state: This is a pure base class, doesn't even have a own .cpp file. And no data, The gamestates that inherit this class adds their own data. An example: The process_events function looks like this: And the gamestate manager that runs everything looks like this: Any comments, I know it's not that advanced, and probobly not very smart =) but I want to learn, so point out all the mistakes you have the strenght to find =) Thanks.
[source lang = cpp]
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include "globals.h"
class cGamestate
{
public:
cGamestate() { }
~cGamestate() { }
virtual void run() { /* Overloaded =) */ }
virtual void process_events( SDL_Event event) { /* Overloaded =) */ }
};
#endif
[source lang = cpp]
#ifndef PLAYSTATE_H
#define PLAYSTATE_H
#include "globals.h"
class cPlaystate : public cGamestate
{
public:
cPlaystate();
~cPlaystate();
void run();
void process_events( SDL_Event event);
void manage_enemies();
void manage_player();
void draw_hud();
private:
cPlayer player;
vector<cEnemy> enemies;
bool paused;
bool gameover;
Uint8* keys;
};
#endif
[source lang = cpp]
void cPlaystate::process_events( SDL_Event event)
{
while( SDL_PollEvent(&event) )
{
switch(event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
done = true;
break;
case SDLK_F1:
Toggle_Fullscreen();
break;
case SDLK_SPACE:
player.shoot();
break;
case SDLK_RETURN:
paused = true;
break;
case SDLK_p:
if(paused == true) { paused = false; }
else { paused = true; }
break;
}
default:
break;
}
}
if( paused == false )
{
keys = SDL_GetKeyState(NULL);
if(keys[SDLK_UP])
{
player.move(0, -350 * framerate.dtime());
}
if(keys[SDLK_DOWN])
{
if(player.sprite.gety() + player.sprite.geth() < screen->h ) { player.move(0, 350 * framerate.dtime()); }
}
if(keys[SDLK_LEFT])
{
player.move(-350 * framerate.dtime(), 0);
}
if(keys[SDLK_RIGHT])
{
if(player.sprite.getx() + player.sprite.getw() < screen->w ) { player.move(350 * framerate.dtime(), 0); }
}
}
}
[source lang = cpp]
#ifndef GAMESTATEMANAGER_H
#define GAMESTATEMANAGER_H
#include "globals.h"
class cGamestatemanager
{
public:
cGamestatemanager();
cGamestatemanager( cGamestate *nstate);
~cGamestatemanager();
void run();
void process_events( SDL_Event event);
void set_state( cGamestate *nstate );
private:
cGamestate *pGamestate;
};
#endif
[source lang = cpp]
#include "globals.h"
cGamestatemanager::cGamestatemanager()
{
pGamestate = NULL;
}
cGamestatemanager::cGamestatemanager( cGamestate *nstate )
{
pGamestate = nstate;
}
cGamestatemanager::~cGamestatemanager()
{
if( pGamestate )
{
delete pGamestate;
pGamestate = NULL;
}
}
void cGamestatemanager::run()
{
pGamestate->run();
}
void cGamestatemanager::process_events( SDL_Event event )
{
pGamestate->process_events( event );
}
void cGamestatemanager::set_state( cGamestate *nstate )
{
pGamestate = nstate;
}