ilimination of system code, removal of x-watcher and replacement of it by dmon, fixes in audio code, dynamic asset reload
This commit is contained in:
98
src/game_object/twn_dynamic_game_object.c
Normal file
98
src/game_object/twn_dynamic_game_object.c
Normal file
@ -0,0 +1,98 @@
|
||||
#include "twn_game_object_c.h"
|
||||
#include "twn_engine_context_c.h"
|
||||
#include "twn_filewatch_c.h"
|
||||
#include "twn_util_c.h"
|
||||
#include "twn_util.h"
|
||||
#include "twn_loop_c.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define GAME_OBJECT_NAME "libgame.dll"
|
||||
#else
|
||||
#define GAME_OBJECT_NAME "libgame.so"
|
||||
#endif
|
||||
|
||||
static void (*game_tick_callback)(void);
|
||||
static void (*game_end_callback)(void);
|
||||
|
||||
static void *handle = NULL;
|
||||
|
||||
|
||||
static void game_object_file_action(char const *path, enum FilewatchAction action) {
|
||||
(void)action;
|
||||
|
||||
if (action == FILEWATCH_ACTION_FILE_DELETED)
|
||||
return;
|
||||
|
||||
if (handle) {
|
||||
SDL_UnloadObject(handle);
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
handle = NULL;
|
||||
}
|
||||
|
||||
handle = SDL_LoadObject(path);
|
||||
if (!handle) {
|
||||
CRY_SDL("Hot Reload Error: Cannot open game code shared object");
|
||||
return;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
|
||||
game_tick_callback = (void (*)(void))SDL_LoadFunction(handle, "game_tick");
|
||||
if (!game_tick_callback) {
|
||||
CRY("Hot Reload Error", "game_tick_callback() symbol wasn't found");
|
||||
goto ERR_GETTING_PROC;
|
||||
}
|
||||
|
||||
game_end_callback = (void (*)(void))SDL_LoadFunction(handle, "game_end");
|
||||
if (!game_end_callback) {
|
||||
CRY("Hot Reload Error", "game_end_callback() symbol wasn't found");
|
||||
goto ERR_GETTING_PROC;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
if (fabsf(0.0f - ctx.game.frame_number) > 0.00001f) {
|
||||
log_info("Game object was reloaded\n");
|
||||
reset_state();
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
ERR_GETTING_PROC:
|
||||
SDL_UnloadObject(handle);
|
||||
handle = NULL;
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
}
|
||||
|
||||
|
||||
void game_object_load(void) {
|
||||
static bool filewatch_attached;
|
||||
if (!filewatch_attached) {
|
||||
char *game_object_path;
|
||||
SDL_asprintf(&game_object_path, "%s%s", ctx.base_dir, GAME_OBJECT_NAME);
|
||||
filewatch_add_file(game_object_path, game_object_file_action);
|
||||
game_object_file_action(game_object_path, FILEWATCH_ACTION_FILE_MODIFIED);
|
||||
SDL_free(game_object_path);
|
||||
filewatch_attached = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void game_object_unload(void) {
|
||||
game_end_callback();
|
||||
/* needs to be closed otherwise symbols aren't resolved again */
|
||||
SDL_UnloadObject(handle);
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
handle = NULL;
|
||||
}
|
||||
|
||||
|
||||
void game_object_tick(void) {
|
||||
game_tick_callback();
|
||||
}
|
@ -1,161 +0,0 @@
|
||||
#include "twn_game_object_c.h"
|
||||
#include "twn_engine_context_c.h"
|
||||
#include "twn_util_c.h"
|
||||
#include "twn_util.h"
|
||||
|
||||
#include <x-watcher.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
|
||||
#define GAME_OBJECT_NAME "libgame.so"
|
||||
#define MODIFIED_TICKS_MERGED 10
|
||||
|
||||
|
||||
static void (*game_tick_callback)(void);
|
||||
static void (*game_end_callback)(void);
|
||||
|
||||
static x_watcher *watcher;
|
||||
static void *handle = NULL;
|
||||
|
||||
static float last_tick_modified;
|
||||
static bool loaded_after_modification = true;
|
||||
static SDL_mutex *lock;
|
||||
|
||||
static char *game_object_path;
|
||||
|
||||
static void load_game_object(void) {
|
||||
/* needs to be closed otherwise symbols aren't resolved again */
|
||||
if (handle) {
|
||||
dlclose(handle);
|
||||
handle = NULL;
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
}
|
||||
|
||||
void *new_handle = dlopen(game_object_path, RTLD_LAZY);
|
||||
if (!new_handle) {
|
||||
log_critical("Hot Reload Error: Cannot open game code shared object (%s)", dlerror());
|
||||
goto ERR_OPENING_SO;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
|
||||
game_tick_callback = (void (*)(void))dlsym(new_handle, "game_tick");
|
||||
if (!game_tick_callback) {
|
||||
CRY("Hot Reload Error", "game_tick_callback() symbol wasn't found");
|
||||
goto ERR_GETTING_PROC;
|
||||
}
|
||||
|
||||
game_end_callback = (void (*)(void))dlsym(new_handle, "game_end");
|
||||
if (!game_end_callback) {
|
||||
CRY("Hot Reload Error", "game_end_callback() symbol wasn't found");
|
||||
goto ERR_GETTING_PROC;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
handle = new_handle;
|
||||
|
||||
if (fabsf(0.0f - ctx.game.frame_number) > 0.00001f)
|
||||
log_info("Game object was reloaded\n");
|
||||
|
||||
return;
|
||||
|
||||
ERR_GETTING_PROC:
|
||||
dlclose(new_handle);
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
|
||||
ERR_OPENING_SO:
|
||||
SDL_UnlockMutex(lock);
|
||||
|
||||
}
|
||||
|
||||
static void watcher_callback(XWATCHER_FILE_EVENT event,
|
||||
const char *path,
|
||||
int context,
|
||||
void *data)
|
||||
{
|
||||
(void)context;
|
||||
(void)path;
|
||||
(void)data;
|
||||
|
||||
switch(event) {
|
||||
case XWATCHER_FILE_CREATED:
|
||||
case XWATCHER_FILE_MODIFIED:
|
||||
SDL_LockMutex(lock);
|
||||
last_tick_modified = ctx.game.frame_number;
|
||||
loaded_after_modification = false;
|
||||
SDL_UnlockMutex(lock);
|
||||
break;
|
||||
|
||||
case XWATCHER_FILE_UNSPECIFIED:
|
||||
case XWATCHER_FILE_REMOVED:
|
||||
case XWATCHER_FILE_OPENED:
|
||||
case XWATCHER_FILE_ATTRIBUTES_CHANGED:
|
||||
case XWATCHER_FILE_NONE:
|
||||
case XWATCHER_FILE_RENAMED:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void game_object_load(void) {
|
||||
SDL_asprintf(&game_object_path, "%s%s", ctx.base_dir, GAME_OBJECT_NAME);
|
||||
|
||||
watcher = xWatcher_create();
|
||||
|
||||
xWatcher_reference dir;
|
||||
dir.path = game_object_path;
|
||||
dir.callback_func = watcher_callback;
|
||||
|
||||
xWatcher_appendFile(watcher, &dir);
|
||||
xWatcher_start(watcher);
|
||||
|
||||
lock = SDL_CreateMutex();
|
||||
load_game_object();
|
||||
}
|
||||
|
||||
|
||||
void game_object_unload(void) {
|
||||
game_end_callback();
|
||||
xWatcher_destroy(watcher);
|
||||
watcher = NULL;
|
||||
dlclose(handle);
|
||||
handle = NULL;
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
SDL_DestroyMutex(lock);
|
||||
}
|
||||
|
||||
|
||||
bool game_object_try_reloading(void) {
|
||||
bool result = false;
|
||||
|
||||
/* only load the modified library after some time, as compilers make a lot of modifications */
|
||||
SDL_LockMutex(lock);
|
||||
if (ctx.game.frame_number - last_tick_modified > MODIFIED_TICKS_MERGED &&
|
||||
!loaded_after_modification) {
|
||||
load_game_object();
|
||||
loaded_after_modification = true;
|
||||
result = true;
|
||||
} SDL_UnlockMutex(lock);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void game_object_tick(void) {
|
||||
game_tick_callback();
|
||||
}
|
||||
|
||||
void *game_object_get_game_tick_address(void) {
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
return (void *)&game_tick_callback;
|
||||
#pragma GCC diagnostic pop
|
||||
}
|
@ -5,24 +5,10 @@
|
||||
void game_object_load(void) {
|
||||
}
|
||||
|
||||
|
||||
void game_object_unload(void) {
|
||||
game_end();
|
||||
}
|
||||
|
||||
|
||||
bool game_object_try_reloading(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void game_object_tick(void) {
|
||||
game_tick();
|
||||
}
|
||||
|
||||
void *game_object_get_game_tick_address(void) {
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
return (void *)&game_tick;
|
||||
#pragma GCC diagnostic pop
|
||||
}
|
||||
|
@ -1,90 +0,0 @@
|
||||
#include "twn_game_object_c.h"
|
||||
#include "twn_engine_context_c.h"
|
||||
#include "twn_util_c.h"
|
||||
#include "twn_util.h"
|
||||
|
||||
#include <errhandlingapi.h>
|
||||
#include <libloaderapi.h>
|
||||
|
||||
|
||||
#define GAME_OBJECT_PATH "libgame.dll"
|
||||
|
||||
|
||||
static void (*game_tick_callback)(void);
|
||||
static void (*game_end_callback)(void);
|
||||
|
||||
static void *handle = NULL;
|
||||
|
||||
|
||||
static void load_game_object(void) {
|
||||
/* needs to be closed otherwise symbols aren't resolved again */
|
||||
if (handle) {
|
||||
FreeLibrary(handle);
|
||||
handle = NULL;
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
}
|
||||
|
||||
void *new_handle = LoadLibraryA(GAME_OBJECT_PATH);
|
||||
if (!new_handle) {
|
||||
log_critical("Hot Reload Error: Cannot open game code shared object (%s)", GetLastError());
|
||||
goto ERR_OPENING_SO;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
|
||||
game_tick_callback = (void (*)(void))GetProcAddress(new_handle, "game_tick");
|
||||
if (!game_tick_callback) {
|
||||
CRY("Hot Reload Error", "game_tick_callback() symbol wasn't found");
|
||||
goto ERR_GETTING_PROC;
|
||||
}
|
||||
|
||||
game_end_callback = (void (*)(void))GetProcAddress(new_handle, "game_end");
|
||||
if (!game_end_callback) {
|
||||
CRY("Hot Reload Error", "game_end_callback() symbol wasn't found");
|
||||
goto ERR_GETTING_PROC;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
handle = new_handle;
|
||||
|
||||
if (ctx.game.frame_number != 0)
|
||||
log_info("Game object was reloaded\n");
|
||||
|
||||
return;
|
||||
|
||||
ERR_GETTING_PROC:
|
||||
FreeLibrary(new_handle);
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
|
||||
ERR_OPENING_SO:
|
||||
die_abruptly();
|
||||
}
|
||||
|
||||
|
||||
void game_object_load(void) {
|
||||
load_game_object();
|
||||
}
|
||||
|
||||
|
||||
void game_object_unload(void) {
|
||||
game_end_callback();
|
||||
FreeLibrary(handle);
|
||||
handle = NULL;
|
||||
game_tick_callback = NULL;
|
||||
game_end_callback = NULL;
|
||||
}
|
||||
|
||||
|
||||
/* doesn't support reloading because of problems with file watching */
|
||||
bool game_object_try_reloading(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void game_object_tick(void) {
|
||||
game_tick_callback();
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
#include "../twn_timer.h"
|
||||
#include "twn_engine_context_c.h"
|
||||
|
||||
#include <SDL_messagebox.h>
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
static timer_t timerid;
|
||||
static struct sigaction sa;
|
||||
static struct sigevent sev;
|
||||
|
||||
static bool created;
|
||||
static uint64_t used_milliseconds_to_expire;
|
||||
|
||||
#define SANITY_TIMER_MESSAGE_FMT "Game tick exeeded its allocated time (%lu milliseconds), application is closing"
|
||||
|
||||
/* stop application */
|
||||
static void sanity_timer_handler(int sig, siginfo_t *si, void *uc) {
|
||||
(void)uc; (void)sig; (void)si;
|
||||
size_t text_str_len = snprintf(NULL, 0, SANITY_TIMER_MESSAGE_FMT, used_milliseconds_to_expire) + 1;
|
||||
char *text_str = SDL_malloc(text_str_len);
|
||||
snprintf(text_str, text_str_len, SANITY_TIMER_MESSAGE_FMT, used_milliseconds_to_expire);
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "sanity timer", text_str, ctx.window);
|
||||
SDL_free(text_str);
|
||||
raise(SIGKILL);
|
||||
}
|
||||
|
||||
|
||||
bool start_sanity_timer(uint64_t milliseconds_to_expire) {
|
||||
if (!created) {
|
||||
sa.sa_flags = SA_SIGINFO;
|
||||
sa.sa_sigaction = sanity_timer_handler;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
if (sigaction(SIGRTMIN, &sa, NULL) == -1)
|
||||
goto ERR_SIGACTION;
|
||||
|
||||
sev.sigev_notify = SIGEV_SIGNAL;
|
||||
sev.sigev_signo = SIGRTMIN;
|
||||
sev.sigev_value.sival_ptr = &timerid;
|
||||
if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == -1)
|
||||
goto ERR_TIMERCREATE;
|
||||
|
||||
created = true;
|
||||
|
||||
ERR_TIMERCREATE:
|
||||
// ERR_SIGPROCMASK:
|
||||
ERR_SIGACTION:
|
||||
return false;
|
||||
}
|
||||
|
||||
struct itimerspec its = {0};
|
||||
its.it_value.tv_sec = milliseconds_to_expire / 1000;
|
||||
its.it_value.tv_nsec = (milliseconds_to_expire * 1000000 % 1000000000);
|
||||
if (timer_settime(timerid, 0, &its, NULL) == -1)
|
||||
return false;
|
||||
|
||||
used_milliseconds_to_expire = milliseconds_to_expire;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool end_sanity_timer(void) {
|
||||
struct itimerspec its = {0};
|
||||
its.it_value.tv_sec = 0;
|
||||
its.it_value.tv_nsec = 0;
|
||||
if (timer_settime(timerid, 0, &its, NULL) == -1)
|
||||
return false;
|
||||
|
||||
return false;
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
#include "twn_textures.c"
|
||||
#include "twn_util.c"
|
||||
#include "twn_filewatch.c"
|
||||
#include "twn_timer.c"
|
||||
|
||||
#include "rendering/twn_circles.c"
|
||||
#include "rendering/twn_draw.c"
|
||||
|
@ -29,6 +29,7 @@ static const uint8_t audio_exts_len[AUDIO_FILE_TYPE_COUNT] = {
|
||||
};
|
||||
|
||||
/* TODO: allow for vectorization and packed vectors (alignment care and alike) */
|
||||
/* TODO: free channel name duplicates */
|
||||
|
||||
/* TODO: count frames without use, free the memory when threshold is met */
|
||||
/* TODO: count repeated usages for sound effect cases with rendering to ram? */
|
||||
@ -212,6 +213,7 @@ static void free_audio_channel(AudioChannel channel) {
|
||||
SDL_assert_always(false);
|
||||
break;
|
||||
}
|
||||
SDL_free(channel.path);
|
||||
}
|
||||
|
||||
|
||||
@ -283,32 +285,42 @@ void audio_play(const char *path,
|
||||
/* create a channel if it doesn't exist */
|
||||
if (!pair) {
|
||||
AudioFileType const file_type = infer_audio_file_type(path);
|
||||
AudioChannel new_channel = {
|
||||
AudioChannel const new_channel = {
|
||||
.file_type = file_type,
|
||||
.context = init_audio_context(path, file_type),
|
||||
.path = path,
|
||||
.name = channel,
|
||||
.path = SDL_strdup(path),
|
||||
.repeat = repeat,
|
||||
.volume = volume,
|
||||
.panning = panning,
|
||||
};
|
||||
shput(ctx.audio_channels, channel, new_channel);
|
||||
shput(ctx.audio_channels, SDL_strdup(channel), new_channel);
|
||||
pair = shgetp_null(ctx.audio_channels, channel);
|
||||
}
|
||||
|
||||
/* TODO: destroy and create new context when channel is reused for different file */
|
||||
|
||||
/* works for both restarts and new audio */
|
||||
if (strcmp(pair->value.path, path) == 0)
|
||||
/* repeat if so */
|
||||
else if (strcmp(pair->value.path, path) == 0)
|
||||
repeat_audio(&pair->value);
|
||||
else {
|
||||
/* start anew, reuse channel name */
|
||||
free_audio_channel(pair->value);
|
||||
AudioFileType const file_type = infer_audio_file_type(path);
|
||||
AudioChannel const new_channel = {
|
||||
.file_type = file_type,
|
||||
.context = init_audio_context(path, file_type),
|
||||
.path = SDL_strdup(path),
|
||||
.repeat = repeat,
|
||||
.volume = volume,
|
||||
.panning = panning,
|
||||
};
|
||||
pair->value = new_channel;
|
||||
}
|
||||
} else {
|
||||
/* audio without channel plays without repeat and ability to change parameters over its course, nor stop it */
|
||||
AudioFileType const file_type = infer_audio_file_type(path);
|
||||
AudioChannel new_channel = {
|
||||
.file_type = file_type,
|
||||
.context = init_audio_context(path, file_type),
|
||||
.path = path,
|
||||
.name = NULL,
|
||||
.path = SDL_strdup(path),
|
||||
.repeat = false,
|
||||
.volume = volume,
|
||||
.panning = panning,
|
||||
@ -506,12 +518,12 @@ static void audio_sample_and_mixin_channel(AudioChannel *channel,
|
||||
}
|
||||
|
||||
|
||||
static void sanity_check_channel(const AudioChannel *channel) {
|
||||
if (channel->volume < 0.0f || channel->volume > 1.0f)
|
||||
log_warn("Volume argument is out of range for channel (%s)", channel->name);
|
||||
static void sanity_check_channel(const AudioChannelItem channel) {
|
||||
if (channel.value.volume < 0.0f || channel.value.volume > 1.0f)
|
||||
log_warn("Volume argument is out of range for channel (%s)", channel.key ? channel.key : "unnamed");
|
||||
|
||||
if (channel->panning < -1.0f || channel->panning > 1.0f)
|
||||
log_warn("Panning argument is out of range for channel (%s)", channel->name);
|
||||
if (channel.value.panning < -1.0f || channel.value.panning > 1.0f)
|
||||
log_warn("Panning argument is out of range for channel (%s)", channel.key ? channel.key : "unnamed");
|
||||
}
|
||||
|
||||
|
||||
@ -523,7 +535,7 @@ void audio_callback(void *userdata, uint8_t *stream, int len) {
|
||||
|
||||
size_t const audio_channels_len = shlen(ctx.audio_channels);
|
||||
for (size_t i = 0; i < audio_channels_len; ++i) {
|
||||
sanity_check_channel(&ctx.audio_channels[i].value);
|
||||
sanity_check_channel(ctx.audio_channels[i]);
|
||||
audio_sample_and_mixin_channel(&ctx.audio_channels[i].value,
|
||||
stream, len,
|
||||
i == audio_channels_len - 1);
|
||||
@ -531,7 +543,7 @@ void audio_callback(void *userdata, uint8_t *stream, int len) {
|
||||
|
||||
size_t const unnamed_audio_channels_len = shlen(ctx.unnamed_audio_channels);
|
||||
for (size_t i = 0; i < unnamed_audio_channels_len; ++i) {
|
||||
sanity_check_channel(&ctx.unnamed_audio_channels[i]);
|
||||
sanity_check_channel((AudioChannelItem){NULL, ctx.unnamed_audio_channels[i]});
|
||||
audio_sample_and_mixin_channel(&ctx.unnamed_audio_channels[i],
|
||||
stream, len,
|
||||
i == unnamed_audio_channels_len - 1);
|
||||
|
@ -48,8 +48,7 @@ union AudioContext {
|
||||
typedef struct AudioChannel {
|
||||
AudioFileType file_type;
|
||||
union AudioContext context; /* interpreted by `file_type` value */
|
||||
const char *path;
|
||||
const char *name;
|
||||
char *path;
|
||||
bool repeat;
|
||||
float volume;
|
||||
float panning;
|
||||
|
@ -2,169 +2,111 @@
|
||||
#include "twn_util.h"
|
||||
#include "twn_engine_context_c.h"
|
||||
|
||||
#include <x-watcher.h>
|
||||
#define DMON_IMPL
|
||||
#include <dmon.h>
|
||||
#include <stb_ds.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
|
||||
struct FilewatchPathToCallback {
|
||||
struct FilewatchEntry {
|
||||
char *path;
|
||||
FileatchCallback callback;
|
||||
float tick_last_reported;
|
||||
enum FilewatchAction action_pending : 3;
|
||||
bool action_processed : 1;
|
||||
enum FilewatchAction *actions_pending;
|
||||
};
|
||||
|
||||
static struct FilewatchPathToCallback *filewatch_directories;
|
||||
static struct FilewatchPathToCallback *filewatch_files;
|
||||
static struct FilewatchEntry *filewatch_directories;
|
||||
static struct FilewatchEntry *filewatch_files;
|
||||
/* note: it gets rebuilt on every addition, as api is such */
|
||||
/* TODO: test whether it's possible to miss on update while watcher is being rebuilt */
|
||||
/* we might have to enumerate things, just to make sure */
|
||||
static x_watcher *filewatcher;
|
||||
static SDL_mutex *filewatcher_lock;
|
||||
static bool filewatcher_initialized;
|
||||
|
||||
/* TODO: check whether path and p->path match? */
|
||||
static void filewatch_dispatch(XWATCHER_FILE_EVENT event,
|
||||
const char *path,
|
||||
int context,
|
||||
void *additional_data
|
||||
) {
|
||||
(void)additional_data; (void)path;
|
||||
static void filewatch_callback(dmon_watch_id watch_id,
|
||||
dmon_action action,
|
||||
const char* rootdir,
|
||||
const char* filepath,
|
||||
const char* oldfilepath,
|
||||
void* user)
|
||||
{
|
||||
enum FilewatchAction faction;
|
||||
|
||||
enum FilewatchAction action;
|
||||
|
||||
switch (event) {
|
||||
case XWATCHER_FILE_CREATED:
|
||||
action = FILEWATCH_ACTION_FILE_CREATED;
|
||||
switch (action) {
|
||||
case DMON_ACTION_CREATE:
|
||||
faction = FILEWATCH_ACTION_FILE_CREATED;
|
||||
break;
|
||||
case XWATCHER_FILE_REMOVED:
|
||||
action = FILEWATCH_ACTION_FILE_DELETED;
|
||||
case DMON_ACTION_DELETE:
|
||||
faction = FILEWATCH_ACTION_FILE_DELETED;
|
||||
break;
|
||||
case XWATCHER_FILE_MODIFIED:
|
||||
action = FILEWATCH_ACTION_FILE_MODIFIED;
|
||||
case DMON_ACTION_MODIFY:
|
||||
faction = FILEWATCH_ACTION_FILE_MODIFIED;
|
||||
break;
|
||||
case XWATCHER_FILE_NONE:
|
||||
case XWATCHER_FILE_OPENED:
|
||||
case XWATCHER_FILE_RENAMED:
|
||||
case XWATCHER_FILE_UNSPECIFIED:
|
||||
case XWATCHER_FILE_ATTRIBUTES_CHANGED:
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_LockMutex(filewatcher_lock);
|
||||
|
||||
struct FilewatchPathToCallback *p;
|
||||
intptr_t const context = (intptr_t)user;
|
||||
struct FilewatchEntry *p;
|
||||
|
||||
if (context < 0)
|
||||
p = &filewatch_files[-context - 1];
|
||||
else
|
||||
p = &filewatch_directories[context];
|
||||
|
||||
p->tick_last_reported = ctx.game.frame_number;
|
||||
p->action_processed = false;
|
||||
p->action_pending = action;
|
||||
|
||||
arrpush(p->actions_pending, faction);
|
||||
SDL_UnlockMutex(filewatcher_lock);
|
||||
}
|
||||
|
||||
|
||||
static bool filewatcher_rebuild(void) {
|
||||
if (filewatcher)
|
||||
xWatcher_destroy(filewatcher);
|
||||
|
||||
filewatcher = xWatcher_create();
|
||||
if (!filewatcher) {
|
||||
log_warn("Error creating xWatcher instance, no file watching is done");
|
||||
return false;
|
||||
}
|
||||
|
||||
int const filewatch_directories_len = arrlen(filewatch_directories);
|
||||
for (int i = 0; i < filewatch_directories_len; ++i) {
|
||||
xWatcher_reference ref = {
|
||||
.path = filewatch_directories[i].path,
|
||||
.callback_func = filewatch_dispatch,
|
||||
.context = i,
|
||||
};
|
||||
if (!xWatcher_appendDir(filewatcher, &ref))
|
||||
log_warn("Error watching dir contents: %s", filewatch_directories[i].path);
|
||||
}
|
||||
|
||||
int const filewatch_files_len = arrlen(filewatch_files);
|
||||
for (int i = 0; i < filewatch_files_len; ++i) {
|
||||
xWatcher_reference ref = {
|
||||
.path = filewatch_files[i].path,
|
||||
.callback_func = filewatch_dispatch,
|
||||
.context = -(i + 1), /* in negative range to allow inrefence */
|
||||
};
|
||||
if (!xWatcher_appendDir(filewatcher, &ref))
|
||||
log_warn("Error watching file: %s", filewatch_files[i].path);
|
||||
}
|
||||
|
||||
if (!xWatcher_start(filewatcher)) {
|
||||
log_warn("Error creating xWatcher instance, no file watching is done");
|
||||
xWatcher_destroy(filewatcher);
|
||||
filewatcher = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool filewatch_add_directory(char *dir, FileatchCallback callback) {
|
||||
bool filewatch_add_directory(char const *dir, FileatchCallback callback) {
|
||||
SDL_assert(dir && callback);
|
||||
|
||||
struct FilewatchPathToCallback const w = {
|
||||
if (!filewatcher_initialized) {
|
||||
dmon_init();
|
||||
filewatcher_initialized = true;
|
||||
}
|
||||
|
||||
struct FilewatchEntry const w = {
|
||||
.callback = callback,
|
||||
.path = SDL_strdup(dir),
|
||||
.action_processed = true,
|
||||
.tick_last_reported = ctx.game.frame_number,
|
||||
.path = SDL_strdup(dir), /* TODO: free */
|
||||
};
|
||||
arrpush(filewatch_directories, w);
|
||||
|
||||
return filewatcher_rebuild();
|
||||
dmon_watch(dir, filewatch_callback, DMON_WATCHFLAGS_RECURSIVE, (void *)(intptr_t)(arrlen(filewatch_directories) - 1));
|
||||
}
|
||||
|
||||
|
||||
bool filewatch_add_file(char *filepath, FileatchCallback callback) {
|
||||
bool filewatch_add_file(char const *filepath, FileatchCallback callback) {
|
||||
SDL_assert(filepath && callback);
|
||||
|
||||
struct FilewatchPathToCallback const f = {
|
||||
if (!filewatcher_initialized) {
|
||||
dmon_init();
|
||||
filewatcher_initialized = true;
|
||||
}
|
||||
|
||||
struct FilewatchEntry const f = {
|
||||
.callback = callback,
|
||||
.path = SDL_strdup(filepath),
|
||||
.action_processed = true,
|
||||
.tick_last_reported = ctx.game.frame_number,
|
||||
.path = SDL_strdup(filepath), /* TODO: free */
|
||||
};
|
||||
arrpush(filewatch_files, f);
|
||||
|
||||
return filewatcher_rebuild();
|
||||
dmon_watch("./", filewatch_callback, 0, (void *)(intptr_t)(-arrlen(filewatch_files)));
|
||||
}
|
||||
|
||||
|
||||
void filewatch_poll(void) {
|
||||
SDL_LockMutex(filewatcher_lock);
|
||||
|
||||
int const filewatch_directories_len = arrlen(filewatch_directories);
|
||||
for (int i = 0; i < filewatch_directories_len; ++i) {
|
||||
if (!filewatch_directories[i].action_processed
|
||||
&& (ctx.game.frame_number - filewatch_directories[i].tick_last_reported > FILEWATCH_MODIFIED_TICKS_MERGED)) {
|
||||
SDL_assert(filewatch_directories[i].action_pending != FILEWATCH_ACTION_FILE_NONE);
|
||||
filewatch_directories[i].callback(filewatch_directories[i].path, filewatch_directories[i].action_pending);
|
||||
filewatch_directories[i].action_pending = FILEWATCH_ACTION_FILE_NONE;
|
||||
filewatch_directories[i].action_processed = true;
|
||||
}
|
||||
for (int i = 0; i < arrlen(filewatch_directories); ++i) {
|
||||
for (int u = 0; u < arrlen(filewatch_directories[i].actions_pending); ++u)
|
||||
filewatch_directories[i].callback(filewatch_directories[i].path, filewatch_directories[i].actions_pending[u]);
|
||||
arrfree(filewatch_directories[i].actions_pending);
|
||||
}
|
||||
|
||||
int const filewatch_files_len = arrlen(filewatch_files);
|
||||
for (int i = 0; i < filewatch_files_len; ++i) {
|
||||
if (!filewatch_files[i].action_processed
|
||||
&& (ctx.game.frame_number - filewatch_files[i].tick_last_reported > FILEWATCH_MODIFIED_TICKS_MERGED)) {
|
||||
SDL_assert(filewatch_files[i].action_pending != FILEWATCH_ACTION_FILE_NONE);
|
||||
filewatch_files[i].callback(filewatch_files[i].path, filewatch_files[i].action_pending);
|
||||
filewatch_files[i].action_pending = FILEWATCH_ACTION_FILE_NONE;
|
||||
filewatch_files[i].action_processed = true;
|
||||
}
|
||||
for (int i = 0; i < arrlen(filewatch_files); ++i) {
|
||||
for (int u = 0; u < arrlen(filewatch_files[i].actions_pending); ++u)
|
||||
filewatch_files[i].callback(filewatch_files[i].path, filewatch_files[i].actions_pending[u]);
|
||||
arrfree(filewatch_files[i].actions_pending);
|
||||
}
|
||||
|
||||
SDL_UnlockMutex(filewatcher_lock);
|
||||
|
@ -3,8 +3,6 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#define FILEWATCH_MODIFIED_TICKS_MERGED 10
|
||||
|
||||
enum FilewatchAction {
|
||||
FILEWATCH_ACTION_FILE_NONE,
|
||||
FILEWATCH_ACTION_FILE_CREATED,
|
||||
@ -14,9 +12,9 @@ enum FilewatchAction {
|
||||
|
||||
typedef void (*FileatchCallback)(char const *path, enum FilewatchAction action);
|
||||
|
||||
bool filewatch_add_directory(char *dir, FileatchCallback callback);
|
||||
bool filewatch_add_directory(char const *dir, FileatchCallback callback);
|
||||
|
||||
bool filewatch_add_file(char *file, FileatchCallback callback);
|
||||
bool filewatch_add_file(char const *file, FileatchCallback callback);
|
||||
|
||||
void filewatch_poll(void);
|
||||
|
||||
|
@ -11,13 +11,9 @@
|
||||
|
||||
void game_object_load(void);
|
||||
|
||||
/* note: it should be only called when application is closing */
|
||||
void game_object_unload(void);
|
||||
|
||||
/* returns true if reload happened, otherwise false */
|
||||
bool game_object_try_reloading(void);
|
||||
|
||||
void game_object_tick(void);
|
||||
|
||||
void *game_object_get_game_tick_address(void);
|
||||
|
||||
#endif
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "twn_loop.h"
|
||||
#include "twn_loop_c.h"
|
||||
#include "twn_engine_context_c.h"
|
||||
#include "twn_filewatch_c.h"
|
||||
#include "twn_input_c.h"
|
||||
@ -6,7 +6,7 @@
|
||||
#include "twn_util_c.h"
|
||||
#include "twn_game_object_c.h"
|
||||
#include "twn_textures_c.h"
|
||||
#include "system/twn_timer.h"
|
||||
#include "twn_timer_c.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <physfs.h>
|
||||
@ -747,15 +747,20 @@ static void clean_up(void) {
|
||||
}
|
||||
|
||||
|
||||
static void reset_state(void) {
|
||||
void reset_state(void) {
|
||||
ctx.game.initialization_needed = true;
|
||||
input_reset_state(&ctx.input);
|
||||
textures_reset_state();
|
||||
}
|
||||
|
||||
|
||||
static void pack_contents_modified(char const *path, enum FilewatchAction action) {
|
||||
log_info("Pack contents invalidated: %s, action: %i", path, action);
|
||||
reset_state();
|
||||
static uint32_t last_reset_tick;
|
||||
if (last_reset_tick != (uint32_t)ctx.game.frame_number) {
|
||||
log_info("Pack contents invalidated: %s, action: %i", path, action);
|
||||
reset_state();
|
||||
last_reset_tick = (uint32_t)ctx.game.frame_number;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -868,11 +873,7 @@ int enter_loop(int argc, char **argv) {
|
||||
profile_end("startup");
|
||||
|
||||
while (ctx.is_running) {
|
||||
if (game_object_try_reloading()) {
|
||||
ctx.game.initialization_needed = true;
|
||||
reset_state();
|
||||
}
|
||||
|
||||
/* dispatch all filewatch driven events, such as game object and asset pack reload */
|
||||
filewatch_poll();
|
||||
main_loop();
|
||||
}
|
||||
|
12
src/twn_loop_c.h
Normal file
12
src/twn_loop_c.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef TWN_LOOP_H
|
||||
#define TWN_LOOP_H
|
||||
|
||||
#include "twn_engine_api.h"
|
||||
|
||||
|
||||
TWN_API int enter_loop(int argc, char **argv);
|
||||
|
||||
TWN_API void reset_state(void);
|
||||
|
||||
|
||||
#endif
|
@ -1,4 +1,4 @@
|
||||
#include "twn_loop.h"
|
||||
#include "twn_loop_c.h"
|
||||
|
||||
#ifndef EMSCRIPTEN
|
||||
#define SDL_MAIN_HANDLED
|
||||
|
38
src/twn_timer.c
Normal file
38
src/twn_timer.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include "twn_timer_c.h"
|
||||
#include "twn_engine_context_c.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
static SDL_TimerID sanity_timer;
|
||||
|
||||
#define SANITY_TIMER_MESSAGE_FMT "Game tick exeeded its allocated time (%u milliseconds), application is closing"
|
||||
|
||||
/* stop application */
|
||||
static uint32_t sanity_timer_handler(uint32_t interval, void *data) {
|
||||
(void)data;
|
||||
|
||||
size_t text_str_len = snprintf(NULL, 0, SANITY_TIMER_MESSAGE_FMT, interval) + 1;
|
||||
char *text_str = SDL_malloc(text_str_len);
|
||||
snprintf(text_str, text_str_len, SANITY_TIMER_MESSAGE_FMT, interval);
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "sanity timer", text_str, ctx.window);
|
||||
SDL_free(text_str);
|
||||
|
||||
/* TODO: figure out the most portable way to do it */
|
||||
/* TODO: different type of behavior is possible, especially for debugging */
|
||||
quick_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
bool start_sanity_timer(uint32_t milliseconds_to_expire) {
|
||||
if (!sanity_timer) {
|
||||
sanity_timer = SDL_AddTimer(milliseconds_to_expire, sanity_timer_handler, NULL);
|
||||
}
|
||||
|
||||
return sanity_timer != 0;
|
||||
}
|
||||
|
||||
|
||||
bool end_sanity_timer(void) {
|
||||
SDL_RemoveTimer(sanity_timer);
|
||||
sanity_timer = 0;
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdbit.h>
|
||||
|
||||
bool start_sanity_timer(uint64_t milliseconds_to_expire);
|
||||
bool start_sanity_timer(uint32_t milliseconds_to_expire);
|
||||
bool end_sanity_timer(void);
|
||||
|
||||
#endif // TWN_TIMER_H
|
Reference in New Issue
Block a user