diff --git a/Makefile b/Makefile index 24f55e6..f997825 100644 --- a/Makefile +++ b/Makefile @@ -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 - diff --git a/boids_game.c b/boids_game.c index 6c2fc92..1230fd5 100644 --- a/boids_game.c +++ b/boids_game.c @@ -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 = { diff --git a/boids_game.h b/boids_game.h index e894d53..dfef593 100644 --- a/boids_game.h +++ b/boids_game.h @@ -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; diff --git a/boids_main.c b/boids_main.c index 3a6aeb9..55d76fe 100644 --- a/boids_main.c +++ b/boids_main.c @@ -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"); + game->handle = handle; + 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(); } diff --git a/hours.org b/hours.org index 81cb2e6..c5d6fe4 100644 --- a/hours.org +++ b/hours.org @@ -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: diff --git a/lib/libraylib.so b/lib/libraylib.so new file mode 120000 index 0000000..9da6a81 --- /dev/null +++ b/lib/libraylib.so @@ -0,0 +1 @@ +/home/joe/Development/tinyswords/lib/libraylib.so.500 \ No newline at end of file diff --git a/lib/libraylib.so.5.0.0 b/lib/libraylib.so.5.0.0 new file mode 100755 index 0000000..a59509a Binary files /dev/null and b/lib/libraylib.so.5.0.0 differ diff --git a/lib/libraylib.so.500 b/lib/libraylib.so.500 new file mode 120000 index 0000000..7277f39 --- /dev/null +++ b/lib/libraylib.so.500 @@ -0,0 +1 @@ +/home/joe/Development/tinyswords/lib/libraylib.so.5.0.0 \ No newline at end of file