Hot reloading: Current configuration seems to hot reload correctly

This commit is contained in:
Joseph Ferano 2024-01-10 09:13:24 +07:00
parent 517df72ec3
commit 93972ba1d4
8 changed files with 65 additions and 28 deletions

View File

@ -14,15 +14,14 @@ sprites.o: sprites.c sprites.h lib.h
dod: dod.c ./lib/libraylib.a
$(CC) $(CFLAGS) -Iinclude/ -lm dod.c -o dod ./lib/libraylib.a
boids_main: boids_main.c lib.h ./lib/libraylib.a libboids.so
$(CC) $(CFLAGS) -Iinclude/ -fPIC -ldl -lm $< -o $@ ./lib/libraylib.a
boids_main: boids_main.c lib.h libboids.so
$(CC) $(CFLAGS) -Iinclude/ -ldl -lm -lraylib $< -o $@ -L./lib -Wl,-rpath,./lib/
libboids.so: boids_game.c boids_game.h lib.h
$(CC) $(CFLAGS) -shared -fPIC -Iinclude/ -lm -lOpenGL $< -o $@ ./lib/libraylib.a
$(CC) $(CFLAGS) -shared -fPIC -Iinclude/ -lm $< -o $@
run: all
./main
clean:
rm -vf *.so *.o main boids_main dod

View File

@ -48,30 +48,41 @@ static GameState *init() {
boid->acceleration = (Vector2){0};
}
printf("I loaded the contents\n");
printf("Initialized Game\n");
return state;
}
static void finalize(GameState *state) {
(void)state;
free(state->boids);
CloseWindow();
}
static void reload(GameState *state) {
(void)state;
state->max_speed = 5.0f;
state->max_force = 0.1;
printf("Reloaded Game\n");
}
static void unload(GameState *state) {
(void)state;
}
static void step(GameState *state) {
static int step(GameState *state) {
if (WindowShouldClose()) {
return 1;
}
int return_int = 0;
if (IsKeyPressed(KEY_SPACE)) {
return_int = 2;
}
// Process Input
if (IsMouseButtonPressed(0)) {
Vector2 mouse_pos = GetMousePosition();
state->target_pos = (PointOption){.tag = SOME, .some.point = mouse_pos};
}
// Update
for (int i = 0; i < state->num_boids; i++) {
Boid *boid = &state->boids[i];
@ -105,6 +116,7 @@ static void step(GameState *state) {
}
EndDrawing();
return return_int;
}
const struct GameApi GAME_API = {

View File

@ -7,7 +7,7 @@ typedef struct GameApi {
void (*finalize) (struct GameState *state);
void (*reload) (struct GameState *state);
void (*unload) (struct GameState *state);
void (*step) (struct GameState *state);
int (*step) (struct GameState *state);
} GameApi;
extern const GameApi GAME_API;

View File

@ -14,26 +14,35 @@
#define SCREEN_HEIGHT 1080
#define TARGET_FPS 60
const char *GAME_LIB = "./libboids.so";
const char* GAME_LIB = "./libboids.so";
char epoll_buf[1024];
struct Game {
void *handle;
struct Game
{
void* handle;
struct GameApi api;
struct GameState *state;
struct GameState* state;
};
#define MAX_EVENTS 1
void load_game(struct Game *game) {
void *handle = dlopen(GAME_LIB, RTLD_NOW);
void load_game(struct Game* game) {
if (game->handle) {
game->api.unload(game->state);
dlclose(game->handle);
}
void* handle = dlopen(GAME_LIB, RTLD_NOW);
if (handle) {
game->handle = handle;
const struct GameApi *api = dlsym(game->handle, "GAME_API");
const struct GameApi* api = dlsym(game->handle, "GAME_API");
if (api != NULL) {
printf("Loaded API, calling init()\n");
game->api = *api;
game->state = game->api.init();
if (game->state == NULL) {
game->state = game->api.init();
}
game->api.reload(game->state);
} else {
printf("Failed to load API\n");
dlclose(game->handle);
@ -71,27 +80,39 @@ int main(void) {
struct Game game = {0};
load_game(&game);
while (!WindowShouldClose()) {
int should_exit = 0;
while (should_exit != 1) {
int nfds = epoll_wait(epollfd, events, MAX_EVENTS, 0);
if (nfds == -1) {
fprintf(stderr, "epoll_wait failed\n");
break;
}
// printf("%d %d\n", nfds, fd);
for (int n = 0; n < nfds; ++n) {
// printf("DO I EVER GET CALLED\n", events[n].data.fd, fd);
printf("DO I EVER GET CALLED\n");
if (events[n].data.fd == fd) {
printf("SO has been updated!\n");
// This clears the inotify queue so we don't get any more events
read(fd, epoll_buf, sizeof(epoll_buf));
load_game(&game);
}
}
game.api.step(game.state);
should_exit = game.api.step(game.state);
if (should_exit == 2) {
printf("I should exit?\n");
load_game(&game);
should_exit = 0;
usleep(1000);
}
}
game.api.finalize(game.state);
free(game.state);
inotify_rm_watch(fd, wd);
close(fd);
close(epollfd);
CloseWindow();
// CloseWindow();
}

View File

@ -1,13 +1,13 @@
* Bexorg Hours
#+BEGIN: clocktable :scope subtree :maxlevel 2
#+CAPTION: Clock summary at [2024-01-09 Tue 20:31]
#+CAPTION: Clock summary at [2024-01-10 Wed 08:45]
| Headline | Time | |
|----------------------+------+------|
| *Total time* | *3:41* | |
| *Total time* | *5:03* | |
|----------------------+------+------|
| Bexorg Hours | 3:41 | |
| Bexorg Hours | 5:03 | |
| \_ <2024-01-06 Sat> | | 2:10 |
| \_ <2024-01-09 Tue> | | 1:31 |
| \_ <2024-01-09 Tue> | | 2:53 |
#+END:
** <2024-01-06 Sat>
@ -16,8 +16,11 @@ CLOCK: [2024-01-06 Sat 11:00]--[2024-01-06 Sat 13:10] => 2:10
:END:
** <2024-01-09 Tue>
:LOGBOOK:
CLOCK: [2024-01-09 Tue 21:28]
CLOCK: [2024-01-09 Tue 21:28]--[2024-01-09 Tue 22:50] => 1:22
CLOCK: [2024-01-09 Tue 19:30]--[2024-01-09 Tue 20:31] => 1:01
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 08:10]
:END:

1
lib/libraylib.so Symbolic link
View File

@ -0,0 +1 @@
/home/joe/Development/tinyswords/lib/libraylib.so.500

BIN
lib/libraylib.so.5.0.0 Executable file

Binary file not shown.

1
lib/libraylib.so.500 Symbolic link
View File

@ -0,0 +1 @@
/home/joe/Development/tinyswords/lib/libraylib.so.5.0.0