From 7e954ed34bfd46f10b5e303f47861c007b10fa9d Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 31 Jul 2023 15:01:51 +0900 Subject: [PATCH] Use game object for updates/draws/state Also fix Makefile(s) --- Makefile | 12 ++++++++---- src/game.cc | 10 ++++++++++ src/game.h | 22 ++++++++++++++++++++++ src/main.cc | 16 ++++++++++++---- wasm_build/Makefile | 6 ++++-- 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 src/game.cc create mode 100644 src/game.h diff --git a/Makefile b/Makefile index 609375a..41dedd7 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,11 @@ LINKER_FLAGS = -lraylib OBJDIR = objdir SOURCES = \ - src/main.cc + src/main.cc \ + src/game.cc -HEADERS = +HEADERS = \ + src/game.h OBJECTS = $(addprefix ${OBJDIR}/,$(subst .cc,.cc.o,${SOURCES})) @@ -30,6 +32,8 @@ clean: format: clang-format -i --style=google ${HEADERS} ${SOURCES} -${OBJDIR}/%.cc.o: %.cc +.SECONDEXPANSION: + +${OBJDIR}/%.cc.o: $$(subst ${OBJDIR}/,,%.cc) ${HEADERS} @mkdir -p $(dir $@) - ${CXX} -c ${CXX_FLAGS} -o $@ $^ + ${CXX} -c ${CXX_FLAGS} -o $@ $< diff --git a/src/game.cc b/src/game.cc new file mode 100644 index 0000000..77a0b4a --- /dev/null +++ b/src/game.cc @@ -0,0 +1,10 @@ +#include "game.h" + +// third party includes +#include + +Game::Game() {} + +void Game::update() {} + +void Game::draw() { DrawText("Testing...", 100, 100, 30, RAYWHITE); } diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..84f5e9f --- /dev/null +++ b/src/game.h @@ -0,0 +1,22 @@ +#ifndef JUMPARTIFACT_DOT_COM_DEMO_GAME_H_ +#define JUMPARTIFACT_DOT_COM_DEMO_GAME_H_ + +class Game { + public: + Game(); + + // No copy. + Game(const Game&) = delete; + Game& operator=(const Game&) = delete; + + // Allow move. + Game(Game&&) = default; + Game& operator=(Game&&) = default; + + void update(); + void draw(); + + private: +}; + +#endif diff --git a/src/main.cc b/src/main.cc index 18ea0ac..f62ab94 100644 --- a/src/main.cc +++ b/src/main.cc @@ -12,6 +12,7 @@ #include // local includes +#include "game.h" #ifdef __EMSCRIPTEN__ extern "C" { @@ -29,26 +30,33 @@ EM_BOOL resize_event_callback(int event_type, const EmscriptenUiEvent *event, // Main loop frame void jumpartifact_demo_update(void *ud) { + Game *game = (Game *)ud; + + game->update(); + BeginDrawing(); ClearBackground(BLACK); - DrawText("Testing...", 100, 100, 30, RAYWHITE); + game->draw(); EndDrawing(); } int main() { InitWindow(800, 800, "Demo"); + + Game game{}; + #ifdef __EMSCRIPTEN__ SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height()); - emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, false, + emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, &game, false, resize_event_callback); - emscripten_set_main_loop_arg(jumpartifact_demo_update, 0, 0, 1); + emscripten_set_main_loop_arg(jumpartifact_demo_update, &game, 0, 1); #else SetTargetFPS(60); while (!WindowShouldClose()) { - jumpartifact_demo_update(0); + jumpartifact_demo_update(&game); } CloseAudioDevice(); diff --git a/wasm_build/Makefile b/wasm_build/Makefile index 4fa1364..482aae2 100644 --- a/wasm_build/Makefile +++ b/wasm_build/Makefile @@ -6,10 +6,12 @@ endif SOURCES = \ ../src/main.cc \ - ../src/ems.cc + ../src/ems.cc \ + ../src/game.cc HEADERS = \ - ../src/ems.h + ../src/ems.h \ + ../src/game.h CXX = source ${HOME}/git/emsdk/emsdk_env.sh && em++