Gamefsm Class Reference

#include <Gamefsm.h>

List of all members.

Public Member Functions

 Gamefsm (Server *server)
virtual ~Gamefsm ()
void threadBody ()
void Start ()
void Stop ()
void Evolve ()
SDL_Thread * getThread ()

Protected Member Functions

bool ExecTrans (game_fsm_trans t)
void Init ()
void Update ()
void Quit ()

Protected Attributes

void(Gamefsm::* ExecState )(void)

Static Private Member Functions

int startThread (void *data)

Private Attributes

LoggermyLogger
bool exitRequest
bool exitLoop
SDL_Thread * myThread
ServermyServer


Detailed Description

Manages the game with a finite state machine.

This FSM runs in its own thread and manages the phases of the game. Non thread-safe, (Only the Server instance will use it).

Definition at line 17 of file Gamefsm.h.


Constructor & Destructor Documentation

Gamefsm::Gamefsm Server server  ) 
 

Constructor.

Definition at line 7 of file Gamefsm.cpp.

00008     :myLogger(Logger::getInstance()),
00009     exitRequest(false),
00010     exitLoop(false),
00011     myThread(NULL),
00012     myServer(server)
00013 {
00014 }

Gamefsm::~Gamefsm  )  [virtual]
 

Virtual destructor.

Definition at line 16 of file Gamefsm.cpp.

00017 {
00018 }


Member Function Documentation

void Gamefsm::Evolve  ) 
 

Evolve to the next state of the machine.

Author:
stonedz
Since:
pre-alpha

Definition at line 45 of file Gamefsm.cpp.

Referenced by threadBody().

00045                     {
00046     (this->*ExecState)();
00047 }

bool Gamefsm::ExecTrans game_fsm_trans  t  )  [protected]
 

Executes translations between states

Author:
dndvault

stonedz

Since:
pre-alpha
Parameters:
t state to evelve to.

Definition at line 54 of file Gamefsm.cpp.

References ExecState, Logger::log(), and myLogger.

Referenced by Init(), threadBody(), and Update().

00054                                        {
00055     switch(t){
00056         case t_init: // initializes the game.
00057             ExecState = &Gamefsm::Init;
00058             break;
00059         case t_main: // Main loop , updates the game.
00060             ExecState = &Gamefsm::Update;
00061             break;
00062         case t_quit: //Quits the game, save and prepare for a clean shutdown.
00063             ExecState = &Gamefsm::Quit;
00064             break;
00065         default:
00066             myLogger->log("Unsupported transition in ClientFSM",LOGMODE_ERROR);
00067     }
00068     return true;
00069 }

SDL_Thread * Gamefsm::getThread  ) 
 

Returns the thread the Gamefsm is running into.

Author:
stonedz
Since:
pre-alpha
Returns:
A pointer to the thread the Gamefsm is running into.

Definition at line 50 of file Gamefsm.cpp.

Referenced by Server::startListen().

00050                               {
00051     return myThread;
00052 }

void Gamefsm::Init  )  [protected]
 

Initializes the game, loads world, maps and everything that is needed to activate the game.

Author:
stonedz
Since:
pre-alpha

Definition at line 71 of file Gamefsm.cpp.

References ExecTrans(), myServer, and Server::setInit().

00071                   {
00072     // Initialize the game...
00073     myServer->setInit(true);
00074     // then pass the control to the main loop.
00075     ExecTrans(t_main);
00076 }

void Gamefsm::Quit  )  [protected]
 

Quits the game, save and prepare for a clean shutdown of the server.

Author:
stonedz
Since:
pre-alpha

Definition at line 87 of file Gamefsm.cpp.

References exitLoop.

00087                   {
00088     // Save the game and prepare for a proper shutdown
00089     //std::cout << std::endl << "quitting GAMEFSM";
00090     exitLoop = true;
00091 }

void Gamefsm::Start  ) 
 

Effectively starts the thread and FSM.

Author:
stonedz
Since:
pre-alpha

Definition at line 30 of file Gamefsm.cpp.

References Logger::log(), myLogger, myThread, and startThread().

Referenced by Server::startListen().

00030                    {
00031     myThread = SDL_CreateThread(startThread, this); // Starts the thread
00032     if (myThread == NULL){ // Something went terribly wrong :D
00033         std::string toLog;
00034         toLog = "Unable to create Gamefsm Thread :";
00035         toLog.append(SDL_GetError());
00036         myLogger->log(toLog, LOGMODE_ERROR);
00037         exit(5);
00038     }
00039 }

int Gamefsm::startThread void *  data  )  [static, private]
 

Wrapper around SDL threads, it simply callbacks threadBody method.

This method is static cause it must be called as a pure C function by SDL_CreateThread().

Author:
stonedz
Since:
pre-alpha
Parameters:
data Pointer to self, used to call threadBody.
Returns:
1
See also:
threadBody

Definition at line 93 of file Gamefsm.cpp.

References threadBody().

Referenced by Start().

00093                                    {
00094     Gamefsm* instance = reinterpret_cast<Gamefsm *>(data);
00095     instance->threadBody(); // Pass the control to the threadBody.
00096     return 1;
00097 }

void Gamefsm::Stop  ) 
 

Stops the FSM.

Author:
stonedz
Since:
pre-alpha

Definition at line 41 of file Gamefsm.cpp.

References exitRequest.

Referenced by Server::startListen().

00041                   {
00042     exitRequest = true;
00043 }

void Gamefsm::threadBody  ) 
 

Gamefsm body, it is where the FSM is started and controlled.

Author:
stonedz
Since:
pre-alpha
See also:
startThread
Todo:
TODO Implements the anti-lag timing.

Definition at line 21 of file Gamefsm.cpp.

References Evolve(), and ExecTrans().

Referenced by startThread().

00021                         {
00022     ExecTrans(t_init);
00023     while(!exitLoop){
00024         Evolve();
00025         SDL_Delay(100); // this shouldn't be fixed but for exampe 100-lastUpdateTime.
00026     }
00027 
00028 }

void Gamefsm::Update  )  [protected]
 

Main loop, update the world.

Author:
stonedz
Since:
pre-alpha

Definition at line 78 of file Gamefsm.cpp.

References ExecTrans().

00078                     {
00079     if(exitRequest){
00080         ExecTrans(t_quit);
00081     }
00082     else{
00083         //std::cout << "OK " ;
00084     }
00085 }


Member Data Documentation

void(Gamefsm::* Gamefsm::ExecState)(void) [protected]
 

Pointer to the next machine state.

Referenced by ExecTrans().

bool Gamefsm::exitLoop [private]
 

If true the FSM has exited and the whole loop may terminate.

Definition at line 124 of file Gamefsm.h.

Referenced by Quit().

bool Gamefsm::exitRequest [private]
 

A polite exit request has been sent to the FSM.

Definition at line 123 of file Gamefsm.h.

Referenced by Stop().

Logger* Gamefsm::myLogger [private]
 

A Logger instance, no need to free it (Singleton).

Definition at line 122 of file Gamefsm.h.

Referenced by ExecTrans(), and Start().

Server* Gamefsm::myServer [private]
 

Pointer to the Server instance that created the Gamefsm.

Definition at line 126 of file Gamefsm.h.

Referenced by Init().

SDL_Thread* Gamefsm::myThread [private]
 

The thread I'm running in (used to JOIN).

Definition at line 125 of file Gamefsm.h.

Referenced by Start().


The documentation for this class was generated from the following files:
Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1