src/Gamefsm.cpp

00001 #include "Gamefsm.h"
00002 #include "Logger.h"
00003 #include "Server.h"
00004 
00005 #include <iostream>
00006 
00007 Gamefsm::Gamefsm(Server* server)
00008     :myLogger(Logger::getInstance()),
00009     exitRequest(false),
00010     exitLoop(false),
00011     myThread(NULL),
00012     myServer(server)
00013 {
00014 }
00015 
00016 Gamefsm::~Gamefsm()
00017 {
00018 }
00019 
00020 
00021 void Gamefsm::threadBody(){
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 }
00029 
00030 void Gamefsm::Start(){
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 }
00040 
00041 void Gamefsm::Stop(){
00042     exitRequest = true;
00043 }
00044 
00045 void Gamefsm::Evolve(){
00046     (this->*ExecState)();
00047 }
00048 
00049 
00050 SDL_Thread* Gamefsm::getThread(){
00051     return myThread;
00052 }
00053 
00054 bool Gamefsm::ExecTrans(game_fsm_trans t){
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 }
00070 
00071 void Gamefsm::Init(){
00072     // Initialize the game...
00073     myServer->setInit(true);
00074     // then pass the control to the main loop.
00075     ExecTrans(t_main);
00076 }
00077 
00078 void Gamefsm::Update(){
00079     if(exitRequest){
00080         ExecTrans(t_quit);
00081     }
00082     else{
00083         //std::cout << "OK " ;
00084     }
00085 }
00086 
00087 void Gamefsm::Quit(){
00088     // Save the game and prepare for a proper shutdown
00089     //std::cout << std::endl << "quitting GAMEFSM";
00090     exitLoop = true;
00091 }
00092 
00093 int Gamefsm::startThread(void * data){
00094     Gamefsm* instance = reinterpret_cast<Gamefsm *>(data);
00095     instance->threadBody(); // Pass the control to the threadBody.
00096     return 1;
00097 }

Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1