improve script hot reload

This commit is contained in:
asrael 2026-01-08 01:19:25 -06:00
parent 01d6e09a91
commit 15041984f1
25 changed files with 1516 additions and 293 deletions

View file

@ -15,7 +15,9 @@
#include "pxl8_log.h"
#include "pxl8_macros.h"
#include "pxl8_repl.h"
#include "pxl8_replay.h"
#include "pxl8_script.h"
#include "pxl8_sfx.h"
#include "pxl8_sys.h"
struct pxl8 {
@ -27,6 +29,15 @@ struct pxl8 {
void* platform_data;
};
#ifndef NDEBUG
static void pxl8_audio_event_callback(u8 event_type, u8 context_id, u8 note, f32 volume, void* userdata) {
pxl8_game* game = (pxl8_game*)userdata;
if (game && game->debug_replay) {
pxl8_replay_write_audio_event(game->debug_replay, game->frame_count, event_type, context_id, note, volume);
}
}
#endif
pxl8* pxl8_create(const pxl8_hal* hal) {
pxl8* sys = (pxl8*)calloc(1, sizeof(pxl8));
if (!sys) return NULL;
@ -218,18 +229,26 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
return PXL8_ERROR_INITIALIZATION_FAILED;
}
game->mixer = pxl8_sfx_mixer_create();
game->mixer = pxl8_sfx_mixer_create(sys->hal);
if (!game->mixer) {
pxl8_error("failed to create audio mixer");
return PXL8_ERROR_INITIALIZATION_FAILED;
}
pxl8_rng_seed(&game->rng, (u32)sys->hal->get_ticks());
#ifndef NDEBUG
game->debug_replay = pxl8_replay_create_buffer(60, 60);
pxl8_sfx_mixer_set_event_callback(game->mixer, pxl8_audio_event_callback, game);
#endif
if (game->repl_mode) {
pxl8_info("starting in REPL mode with script: %s", game->script_path);
}
pxl8_script_set_gfx(game->script, game->gfx);
pxl8_script_set_input(game->script, &game->input);
pxl8_script_set_rng(game->script, &game->rng);
pxl8_script_set_sfx(game->script, game->mixer);
pxl8_script_set_sys(game->script, sys);
@ -271,7 +290,18 @@ pxl8_result pxl8_update(pxl8* sys) {
game->fps_frame_count = 0;
}
pxl8_script_check_reload(game->script);
#ifndef NDEBUG
u32 rng_state_before_reload = game->rng.state;
#endif
bool reloaded = pxl8_script_check_reload(game->script);
#ifndef NDEBUG
if (reloaded) {
game->rng.state = rng_state_before_reload;
pxl8_debug("Hot-reload: restored RNG state 0x%08X", rng_state_before_reload);
}
#else
(void)reloaded;
#endif
if (game->repl_mode && !game->repl_started) {
if (game->script_loaded) {
@ -309,6 +339,7 @@ pxl8_result pxl8_update(pxl8* sys) {
}
pxl8_gfx_update(game->gfx, dt);
pxl8_sfx_mixer_process(game->mixer);
if (game->script_loaded) {
pxl8_script_call_function_f32(game->script, "update", dt);
@ -352,6 +383,18 @@ pxl8_result pxl8_frame(pxl8* sys) {
memset(game->input.mouse_buttons_released, 0, sizeof(game->input.mouse_buttons_released));
game->frame_count++;
#ifndef NDEBUG
if (game->debug_replay) {
if (game->frame_count % 60 == 0) {
pxl8_replay_write_keyframe(game->debug_replay, game->frame_count, game->time, &game->rng, &game->input);
} else {
pxl8_replay_write_input(game->debug_replay, game->frame_count, &game->prev_input, &game->input);
}
game->prev_input = game->input;
}
#endif
game->input.mouse_dx = 0;
game->input.mouse_dy = 0;
game->input.mouse_wheel_x = 0;
@ -371,6 +414,10 @@ void pxl8_quit(pxl8* sys) {
pxl8_cart_unmount(sys->cart);
}
#ifndef NDEBUG
pxl8_replay_destroy(game->debug_replay);
#endif
pxl8_sfx_mixer_destroy(game->mixer);
pxl8_gfx_destroy(game->gfx);
pxl8_script_destroy(game->script);