Przeglądaj źródła

Moved the main game state machine into a static class.

Eiyeron Fulmincendii 9 lat temu
rodzic
commit
8e59870e3d
3 zmienionych plików z 72 dodań i 69 usunięć
  1. 65 59
      src/engine/StateMachine.cpp
  2. 3 7
      src/engine/StateMachine.h
  3. 4 3
      src/engine/main.cpp

+ 65 - 59
src/engine/StateMachine.cpp

@@ -7,12 +7,10 @@
 #include "input/Input.h"
 
 using namespace WalrusRPG::Graphics;
-using namespace WalrusRPG::States;
 using namespace WalrusRPG::Timing;
 using WalrusRPG::Input::Key;
 using WalrusRPG::Input::KeyState;
-
-#define STATEMACHINE WalrusRPG::StateMachine
+using WalrusRPG::States::State;
 
 namespace
 {
@@ -54,74 +52,82 @@ namespace
         draw_button(48, 44, key_get_state(Key::K_B));
         draw_button(56, 36, key_get_state(Key::K_A));
     }
-} /* namespace  */
+    
+} /* namespace */
 
-STATEMACHINE::StateMachine(State *state)
+namespace WalrusRPG{namespace StateMachine
 {
-    push(state);
-}
 
-STATEMACHINE::~StateMachine()
-{
-    while (!stack.empty())
-        pop();
-}
+    static tinystl::vector<WalrusRPG::States::State *> stack;
 
-void STATEMACHINE::push(State *state)
-{
-    stack.push_back(state);
-}
+    void init()
+    {
 
-void STATEMACHINE::pop()
-{
-    delete stack.back();
-    stack.pop_back();
-}
+    }
 
-void STATEMACHINE::run()
-{
-    const unsigned loop_time = TIMER_FREQ / 60;
-    unsigned loop_next = loop_time;
-    unsigned last_update = 0, update_stamp, update_time;
-    unsigned last_frame = 0, frame_stamp, frame_time;
+    void deinit()
+    {
+        stack.clear();
+    }
+
+    void push(State *state)
+    {
+        stack.push_back(state);
+    }
 
-    while (!stack.empty())
+    void pop()
     {
-        update_stamp = Timing::gettime();
-        update_time = update_stamp - last_update;
-        Input::key_poll();
-        stack.back()->update(100 * update_time / TIMER_FREQ);
-        last_update = update_stamp;
+        delete stack.back();
+        stack.pop_back();
+    }
+
+    void run()
+    {
+        const unsigned loop_time = TIMER_FREQ / 60;
+        unsigned loop_next = loop_time;
+        unsigned last_update = 0, update_stamp, update_time;
+        unsigned last_frame = 0, frame_stamp, frame_time;
 
-        if (Timing::gettime() < loop_next)
+        while (!stack.empty())
         {
-            frame_stamp = Timing::gettime();
-            frame_time = frame_stamp - last_frame;
-            Graphics::frame_begin();
-            stack.back()->render(100 * frame_time / TIMER_FREQ);
-            last_frame = frame_stamp;
-
-            Text::print_format(0, 0, "WalrusRPG test build %s", git_version);
-            if (frame_time != 0 && update_time != 0)
+            update_stamp = Timing::gettime();
+            update_time = update_stamp - last_update;
+            Input::key_poll();
+            stack.back()->update(100 * update_time / TIMER_FREQ);
+            last_update = update_stamp;
+
+            if (Timing::gettime() < loop_next)
             {
-                Text::print_format(0, 240 - 8, "%ufps, %uups", TIMER_FREQ / frame_time,
-                                   TIMER_FREQ / update_time);
+                frame_stamp = Timing::gettime();
+                frame_time = frame_stamp - last_frame;
+                Graphics::frame_begin();
+                stack.back()->render(100 * frame_time / TIMER_FREQ);
+                last_frame = frame_stamp;
+
+                // Text::print_format(0, 0, "WalrusRPG test build %s", git_version);
+                if (frame_time != 0 && update_time != 0)
+                {
+                    Text::print_format(0, 240 - 8, "%ufps, %uups", TIMER_FREQ / frame_time,
+                                       TIMER_FREQ / update_time);
+                }
+                // draw_buttons();
+                Graphics::frame_end();
             }
-            draw_buttons();
-            Graphics::frame_end();
-        }
 
-        if (Input::key_pressed(Key::K_SELECT))
-        {
-            while (Input::key_down(Key::K_SELECT))
-                Input::key_poll();
-            this->pop();
-        }
+            if (Input::key_pressed(Key::K_SELECT))
+            {
+                while (Input::key_down(Key::K_SELECT))
+                    Input::key_poll();
+                StateMachine::pop();
+            }
 
-#ifdef ACTIVE_WAIT
-        while (Timing::gettime() < loop_next)
-            ;
-#endif
-        loop_next += loop_time;
+    #ifdef ACTIVE_WAIT
+            while (Timing::gettime() < loop_next)
+                ;
+    #endif
+            loop_next += loop_time;
+        }
     }
-}
+}} /* namespace StateMachine */
+
+

+ 3 - 7
src/engine/StateMachine.h

@@ -6,14 +6,10 @@
 
 namespace WalrusRPG
 {
-    class StateMachine
+    namespace StateMachine
     {
-      protected:
-        tinystl::vector<WalrusRPG::States::State *> stack;
-
-      public:
-        StateMachine(WalrusRPG::States::State *state);
-        ~StateMachine();
+        void init();
+        void deinit();
         void push(WalrusRPG::States::State *state);
         void pop();
         void run();

+ 4 - 3
src/engine/main.cpp

@@ -48,13 +48,14 @@ int main(int argc, char *argv[])
     map.anim.add_animation(21, {stripe21, true, 0});
     map.anim.add_animation(22, {stripe22, true, 0});
 
-    StateMachine machine(new States::StateMap(0, 0, map));
-    machine.run();
+    StateMachine::init();
+    StateMachine::push(new States::StateMap(0, 0, map));
+    StateMachine::run();
+    StateMachine::deinit();
 
     Quirks::deinit();
     Timing::deinit();
     Graphics::deinit();
-
     delete[] dungeonTest;
     delete[] dungeonTest2;