]> git.seodisparate.com - RockPaperScissorsDuel/commitdiff
Create abstract class GameRenderer
authorStephen Seo <seo.disparate@gmail.com>
Wed, 4 Jan 2023 10:39:53 +0000 (19:39 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 4 Jan 2023 10:39:53 +0000 (19:39 +0900)
src/game.h
src/game_renderer.h [new file with mode: 0644]
src/main.cc
wasm_build/Makefile

index f95fa41269ed444da15dfc7837c6194c31cc18f0..9f89bb2d3f805140e783b72d6bc97f65a655b98c 100644 (file)
@@ -9,7 +9,10 @@
 // third party includes
 #include <raylib.h>
 
-class Game {
+// local includes
+#include "game_renderer.h"
+
+class Game : public GameRenderer {
 public:
   Game();
 
@@ -17,11 +20,11 @@ public:
                     const char *currentPlayer, char first_first,
                     char first_second, char first_third, char second_first,
                     char second_second, char second_third, bool first_ready,
-                    bool second_ready, int pos, int matchup_idx);
+                    bool second_ready, int pos, int matchup_idx) override;
 
-  void do_update();
+  void do_update() override;
 
-  void screen_size_changed();
+  void screen_size_changed() override;
 
 private:
   void update_impl();
diff --git a/src/game_renderer.h b/src/game_renderer.h
new file mode 100644 (file)
index 0000000..3ce3c04
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef ROCK_PAPER_SCISSORS_DUEL_GAME_RENDERER_H_
+#define ROCK_PAPER_SCISSORS_DUEL_GAME_RENDERER_H_
+
+class GameRenderer {
+public:
+  GameRenderer() {}
+  virtual ~GameRenderer() {}
+
+  virtual void update_state(const char *playerOne, const char *playerTwo,
+                            const char *currentPlayer, char first_first,
+                            char first_second, char first_third,
+                            char second_first, char second_second,
+                            char second_third, bool first_ready,
+                            bool second_ready, int pos, int matchup_idx) = 0;
+
+  virtual void do_update() = 0;
+
+  virtual void screen_size_changed() = 0;
+};
+
+#endif
index eddd6f6677173815dfa0970b2e2b1d6baa201f77..5bfe8f1809fa810e714ebceae925bc62ad20ba1c 100644 (file)
@@ -8,12 +8,16 @@
 #include <random>
 #endif
 
+// standard library includes
+#include <memory>
+
 // third party includes
 #include <raylib.h>
 
 // local includes
 #include "constants.h"
 #include "game.h"
+#include "game_renderer.h"
 
 #ifdef __EMSCRIPTEN__
 static void *global_game_ptr = nullptr;
@@ -26,7 +30,7 @@ int EMSCRIPTEN_KEEPALIVE game_visual_update(
     char first_first, char first_second, char first_third, char second_first,
     char second_second, char second_third, bool first_ready, bool second_ready,
     int pos, int matchup_idx) {
-  ((Game *)global_game_ptr)
+  ((GameRenderer *)global_game_ptr)
       ->update_state(playerOne, playerTwo, currentPlayer, first_first,
                      first_second, first_third, second_first, second_second,
                      second_third, first_ready, second_ready, pos, matchup_idx);
@@ -39,13 +43,13 @@ EM_BOOL resize_event_callback(int event_type, const EmscriptenUiEvent *event,
                               void *ud) {
   if (event_type == EMSCRIPTEN_EVENT_RESIZE) {
     SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height());
-    ((Game *)ud)->screen_size_changed();
+    ((GameRenderer *)ud)->screen_size_changed();
   }
   return false;
 }
 #endif
 
-void game_update(void *game_ptr) { ((Game *)game_ptr)->do_update(); }
+void game_update(void *game_ptr) { ((GameRenderer *)game_ptr)->do_update(); }
 
 int main() {
 #ifdef __EMSCRIPTEN__
@@ -55,22 +59,22 @@ int main() {
   InitWindow(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, "RPSDuel_Native");
 #endif
 
-  Game game{};
+  std::unique_ptr<GameRenderer> renderer = std::make_unique<Game>();
 
 #ifdef __EMSCRIPTEN__
-  global_game_ptr = &game;
+  global_game_ptr = renderer.get();
 
   SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height());
 
-  emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, &game, false,
-                                 resize_event_callback);
+  emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, renderer.get(),
+                                 false, resize_event_callback);
 
-  emscripten_set_main_loop_arg(game_update, &game, 0, 1);
+  emscripten_set_main_loop_arg(game_update, renderer.get(), 0, 1);
 #else
   SetTargetFPS(60);
 
   while (!WindowShouldClose()) {
-    game_update(&game);
+    game_update(renderer.get());
   }
 
   CloseAudioDevice();
index 4f6f210caadf7d44383a3ff0c6ef12657ecb1fbd..1871321dee53954e2b90e747f1a3177292bb9a4b 100644 (file)
@@ -14,7 +14,8 @@ HEADERS = \
                ../src/constants.h \
                ../src/ems.h \
                ../src/game.h \
-               ../src/helpers.h
+               ../src/helpers.h \
+               ../src/game_renderer.h
 
 CXX = source ${HOME}/git/emsdk/emsdk_env.sh && em++