Ver código fonte

Add basic statefulness

Streetwalrus Einstein 10 anos atrás
pai
commit
96a34e6ca9
4 arquivos alterados com 92 adições e 28 exclusões
  1. 20 0
      include/State.h
  2. 24 0
      include/StateMap.h
  3. 44 0
      src/StateMap.cpp
  4. 4 28
      src/main.cpp

+ 20 - 0
include/State.h

@@ -0,0 +1,20 @@
+#ifndef INCLUDE_STATE_H
+#define INCLUDE_STATE_H
+
+#include "Camera.h"
+
+namespace WalrusRPG
+{
+    namespace States
+    {
+        class State
+        {
+          public:
+            virtual void update(unsigned dt) = 0;
+            virtual void render(unsigned dt) = 0;
+        };
+    }
+}
+
+
+#endif

+ 24 - 0
include/StateMap.h

@@ -0,0 +1,24 @@
+#ifndef INCLUDE_STATEMAP_H
+#define INCLUDE_STATEMAP_H
+
+#include "State.h"
+#include "Map.h"
+
+namespace WalrusRPG
+{
+    namespace States
+    {
+        class StateMap : public State
+        {
+          protected:
+            Camera camera;
+            Map ↦
+
+          public:
+            StateMap(int x, int y, Map &map);
+            void render(unsigned dt);
+            void update(unsigned dt);
+        };
+    }
+}
+#endif

+ 44 - 0
src/StateMap.cpp

@@ -0,0 +1,44 @@
+#include "StateMap.h"
+#include "misc.h"
+#include "version.h"
+#include "Graphics.h"
+#include "Pixel.h"
+#include "Text.h"
+
+#define STATEMAP WalrusRPG::States::StateMap
+
+using namespace WalrusRPG;
+
+namespace
+{
+    void print_debug_camera_data(const Camera &camera)
+    {
+        Graphics::Text::print_format(0, 8, "CAM : X : %d Y: %d", camera.get_x(),
+                                     camera.get_y());
+    }
+
+    void print_debug_map_data(const Map &map)
+    {
+        Graphics::Text::print_format(0, 16, "MAP : W: %d, H:%d", map.get_width(),
+                                     map.get_height());
+    }
+}
+
+STATEMAP::StateMap(int x, int y, Map &map) : camera(x, y), map(map)
+{
+}
+
+void STATEMAP::update(unsigned dt)
+{
+    camera.update(dt);
+}
+
+void STATEMAP::render(unsigned dt)
+{
+    Graphics::Pixel pix(Graphics::Green);
+    Graphics::buffer_fill(pix);
+    map.render(camera, dt);
+
+    print_debug_camera_data(camera);
+    print_debug_map_data(map);
+}

+ 4 - 28
src/main.cpp

@@ -5,25 +5,15 @@
 #include "Graphics.h"
 #include "Pixel.h"
 #include "Map.h"
-#include "Camera.h"
 #include "Text.h"
 #include "misc.h"
 #include "sprites.h"
 #include "version.h"
 #include "Interrupts.h"
+#include "StateMap.h"
 
 using namespace WalrusRPG;
 
-void print_debug_camera_data(const Camera &camera)
-{
-    Graphics::Text::print_format(0, 8, "CAM : X : %d Y: %d", camera.get_x(), camera.get_y());
-}
-
-void print_debug_map_data(const Map &map)
-{
-    Graphics::Text::print_format(0, 16, "MAP : W: %d, H:%d", map.get_width(), map.get_height());
-}
-
 void map_loop(unsigned x, unsigned y, Map &map)
 {
     // Free-running, no interrupts, divider = 1, 32 bit, wrapping
@@ -31,37 +21,23 @@ void map_loop(unsigned x, unsigned y, Map &map)
     Timers::load(0, 0);
     unsigned loop_time = 546; // 32768Hz/60ups
     unsigned loop_next = -loop_time;
-    unsigned last_frame = 0;
 
     unsigned keep_running = 1;
-    Camera camera((signed) x, (signed) y);
 
-    // Tileset asdf(better_character, 9, 16);
-    TileRenderer asdf(better_character, 9, 16);
-    Entity test_char(115, 90, 9, 16, &asdf, 0);
+    States::StateMap statemap(x, y, map);
 
     while (keep_running)
     {
         if (isKeyPressed(KEY_NSPIRE_ESC))
             keep_running = 0;
 
-        camera.update(0);
+        statemap.update(1);
 
         // Frameskip
         if (Timers::read(0) > loop_next)
         {
-            Graphics::Pixel pix(Graphics::Green);
-            // TODO?: Preset color macros/consts?
-            Graphics::buffer_fill(pix);
-            map.render(camera, 1);
-            test_char.render(camera, 1);
+            statemap.render(1);
             Graphics::Text::print_format(0, 0, "WalrusRPG test build %s", git_version);
-
-            print_debug_camera_data(camera);
-            print_debug_map_data(map);
-            unsigned frame_stamp = Timers::read(0);
-            Graphics::Text::print_format(0, 240 - 8, "%u fps", 32768 / (last_frame - frame_stamp));
-            last_frame = frame_stamp;
             Graphics::buffer_swap_render();
         }