WindowShouldClose() was moved to function in lib to call from main

This commit is contained in:
Joseph Ferano 2024-01-10 15:15:42 +07:00
parent 4b63f91494
commit d260da4c25
4 changed files with 27 additions and 26 deletions

View File

@ -9,6 +9,8 @@
// #define SCREEN_HEIGHT 1080 // #define SCREEN_HEIGHT 1080
#define SCREEN_WIDTH 800 #define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600 #define SCREEN_HEIGHT 600
#define TARGET_FPS 60
typedef struct Boid { typedef struct Boid {
Point position; Point position;
@ -34,7 +36,7 @@ static GameState *init() {
int win_pos_y = 30; int win_pos_y = 30;
SetWindowPosition(win_pos_x, win_pos_y); SetWindowPosition(win_pos_x, win_pos_y);
SetTargetFPS(60); SetTargetFPS(TARGET_FPS);
GameState *state = malloc(sizeof(struct GameApi)); GameState *state = malloc(sizeof(struct GameApi));
state->num_boids = 16; state->num_boids = 16;
@ -68,8 +70,8 @@ static void finalize(GameState *state) {
static void reload(GameState *state) { static void reload(GameState *state) {
(void)state; (void)state;
state->max_speed = 5.0f; state->max_speed = 20.0f;
state->max_force = 0.1; state->max_force = 0.5;
printf("Reloaded Game\n"); printf("Reloaded Game\n");
} }
@ -77,15 +79,12 @@ static void unload(GameState *state) {
(void)state; (void)state;
} }
static int step(GameState *state) { // We need this so all the raylib state is in the right place
if (WindowShouldClose()) { static bool should_close() {
return 1; return WindowShouldClose();
} }
int return_int = 0; static void step(GameState *state) {
if (IsKeyPressed(KEY_SPACE)) {
return_int = 2;
}
// Process Input // Process Input
if (IsMouseButtonPressed(0)) { if (IsMouseButtonPressed(0)) {
Vector2 mouse_pos = GetMousePosition(); Vector2 mouse_pos = GetMousePosition();
@ -124,7 +123,6 @@ static int step(GameState *state) {
DrawFPS(SCREEN_WIDTH - 80, 10); DrawFPS(SCREEN_WIDTH - 80, 10);
} }
EndDrawing(); EndDrawing();
return return_int;
} }
const struct GameApi GAME_API = { const struct GameApi GAME_API = {
@ -132,5 +130,6 @@ const struct GameApi GAME_API = {
.reload = reload, .reload = reload,
.step = step, .step = step,
.unload = unload, .unload = unload,
.finalize = finalize .finalize = finalize,
.should_close = should_close,
}; };

View File

@ -4,10 +4,11 @@ struct GameState;
typedef struct GameApi { typedef struct GameApi {
struct GameState *(*init)(); struct GameState *(*init)();
bool (*should_close)();
void (*finalize) (struct GameState *state); void (*finalize) (struct GameState *state);
void (*reload) (struct GameState *state); void (*reload) (struct GameState *state);
void (*unload) (struct GameState *state); void (*unload) (struct GameState *state);
int (*step) (struct GameState *state); void (*step) (struct GameState *state);
} GameApi; } GameApi;
extern const GameApi GAME_API; extern const GameApi GAME_API;

View File

@ -8,12 +8,7 @@
#include "include/raymath.h" #include "include/raymath.h"
#include "boids_game.h" #include "boids_game.h"
#define SCREEN_WIDTH 1300
#define SCREEN_HEIGHT 1080
#define TARGET_FPS 60
const char* GAME_LIB = "./libboids.so"; const char* GAME_LIB = "./libboids.so";
char inotify_buf[1024];
struct Game { struct Game {
void* handle; void* handle;
@ -27,6 +22,7 @@ struct Game {
void load_game(struct Game* game) { void load_game(struct Game* game) {
struct stat attr; struct stat attr;
if ((stat(GAME_LIB, &attr) == 0) && (game->gamelib_id != attr.st_ino)) { if ((stat(GAME_LIB, &attr) == 0) && (game->gamelib_id != attr.st_ino)) {
usleep(10000);
if (game->handle) { if (game->handle) {
game->api.unload(game->state); game->api.unload(game->state);
dlclose(game->handle); dlclose(game->handle);
@ -57,10 +53,12 @@ void load_game(struct Game* game) {
int main(void) { int main(void) {
struct Game game = {0}; struct Game game = {0};
int should_exit = 0; while (1) {
while (should_exit != 1) {
load_game(&game); 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); game.api.finalize(game.state);
free(game.state); free(game.state);

View File

@ -1,13 +1,14 @@
* Bexorg Hours * Hours
#+BEGIN: clocktable :scope subtree :maxlevel 2 #+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 | | | 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-06 Sat> | | 2:10 |
| \_ <2024-01-09 Tue> | | 2:53 | | \_ <2024-01-09 Tue> | | 2:53 |
| \_ <2024-01-10 Wed> | | 2:51 |
#+END: #+END:
** <2024-01-06 Sat> ** <2024-01-06 Sat>
@ -22,5 +23,7 @@ CLOCK: [2024-01-09 Tue 10:45]--[2024-01-09 Tue 11:15] => 0:30
:END: :END:
** <2024-01-10 Wed> ** <2024-01-10 Wed>
:LOGBOOK: :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 CLOCK: [2024-01-10 Wed 08:10]--[2024-01-10 Wed 09:40] => 1:30
:END: :END: