hot reload refactor

This commit is contained in:
Krzosa Karol
2025-01-26 12:46:14 +01:00
parent f98c8e3dfa
commit 5d127b7f2c
12 changed files with 64 additions and 51 deletions

View File

@@ -1,9 +1,4 @@
#include "app.gen.h"
typedef struct app_ctx_t app_ctx_t;
struct app_ctx_t {
thread_ctx_t *tcx;
void *(*load_opengl_fn)(const char *name);
};
typedef b32 app_update_t(app_ctx_t *app_ctx, app_frame_t *frame);
typedef b32 app_update_t(thread_ctx_t *tcx, app_frame_t *frame);
#define fn_export __declspec(dllexport)

View File

@@ -10,7 +10,8 @@ gb WNDCLASSW w32_wc;
gb HWND w32_window_handle;
gb HDC w32_dc;
gb b32 w32_quit_app;
gb app_ctx_t app_ctx = {0};
fn b32 app_update(thread_ctx_t *tcx, app_frame_t *frame);
fn v2f32_t w32_get_window_size(HWND window) {
RECT window_rect;
@@ -316,7 +317,7 @@ fn void w32_set_event(app_frame_t *frame, app_event_t ev) {
w32_push_event(frame, ev);
}
fn void w32_prepare_for_update_call(w32_library_t *lib, app_frame_t frame) {
fn void w32_try_reloading_library(w32_library_t *lib, app_frame_t frame) {
i64 new_write_time = os_get_file_mod_time(lib->dll_name);
if (new_write_time == -1 || new_write_time == lib->last_write_time) {
goto end_of_lib_load;
@@ -324,7 +325,7 @@ fn void w32_prepare_for_update_call(w32_library_t *lib, app_frame_t frame) {
if (lib->update_fn) {
w32_set_event(&frame, (app_event_t){.kind = app_event_kind_unload});
lib->update_fn(&app_ctx, &frame);
lib->update_fn(tcx, &frame);
lib->update_fn = NULL;
}
@@ -362,7 +363,7 @@ fn void w32_prepare_for_update_call(w32_library_t *lib, app_frame_t frame) {
} else {
w32_set_event(&frame, (app_event_t){.kind = app_event_kind_reload});
}
lib->update_fn(&app_ctx, &frame);
lib->update_fn(tcx, &frame);
end_of_lib_load:;
}
@@ -457,9 +458,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
w32_library_t lib = {0};
lib.dll_name = s8_lit("app.dll");
lib.update_fn_name = s8_lit("app_update");
app_ctx.tcx = tcx;
app_ctx.load_opengl_fn = w32_load_opengl_fn;
tcx->data[tcx_slot_app] = w32_load_opengl_fn;
f64 time_frame_start = w32_seconds_now();
f64 time_delta = 1.0 / refresh_rate;
@@ -495,10 +494,19 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
frame.frame = frame_counter;
b32 animating = true;
w32_prepare_for_update_call(&lib, frame);
#ifdef APP_HOT_RELOAD
w32_try_reloading_library(&lib, frame);
if (lib.update_fn) {
animating = lib.update_fn(&app_ctx, &frame);
animating = lib.update_fn(tcx, &frame);
}
#else
if (frame_counter == 0) {
app_frame_t frame_copy = frame;
w32_set_event(&frame_copy, (app_event_t){.kind = app_event_kind_init});
app_update(tcx, &frame_copy);
}
animating = app_update(tcx, &frame);
#endif
wait_for_events = !animating;
SwapBuffers(w32_dc);

View File

@@ -1,3 +1,5 @@
#define APP_HOT_RELOAD
#include "core/core_inc.h"
#include "app/app.h"

View File

@@ -13,4 +13,10 @@ struct thread_ctx_t {
logger_t log;
};
enum {
tcx_slot_rn,
tcx_slot_ui,
tcx_slot_app,
};
gb_thread thread_ctx_t *tcx;

View File

@@ -17,6 +17,11 @@
#define STBTT_memcpy memory_copy
#define STBTT_memset memory_set
#include "stb_truetype.h"
#include <windows.h>
#include "glad/glad.h"
#include "glad/glad.c"
#include "render_font.c"
#include "render.c"
#include "render.gen.c"

View File

@@ -53,9 +53,18 @@ fn void rn_reload_font(f32 font_size, f32 dpr) {
ma_end_scratch(scratch);
}
fn void rn_reload(void) {
if (!gladLoadGLLoader((GLADloadproc)tcx->data[tcx_slot_app])) {
fatalf("couldn't load opengl!");
}
}
fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) {
tcx->data[0] = ma_push_type(perm, rn_state_t);
rn = tcx->data[0];
tcx->data[tcx_slot_rn] = ma_push_type(perm, rn_state_t);
rn = tcx->data[tcx_slot_rn];
rn_reload();
rn->cap = 1024*256;
rn->vertices = ma_push_array(perm, rn_vertex_t, rn->cap);
@@ -128,8 +137,8 @@ fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) {
rn->shader2d = rn_create_shader(glsl_vshader, glsl_fshader);
}
void rn_begin_frame(rn_state_t *rn_ctx, app_frame_t *frame) {
rn = rn_ctx;
void rn_begin_frame(app_frame_t *frame) {
rn = tcx->data[tcx_slot_rn];
rn->frame = frame;
}

View File

@@ -670,8 +670,8 @@ fn void ui_draw(void) {
ui__draw_box(ui->frame, &ui->root);
}
fn void ui_begin_frame(ui_t *ui_context, app_frame_t *frame) {
ui = ui_context;
fn void ui_begin_frame(app_frame_t *frame) {
ui = tcx->data[tcx_slot_ui];
ui->frame = frame;
}
@@ -896,17 +896,21 @@ fn void ui_serial_type(void *p, type_t *type) {
}
//
fn void ui_demo_init(ma_arena_t *arena) {
tcx->data[1] = ma_push_type(arena, ui_t);
ui = tcx->data[1];
ui->box_arena = ma_push_arena(arena, mib(1));
fn void ui_reload(void) {
ui_init_colors();
}
fn void ui_init(ma_arena_t *arena) {
tcx->data[tcx_slot_ui] = ma_push_type(arena, ui_t);
ui = tcx->data[tcx_slot_ui];
ui->box_arena = ma_push_arena(arena, mib(1));
ui_reload();
}
gb i32 ui_g_panel = 1;
fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_count) {
ui_begin_frame(tcx->data[1], frame);
rn_begin_frame(tcx->data[0], frame);
ui_begin_frame(frame);
rn_begin_frame(frame);
for (app_event_t *ev = frame->first_event; ev; ev = ev->next) {
ui_begin_build(UILOC, ev, window_rect_from_frame(frame));
@@ -917,7 +921,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
ui_set_lop(ui_lop_cut_left)
ui_set_top(top_box) {
ui_radio_button(&ui_g_panel, 1, "1");
ui_radio_button(&ui_g_panel, 2, "2");
ui_radio_button(&ui_g_panel, 2, "4");
}
ui->top->rect = r2f32_shrinks(ui->top->rect, ui_em(1));

View File

@@ -1,43 +1,28 @@
#include "core/core_inc.h"
#include "app/app.h"
#include "ui/ui_inc.h"
#include <windows.h>
#include "app/glad/glad.h"
#include "core/core_inc.c"
// #include "app/app.c"
#include "render/render_inc.c"
#include "ui/ui_inc.c"
#include "wasm_app.gen.c"
#include "app/glad/glad.c"
fn void app_init(f32 dpr) {
}
fn_export b32 app_update(app_ctx_t *app_ctx, app_frame_t *frame) {
tcx = app_ctx->tcx;
fn_export b32 app_update(thread_ctx_t *thread_ctx, app_frame_t *frame) {
tcx = thread_ctx;
if (frame->first_event->kind == app_event_kind_init) {
run_all_tests();
if (!gladLoadGLLoader((GLADloadproc)app_ctx->load_opengl_fn)) {
fatalf("couldn't load opengl!");
}
debugf("%llx", glad_glDrawArrays);
mt_tweak_f32(font_size, 50, 4, 200);
mt_tweak_f32(_font_size, 50, 50, 50);
ma_arena_t *perm = &tcx->perm;
rn_init(perm, font_size, frame->dpr);
ui_demo_init(perm);
ui_init(perm);
return true;
} else if (frame->first_event->kind == app_event_kind_reload) {
if (!gladLoadGLLoader((GLADloadproc)app_ctx->load_opengl_fn)) {
fatalf("couldn't load opengl!");
}
debugf("%llx", glad_glDrawArrays);
ui_init_colors();
ui_reload();
rn_reload();
return true;
} else if (frame->first_event->kind == app_event_kind_unload) {
@@ -51,4 +36,3 @@ fn_export b32 app_update(app_ctx_t *app_ctx, app_frame_t *frame) {
ui_demo_update(frame, tweak_table, lengthof(tweak_table));
return true;
}
//