]> git.seodisparate.com - RockPaperScissorsDuel/commitdiff
Create Game class and set up glue code with it
authorStephen Seo <seo.disparate@gmail.com>
Mon, 2 Jan 2023 03:24:04 +0000 (12:24 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 2 Jan 2023 03:24:04 +0000 (12:24 +0900)
src/game.cc [new file with mode: 0644]
src/game.h [new file with mode: 0644]
src/main.cc
wasm_build/Makefile

diff --git a/src/game.cc b/src/game.cc
new file mode 100644 (file)
index 0000000..c53012e
--- /dev/null
@@ -0,0 +1,26 @@
+#include "game.h"
+
+// third party includes
+#include <raylib.h>
+
+Game::Game() {}
+
+void Game::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) {}
+
+void Game::do_update() {
+  update_impl();
+  draw_impl();
+}
+
+void Game::update_impl() {}
+
+void Game::draw_impl() {
+  BeginDrawing();
+  ClearBackground(BLACK);
+  DrawText("Testing...", 100, 100, 30, RAYWHITE);
+  EndDrawing();
+}
diff --git a/src/game.h b/src/game.h
new file mode 100644 (file)
index 0000000..2b9e42b
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef ROCK_PAPER_SCISSORS_DUEL_GAME_H_
+#define ROCK_PAPER_SCISSORS_DUEL_GAME_H_
+
+class Game {
+public:
+  Game();
+
+  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);
+
+  void do_update();
+
+private:
+
+  void update_impl();
+  void draw_impl();
+
+};
+
+#endif
index 49361261967fa08a8e51d7817b8f98015792c3de..82f676e88a134c64e458c0dc41dd4bdab8a5e4c9 100644 (file)
 
 // local includes
 #include "constants.h"
+#include "game.h"
 
 #ifdef __EMSCRIPTEN__
+static void *global_game_ptr = nullptr;
+
 // em exposed fns
 extern "C" {
 
@@ -23,7 +26,10 @@ void 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) {
-  // TODO
+  ((Game *)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);
 }
 
 } // end em exposed functions
@@ -37,12 +43,7 @@ EM_BOOL resize_event_callback(int event_type, const EmscriptenUiEvent *event,
 }
 #endif
 
-void game_update(void *game_ptr) {
-  BeginDrawing();
-  ClearBackground(BLACK);
-  DrawText("Testing...", 100, 100, 30, RAYWHITE);
-  EndDrawing();
-}
+void game_update(void *game_ptr) { ((Game *)game_ptr)->do_update(); }
 
 int main() {
 #ifdef __EMSCRIPTEN__
@@ -52,19 +53,21 @@ int main() {
   InitWindow(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, "RPSDuel_Native");
 #endif
 
+  Game game{};
+  global_game_ptr = &game;
+
 #ifdef __EMSCRIPTEN__
   SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height());
 
   emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, false,
                                  resize_event_callback);
 
-  // TODO set game ptr
-  emscripten_set_main_loop_arg(game_update, nullptr, 0, 1);
+  emscripten_set_main_loop_arg(game_update, &game, 0, 1);
 #else
   SetTargetFPS(60);
 
   while (!WindowShouldClose()) {
-    game_update(nullptr);
+    game_update(&game);
   }
 
   CloseAudioDevice();
index 3370736b46544d7846a16a8d8e170120ff7c3901..6e4d4fc854095c0bf60da2dd34fb351e334fdb28 100644 (file)
@@ -6,7 +6,8 @@ endif
 
 SOURCES = \
                ../src/main.cc \
-               ../src/ems.cc
+               ../src/ems.cc \
+               ../src/game.cc
 
 HEADERS = \
                ../src/constants.h