From 5d127b7f2c6882446012cff5027b50fa49bbca27 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 26 Jan 2025 12:46:14 +0100 Subject: [PATCH] hot reload refactor --- build_file.c | 4 +-- src/app/app.h | 7 +---- src/app/app_win32.c | 26 ++++++++++++------- .../win32_exe.c => app/app_win32_exe.c} | 2 ++ src/core/core_ctx.h | 6 +++++ src/{app => render}/glad/glad.c | 0 src/{app => render}/glad/glad.h | 0 src/{app => render}/glad/khrplatform.h | 0 src/render/render_inc.c | 5 ++++ src/render/render_opengl.c | 17 +++++++++--- src/ui/ui.c | 22 +++++++++------- src/wasm_app/main.c | 26 ++++--------------- 12 files changed, 64 insertions(+), 51 deletions(-) rename src/{wasm_app/win32_exe.c => app/app_win32_exe.c} (80%) rename src/{app => render}/glad/glad.c (100%) rename src/{app => render}/glad/glad.h (100%) rename src/{app => render}/glad/khrplatform.h (100%) diff --git a/build_file.c b/build_file.c index 1709b1e..6b68b60 100644 --- a/build_file.c +++ b/build_file.c @@ -56,10 +56,10 @@ int main(int argc, char **argv) { if (ok != 0) return ok; } - if (win32_target && cache_code_modified(s8_lit("../src/wasm_app/win32_exe.c"), s8_lit("app_win32.exe"))) { + if (win32_target && cache_code_modified(s8_lit("../src/app/app_win32_exe.c"), s8_lit("app_win32.exe"))) { // os_delete_file(s8_lit("app_win32.pdb")); ok = os_systemf( - "cl ../src/wasm_app/win32_exe.c -Fe:app_win32.exe -Fd:app_win32.pdb" + "cl ../src/app/app_win32_exe.c -Fe:app_win32.exe -Fd:app_win32.pdb" " -I ../src" " /Zi /FC /nologo /Oi" " /WX /W3 /wd4200 /diagnostics:column" diff --git a/src/app/app.h b/src/app/app.h index 562f567..96ee8a5 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -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) \ No newline at end of file diff --git a/src/app/app_win32.c b/src/app/app_win32.c index fb9dad5..69a9db1 100644 --- a/src/app/app_win32.c +++ b/src/app/app_win32.c @@ -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); diff --git a/src/wasm_app/win32_exe.c b/src/app/app_win32_exe.c similarity index 80% rename from src/wasm_app/win32_exe.c rename to src/app/app_win32_exe.c index c0e320e..ab78be0 100644 --- a/src/wasm_app/win32_exe.c +++ b/src/app/app_win32_exe.c @@ -1,3 +1,5 @@ +#define APP_HOT_RELOAD + #include "core/core_inc.h" #include "app/app.h" diff --git a/src/core/core_ctx.h b/src/core/core_ctx.h index 9b409ad..2d3906f 100644 --- a/src/core/core_ctx.h +++ b/src/core/core_ctx.h @@ -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; diff --git a/src/app/glad/glad.c b/src/render/glad/glad.c similarity index 100% rename from src/app/glad/glad.c rename to src/render/glad/glad.c diff --git a/src/app/glad/glad.h b/src/render/glad/glad.h similarity index 100% rename from src/app/glad/glad.h rename to src/render/glad/glad.h diff --git a/src/app/glad/khrplatform.h b/src/render/glad/khrplatform.h similarity index 100% rename from src/app/glad/khrplatform.h rename to src/render/glad/khrplatform.h diff --git a/src/render/render_inc.c b/src/render/render_inc.c index 3ef0272..1937a64 100644 --- a/src/render/render_inc.c +++ b/src/render/render_inc.c @@ -17,6 +17,11 @@ #define STBTT_memcpy memory_copy #define STBTT_memset memory_set #include "stb_truetype.h" + +#include +#include "glad/glad.h" +#include "glad/glad.c" + #include "render_font.c" #include "render.c" #include "render.gen.c" diff --git a/src/render/render_opengl.c b/src/render/render_opengl.c index 163fbcb..3b246fd 100644 --- a/src/render/render_opengl.c +++ b/src/render/render_opengl.c @@ -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; } diff --git a/src/ui/ui.c b/src/ui/ui.c index 5cefd43..429e320 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -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)); diff --git a/src/wasm_app/main.c b/src/wasm_app/main.c index 53dfdba..1a9440c 100644 --- a/src/wasm_app/main.c +++ b/src/wasm_app/main.c @@ -1,43 +1,28 @@ #include "core/core_inc.h" #include "app/app.h" #include "ui/ui_inc.h" -#include -#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; } -// \ No newline at end of file