Use game object for updates/draws/state

Also fix Makefile(s)
This commit is contained in:
Stephen Seo 2023-07-31 15:01:51 +09:00
parent c4d9dc8437
commit 7e954ed34b
5 changed files with 56 additions and 10 deletions

View file

@ -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 $@ $<

10
src/game.cc Normal file
View file

@ -0,0 +1,10 @@
#include "game.h"
// third party includes
#include <raylib.h>
Game::Game() {}
void Game::update() {}
void Game::draw() { DrawText("Testing...", 100, 100, 30, RAYWHITE); }

22
src/game.h Normal file
View file

@ -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

View file

@ -12,6 +12,7 @@
#include <raylib.h>
// 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();

View file

@ -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++