From d260da4c250e5234b42d254469a004a3820db5a1 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Wed, 10 Jan 2024 15:15:42 +0700 Subject: [PATCH] WindowShouldClose() was moved to function in lib to call from main --- boids_game.c | 25 ++++++++++++------------- boids_game.h | 3 ++- boids_main.c | 14 ++++++-------- hours.org | 11 +++++++---- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/boids_game.c b/boids_game.c index 2e8e87a..e6f91fe 100644 --- a/boids_game.c +++ b/boids_game.c @@ -9,6 +9,8 @@ // #define SCREEN_HEIGHT 1080 #define SCREEN_WIDTH 800 #define SCREEN_HEIGHT 600 +#define TARGET_FPS 60 + typedef struct Boid { Point position; @@ -34,7 +36,7 @@ static GameState *init() { int win_pos_y = 30; SetWindowPosition(win_pos_x, win_pos_y); - SetTargetFPS(60); + SetTargetFPS(TARGET_FPS); GameState *state = malloc(sizeof(struct GameApi)); state->num_boids = 16; @@ -68,8 +70,8 @@ static void finalize(GameState *state) { static void reload(GameState *state) { (void)state; - state->max_speed = 5.0f; - state->max_force = 0.1; + state->max_speed = 20.0f; + state->max_force = 0.5; printf("Reloaded Game\n"); } @@ -77,15 +79,12 @@ static void unload(GameState *state) { (void)state; } -static int step(GameState *state) { - if (WindowShouldClose()) { - return 1; - } +// We need this so all the raylib state is in the right place +static bool should_close() { + return WindowShouldClose(); +} - int return_int = 0; - if (IsKeyPressed(KEY_SPACE)) { - return_int = 2; - } +static void step(GameState *state) { // Process Input if (IsMouseButtonPressed(0)) { Vector2 mouse_pos = GetMousePosition(); @@ -124,7 +123,6 @@ static int step(GameState *state) { DrawFPS(SCREEN_WIDTH - 80, 10); } EndDrawing(); - return return_int; } const struct GameApi GAME_API = { @@ -132,5 +130,6 @@ const struct GameApi GAME_API = { .reload = reload, .step = step, .unload = unload, - .finalize = finalize + .finalize = finalize, + .should_close = should_close, }; diff --git a/boids_game.h b/boids_game.h index dfef593..25edde9 100644 --- a/boids_game.h +++ b/boids_game.h @@ -4,10 +4,11 @@ struct GameState; typedef struct GameApi { struct GameState *(*init)(); + bool (*should_close)(); void (*finalize) (struct GameState *state); void (*reload) (struct GameState *state); void (*unload) (struct GameState *state); - int (*step) (struct GameState *state); + void (*step) (struct GameState *state); } GameApi; extern const GameApi GAME_API; diff --git a/boids_main.c b/boids_main.c index 0d0adeb..dab2657 100644 --- a/boids_main.c +++ b/boids_main.c @@ -8,12 +8,7 @@ #include "include/raymath.h" #include "boids_game.h" -#define SCREEN_WIDTH 1300 -#define SCREEN_HEIGHT 1080 -#define TARGET_FPS 60 - const char* GAME_LIB = "./libboids.so"; -char inotify_buf[1024]; struct Game { void* handle; @@ -27,6 +22,7 @@ struct Game { void load_game(struct Game* game) { struct stat attr; if ((stat(GAME_LIB, &attr) == 0) && (game->gamelib_id != attr.st_ino)) { + usleep(10000); if (game->handle) { game->api.unload(game->state); dlclose(game->handle); @@ -57,10 +53,12 @@ void load_game(struct Game* game) { int main(void) { struct Game game = {0}; - int should_exit = 0; - while (should_exit != 1) { + while (1) { load_game(&game); - should_exit = game.api.step(game.state); + if (game.api.should_close()) { + break; + } + game.api.step(game.state); } game.api.finalize(game.state); free(game.state); diff --git a/hours.org b/hours.org index 99a778d..bc36236 100644 --- a/hours.org +++ b/hours.org @@ -1,13 +1,14 @@ -* Bexorg Hours +* Hours #+BEGIN: clocktable :scope subtree :maxlevel 2 -#+CAPTION: Clock summary at [2024-01-10 Wed 08:45] +#+CAPTION: Clock summary at [2024-01-11 Thu 10:45] | Headline | Time | | |----------------------+------+------| -| *Total time* | *5:03* | | +| *Total time* | *7:54* | | |----------------------+------+------| -| Bexorg Hours | 5:03 | | +| Hours | 7:54 | | | \_ <2024-01-06 Sat> | | 2:10 | | \_ <2024-01-09 Tue> | | 2:53 | +| \_ <2024-01-10 Wed> | | 2:51 | #+END: ** <2024-01-06 Sat> @@ -22,5 +23,7 @@ CLOCK: [2024-01-09 Tue 10:45]--[2024-01-09 Tue 11:15] => 0:30 :END: ** <2024-01-10 Wed> :LOGBOOK: +CLOCK: [2024-01-10 Wed 15:00]--[2024-01-10 Wed 15:16] => 0:16 +CLOCK: [2024-01-10 Wed 10:45]--[2024-01-10 Wed 11:50] => 1:05 CLOCK: [2024-01-10 Wed 08:10]--[2024-01-10 Wed 09:40] => 1:30 :END: