From 95450252c1e4974a0122d971d620eee64710f24f Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 2 Jan 2023 12:24:04 +0900 Subject: [PATCH] Create Game class and set up glue code with it --- src/game.cc | 26 ++++++++++++++++++++++++++ src/game.h | 22 ++++++++++++++++++++++ src/main.cc | 23 +++++++++++++---------- wasm_build/Makefile | 3 ++- 4 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 src/game.cc create mode 100644 src/game.h diff --git a/src/game.cc b/src/game.cc new file mode 100644 index 0000000..c53012e --- /dev/null +++ b/src/game.cc @@ -0,0 +1,26 @@ +#include "game.h" + +// third party includes +#include + +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 index 0000000..2b9e42b --- /dev/null +++ b/src/game.h @@ -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 diff --git a/src/main.cc b/src/main.cc index 4936126..82f676e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -13,8 +13,11 @@ // 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(); diff --git a/wasm_build/Makefile b/wasm_build/Makefile index 3370736..6e4d4fc 100644 --- a/wasm_build/Makefile +++ b/wasm_build/Makefile @@ -6,7 +6,8 @@ endif SOURCES = \ ../src/main.cc \ - ../src/ems.cc + ../src/ems.cc \ + ../src/game.cc HEADERS = \ ../src/constants.h -- 2.49.0