renamed libpg to libbear
[physics.git] / src / game.cpp
index ebf2ba7..000bdf4 100644 (file)
+/*
+ *  Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include "game.h"
-#include "debug.h"
+
+#include <bear/debug.h>
+using namespace bear;
 
 #include <vector>
 using std::vector;
 
 #include "entityCreator.h"
+#include "effectManager.h"
 
 #include "GameStates/GameState.h"
 #include "GameStates/Running.h"
 #include "GameStates/Paused.h"
 #include "GameStates/CreatingPolygon.h"
 
+
 /// ***** Private Variables *****
 
 // The stack of active game states
-vector<GameState*> active_States;
+static vector<GameState*> s_active_States;
 
 // Pointers to each possible game state
-// inserted and removed from the active_States
-Running* running;
-Paused* paused;
-CreatingPolygon* creating_Polygon;
+// inserted and removed from the s_active_States
+static vector<GameState*> s_possible_States;
+
+// true if the top state requested itself to be poped
+static bool s_bPopState;
+// pointer to a state wishing to be added
+static GameState* s_pPushState;
 
 
 /// ***** Public Methods *****
-void gameInit()
+
+void game::init()
 {
-    running = new Running();
-    paused = new Paused();
-    creating_Polygon = new CreatingPolygon();
+    s_bPopState = false;
+    s_pPushState = NULL;
 
     // create starting entities
     creator::init();
+    effect::init();
+
+    Running* pRunning = new Running();
+    s_possible_States.push_back(pRunning);
+    s_possible_States.push_back(new Paused());
+    s_possible_States.push_back(new CreatingPolygon());
 
-    active_States.push_back(running);
+    s_active_States.push_back(pRunning);
 
-#ifdef DEBUGGING
-    cout << "World Created" << endl;
-#endif
+    DPF(0, "World Created");
 }
 
-void gameClean()
+void game::clean()
 {
+    effect::clean();
     creator::clean();
 
-    delete creating_Polygon;
-    delete paused;
-    delete running;
+    for(unsigned int i=0; i < s_possible_States.size(); i++)
+    {
+        delete s_possible_States[i];
+    }
+    s_possible_States.clear();
+    s_active_States.clear();
 }
 
-void gameInput()
+void game::handleInput()
 {
-    int last = active_States.size() -1;
+    creator::handleInput();
+
+    int iLast = s_active_States.size() -1;
+
+    for(unsigned int i=0; i < s_possible_States.size(); i++)
+    {
+        GameState* pState = s_possible_States[i];
+
+        if(s_active_States[iLast] != pState && pState->pushMe())
+        {
+            s_pPushState = pState;
+        }
+    }
+
+
     for( int i = 0;
-         i <= last;
+         i <= iLast;
          i++ )
     {
-        if( i == last )
-            active_States[i]->handleInput(true);
+        if( i == iLast )
+        {
+            if(s_active_States[i]->popMe())
+                s_bPopState = true;
+            else
+                s_active_States[i]->handleInput(true);
+        }
         else
-            active_States[i]->handleInput(false);
+            s_active_States[i]->handleInput(false);
     }
 }
 
-void gameUpdate(float time_step)
+void game::update(float fTimeStep)
 {
-    int last = active_States.size() -1;
+    if(s_bPopState)
+    {
+        s_active_States.pop_back();
+        s_bPopState = false;
+    }
+
+    if(s_pPushState != NULL)
+    {
+        s_active_States.push_back(s_pPushState);
+        s_pPushState = NULL;
+    }
+
+    int iLast = s_active_States.size() -1;
     for( int i = 0;
-         i <= last;
+         i <= iLast;
          i++ )
     {
-        if( i == last )
-            active_States[i]->update(time_step, true);
+        if( i == iLast )
+            s_active_States[i]->update(fTimeStep, true);
         else
-            active_States[i]->update(time_step, false);
+            s_active_States[i]->update(fTimeStep, false);
     }
 }
 
-void gameDraw()
+void game::draw()
 {
-    int last = active_States.size() -1;
+    int iLast = s_active_States.size() -1;
     for( int i = 0;
-         i <= last;
+         i <= iLast;
          i++ )
     {
-        if( i == last )
-            active_States[i]->draw(true);
+        if( i == iLast )
+            s_active_States[i]->draw(true);
         else
-            active_States[i]->draw(false);
+            s_active_States[i]->draw(false);
     }
 }