Hot reloading: Current configuration seems to hot reload correctly
This commit is contained in:
parent
517df72ec3
commit
93972ba1d4
7
Makefile
7
Makefile
@ -14,15 +14,14 @@ sprites.o: sprites.c sprites.h lib.h
|
|||||||
dod: dod.c ./lib/libraylib.a
|
dod: dod.c ./lib/libraylib.a
|
||||||
$(CC) $(CFLAGS) -Iinclude/ -lm dod.c -o dod ./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
|
boids_main: boids_main.c lib.h libboids.so
|
||||||
$(CC) $(CFLAGS) -Iinclude/ -fPIC -ldl -lm $< -o $@ ./lib/libraylib.a
|
$(CC) $(CFLAGS) -Iinclude/ -ldl -lm -lraylib $< -o $@ -L./lib -Wl,-rpath,./lib/
|
||||||
|
|
||||||
libboids.so: boids_game.c boids_game.h lib.h
|
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
|
run: all
|
||||||
./main
|
./main
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -vf *.so *.o main boids_main dod
|
rm -vf *.so *.o main boids_main dod
|
||||||
|
|
||||||
|
20
boids_game.c
20
boids_game.c
@ -48,30 +48,41 @@ static GameState *init() {
|
|||||||
boid->acceleration = (Vector2){0};
|
boid->acceleration = (Vector2){0};
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("I loaded the contents\n");
|
printf("Initialized Game\n");
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void finalize(GameState *state) {
|
static void finalize(GameState *state) {
|
||||||
(void)state;
|
free(state->boids);
|
||||||
|
CloseWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reload(GameState *state) {
|
static void reload(GameState *state) {
|
||||||
(void)state;
|
(void)state;
|
||||||
|
state->max_speed = 5.0f;
|
||||||
|
state->max_force = 0.1;
|
||||||
|
printf("Reloaded Game\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unload(GameState *state) {
|
static void unload(GameState *state) {
|
||||||
(void)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
|
// Process Input
|
||||||
if (IsMouseButtonPressed(0)) {
|
if (IsMouseButtonPressed(0)) {
|
||||||
Vector2 mouse_pos = GetMousePosition();
|
Vector2 mouse_pos = GetMousePosition();
|
||||||
state->target_pos = (PointOption){.tag = SOME, .some.point = mouse_pos};
|
state->target_pos = (PointOption){.tag = SOME, .some.point = mouse_pos};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
for (int i = 0; i < state->num_boids; i++) {
|
for (int i = 0; i < state->num_boids; i++) {
|
||||||
Boid *boid = &state->boids[i];
|
Boid *boid = &state->boids[i];
|
||||||
@ -105,6 +116,7 @@ static void step(GameState *state) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
|
return return_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct GameApi GAME_API = {
|
const struct GameApi GAME_API = {
|
||||||
|
@ -7,7 +7,7 @@ typedef struct GameApi {
|
|||||||
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);
|
||||||
void (*step) (struct GameState *state);
|
int (*step) (struct GameState *state);
|
||||||
} GameApi;
|
} GameApi;
|
||||||
|
|
||||||
extern const GameApi GAME_API;
|
extern const GameApi GAME_API;
|
||||||
|
43
boids_main.c
43
boids_main.c
@ -14,26 +14,35 @@
|
|||||||
#define SCREEN_HEIGHT 1080
|
#define SCREEN_HEIGHT 1080
|
||||||
#define TARGET_FPS 60
|
#define TARGET_FPS 60
|
||||||
|
|
||||||
const char *GAME_LIB = "./libboids.so";
|
const char* GAME_LIB = "./libboids.so";
|
||||||
char epoll_buf[1024];
|
char epoll_buf[1024];
|
||||||
|
|
||||||
struct Game {
|
struct Game
|
||||||
void *handle;
|
{
|
||||||
|
void* handle;
|
||||||
struct GameApi api;
|
struct GameApi api;
|
||||||
struct GameState *state;
|
struct GameState* state;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define MAX_EVENTS 1
|
#define MAX_EVENTS 1
|
||||||
|
|
||||||
void load_game(struct Game *game) {
|
void load_game(struct Game* game) {
|
||||||
void *handle = dlopen(GAME_LIB, RTLD_NOW);
|
if (game->handle) {
|
||||||
|
game->api.unload(game->state);
|
||||||
|
dlclose(game->handle);
|
||||||
|
}
|
||||||
|
void* handle = dlopen(GAME_LIB, RTLD_NOW);
|
||||||
|
|
||||||
if (handle) {
|
if (handle) {
|
||||||
game->handle = 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) {
|
if (api != NULL) {
|
||||||
printf("Loaded API, calling init()\n");
|
printf("Loaded API, calling init()\n");
|
||||||
game->api = *api;
|
game->api = *api;
|
||||||
|
if (game->state == NULL) {
|
||||||
game->state = game->api.init();
|
game->state = game->api.init();
|
||||||
|
}
|
||||||
|
game->api.reload(game->state);
|
||||||
} else {
|
} else {
|
||||||
printf("Failed to load API\n");
|
printf("Failed to load API\n");
|
||||||
dlclose(game->handle);
|
dlclose(game->handle);
|
||||||
@ -71,27 +80,39 @@ int main(void) {
|
|||||||
|
|
||||||
struct Game game = {0};
|
struct Game game = {0};
|
||||||
load_game(&game);
|
load_game(&game);
|
||||||
|
int should_exit = 0;
|
||||||
while (!WindowShouldClose()) {
|
while (should_exit != 1) {
|
||||||
int nfds = epoll_wait(epollfd, events, MAX_EVENTS, 0);
|
int nfds = epoll_wait(epollfd, events, MAX_EVENTS, 0);
|
||||||
if (nfds == -1) {
|
if (nfds == -1) {
|
||||||
fprintf(stderr, "epoll_wait failed\n");
|
fprintf(stderr, "epoll_wait failed\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// printf("%d %d\n", nfds, fd);
|
||||||
for (int n = 0; n < nfds; ++n) {
|
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) {
|
if (events[n].data.fd == fd) {
|
||||||
printf("SO has been updated!\n");
|
printf("SO has been updated!\n");
|
||||||
// This clears the inotify queue so we don't get any more events
|
// This clears the inotify queue so we don't get any more events
|
||||||
read(fd, epoll_buf, sizeof(epoll_buf));
|
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);
|
inotify_rm_watch(fd, wd);
|
||||||
close(fd);
|
close(fd);
|
||||||
close(epollfd);
|
close(epollfd);
|
||||||
CloseWindow();
|
// CloseWindow();
|
||||||
}
|
}
|
||||||
|
15
hours.org
15
hours.org
@ -1,13 +1,13 @@
|
|||||||
* Bexorg Hours
|
* Bexorg Hours
|
||||||
#+BEGIN: clocktable :scope subtree :maxlevel 2
|
#+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 | |
|
| 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-06 Sat> | | 2:10 |
|
||||||
| \_ <2024-01-09 Tue> | | 1:31 |
|
| \_ <2024-01-09 Tue> | | 2:53 |
|
||||||
#+END:
|
#+END:
|
||||||
|
|
||||||
** <2024-01-06 Sat>
|
** <2024-01-06 Sat>
|
||||||
@ -16,8 +16,11 @@ CLOCK: [2024-01-06 Sat 11:00]--[2024-01-06 Sat 13:10] => 2:10
|
|||||||
:END:
|
:END:
|
||||||
** <2024-01-09 Tue>
|
** <2024-01-09 Tue>
|
||||||
:LOGBOOK:
|
: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 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
|
CLOCK: [2024-01-09 Tue 10:45]--[2024-01-09 Tue 11:15] => 0:30
|
||||||
:END:
|
:END:
|
||||||
|
** <2024-01-10 Wed>
|
||||||
|
:LOGBOOK:
|
||||||
|
CLOCK: [2024-01-10 Wed 08:10]
|
||||||
|
:END:
|
||||||
|
1
lib/libraylib.so
Symbolic link
1
lib/libraylib.so
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
/home/joe/Development/tinyswords/lib/libraylib.so.500
|
BIN
lib/libraylib.so.5.0.0
Executable file
BIN
lib/libraylib.so.5.0.0
Executable file
Binary file not shown.
1
lib/libraylib.so.500
Symbolic link
1
lib/libraylib.so.500
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
/home/joe/Development/tinyswords/lib/libraylib.so.5.0.0
|
Loading…
x
Reference in New Issue
Block a user