tinyswords/boids_main.c

119 lines
3.0 KiB
C

#define _DEFAULT_SOURCE // usleep()
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
// #include "inotify-syscalls.h"
#include "include/raylib.h"
#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 epoll_buf[1024];
struct Game
{
void* handle;
struct GameApi api;
struct GameState* state;
};
#define MAX_EVENTS 1
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");
if (api != NULL) {
printf("Loaded API, calling init()\n");
game->api = *api;
if (game->state == NULL) {
game->state = game->api.init();
}
game->api.reload(game->state);
} else {
printf("Failed to load API\n");
dlclose(game->handle);
}
} else {
fprintf(stderr, "Error loading %s\n", GAME_LIB);
fprintf(stderr, "Error was: %s\n", dlerror());
exit(1);
}
}
int main(void) {
int fd = inotify_init();
if (fd < 0) {
fprintf(stderr, "Error initializing inotify\n");
}
int wd;
wd = inotify_add_watch(fd, GAME_LIB, IN_MODIFY);
// create epoll instance
int epollfd = epoll_create1(0);
if (epollfd == -1) {
fprintf(stderr, "Error creating epoll instance\n");
}
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = fd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event) == -1) {
fprintf(stderr, "Error adding inotify descriptor to epoll\n");
}
struct epoll_event events[MAX_EVENTS];
struct Game game = {0};
load_game(&game);
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);
}
}
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();
}