From f98c8e3dfabcb8b55424c16d123b81e0032d9e83 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 26 Jan 2025 11:54:31 +0100 Subject: [PATCH] hot reload working --- build_file.c | 26 +++++-- src/app/app.c | 5 -- src/app/app.gen.c | 5 +- src/app/app.gen.h | 3 + src/app/app.h | 8 +- src/app/app.meta.c | 3 + src/app/app_wasm.c | 6 +- src/app/app_win32.c | 131 ++++++++++++++++++++++++++++---- src/core/core_arena.c | 6 +- src/core/core_ctx.c | 15 ++++ src/core/core_ctx.h | 13 +--- src/core/core_inc.c | 3 +- src/core/core_log.c | 6 +- src/core/core_platform_unix.c | 2 +- src/core/core_platform_wasm.c | 2 +- src/core/core_platform_win32.c | 4 +- src/core/core_test_entry.c | 1 + src/meta/build_tool.c | 2 +- src/render/render.c | 26 +++---- src/render/render.h | 2 +- src/render/render_inc.c | 2 +- src/render/render_opengl.c | 80 ++++++++++--------- src/render/render_wasm_canvas.c | 14 ++-- src/ui/ui.c | 78 ++++++++++--------- src/ui/ui.gen.c | 85 ++++++++++++--------- src/ui/ui.gen.h | 19 +++-- src/ui/ui.h | 2 +- src/ui/ui.meta.c | 38 ++++----- src/wasm_app/main.c | 41 ++++++++-- src/wasm_app/win32_exe.c | 5 ++ todo.txt | 1 + 31 files changed, 415 insertions(+), 219 deletions(-) create mode 100644 src/core/core_ctx.c create mode 100644 src/wasm_app/win32_exe.c diff --git a/build_file.c b/build_file.c index 2e60743..1709b1e 100644 --- a/build_file.c +++ b/build_file.c @@ -17,8 +17,8 @@ #include "src/wasm_app/wasm_app.meta.c" int main(int argc, char **argv) { + core_init(); int ok = 0; - ma_arena_t *arena = ma_create(ma_default_reserve_size); SRC_SearchPaths.include_path = (char*[]){OS_GetAbsolutePath(&Perm, s8_lit("../src")).str}; SRC_SearchPaths.include_path_count = 1; @@ -30,10 +30,10 @@ int main(int argc, char **argv) { os_systemf("py core_math_gen.py"); os_set_working_dir("../../build"); } - mt_ui(arena); - mt_app(arena); - mt_wasm_app(arena); - mt_render(arena); + mt_ui(tcx->temp); + mt_app(tcx->temp); + mt_wasm_app(tcx->temp); + mt_render(tcx->temp); b32 run_server = false; b32 core_test_target = true; @@ -44,10 +44,22 @@ int main(int argc, char **argv) { os_systemf("start /D ..\\package ..\\package\\run_server.bat"); } - if (win32_target && cache_code_modified(s8_lit("../src/wasm_app/main.c"), s8_lit("app_win32.exe"))) { + if (win32_target && cache_code_modified(s8_lit("../src/wasm_app/main.c"), s8_lit("app.dll"))) { os_delete_file(s8_lit("app_win32.pdb")); ok = os_systemf( - "cl ../src/wasm_app/main.c -Fe:app_win32.exe -Fd:app_win32.pdb" + "cl ../src/wasm_app/main.c -Fe:app.dll -Fd:app.pdb" + " -I ../src" + " /Zi /FC /nologo /Oi" + " /WX /W3 /wd4200 /diagnostics:column" + " /link /incremental:no /DEBUG:FULL /DLL" + ); + if (ok != 0) return ok; + } + + if (win32_target && cache_code_modified(s8_lit("../src/wasm_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" " -I ../src" " /Zi /FC /nologo /Oi" " /WX /W3 /wd4200 /diagnostics:column" diff --git a/src/app/app.c b/src/app/app.c index 00003c4..f6681d2 100644 --- a/src/app/app.c +++ b/src/app/app.c @@ -5,8 +5,3 @@ #elif PLATFORM_WASM #include "app_wasm.c" #endif - -fn r2f32_t window_rect_from_frame(app_frame_t *frame) { - r2f32_t window_rect = r2f32(0, 0, frame->window_size.x, frame->window_size.y); - return window_rect; -} \ No newline at end of file diff --git a/src/app/app.gen.c b/src/app/app.gen.c index a5899eb..61afa03 100644 --- a/src/app/app.gen.c +++ b/src/app/app.gen.c @@ -243,6 +243,9 @@ type_t type__app_event_kind_t = { type_kind_enum, s8_const_lit("app_event_kind_t .members = (type_member_t[]){ {.name = s8_const_lit("app_event_kind_null"), .value = app_event_kind_null}, {.name = s8_const_lit("app_event_kind_update"), .value = app_event_kind_update}, + {.name = s8_const_lit("app_event_kind_init"), .value = app_event_kind_init}, + {.name = s8_const_lit("app_event_kind_reload"), .value = app_event_kind_reload}, + {.name = s8_const_lit("app_event_kind_unload"), .value = app_event_kind_unload}, {.name = s8_const_lit("app_event_kind_text"), .value = app_event_kind_text}, {.name = s8_const_lit("app_event_kind_key_down"), .value = app_event_kind_key_down}, {.name = s8_const_lit("app_event_kind_key_up"), .value = app_event_kind_key_up}, @@ -252,7 +255,7 @@ type_t type__app_event_kind_t = { type_kind_enum, s8_const_lit("app_event_kind_t {.name = s8_const_lit("app_event_kind_mouse_move"), .value = app_event_kind_mouse_move}, {.name = s8_const_lit("app_event_kind_count"), .value = app_event_kind_count}, }, - .count = 10, + .count = 13, }; type_t type__app_event_t = { type_kind_struct, s8_const_lit("app_event_t"), sizeof(app_event_t), .members = (type_member_t[]){ diff --git a/src/app/app.gen.h b/src/app/app.gen.h index 411446f..afde91a 100644 --- a/src/app/app.gen.h +++ b/src/app/app.gen.h @@ -82,6 +82,9 @@ typedef enum { typedef enum { app_event_kind_null, app_event_kind_update, + app_event_kind_init, + app_event_kind_reload, + app_event_kind_unload, app_event_kind_text, app_event_kind_key_down, app_event_kind_key_up, diff --git a/src/app/app.h b/src/app/app.h index f3d2aa0..562f567 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -1,3 +1,9 @@ #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); +#define fn_export __declspec(dllexport) \ No newline at end of file diff --git a/src/app/app.meta.c b/src/app/app.meta.c index d3f8b1d..3be498b 100644 --- a/src/app/app.meta.c +++ b/src/app/app.meta.c @@ -146,6 +146,9 @@ void mt_app(ma_arena_t *arena) { typedef enum { app_event_kind_null, app_event_kind_update, + app_event_kind_init, + app_event_kind_reload, + app_event_kind_unload, app_event_kind_text, app_event_kind_key_down, app_event_kind_key_up, diff --git a/src/app/app_wasm.c b/src/app/app_wasm.c index 4c0ad10..e5f4883 100644 --- a/src/app/app_wasm.c +++ b/src/app/app_wasm.c @@ -22,7 +22,7 @@ fn b32 app_update(app_frame_t *frame); fn void app_init(f32 dpr); fn void wasm_add_event(app_event_t event) { - app_event_t *ev = ma_push_type(tcx.temp, app_event_t); + app_event_t *ev = ma_push_type(tcx->temp, app_event_t); *ev = event; ev->id = ++wasm_event_ids; ev->alt = wasm_cached.alt; @@ -99,7 +99,7 @@ fn_wasm_export void wasm_key_down(char *key, b32 ctrl, b32 shift, b32 alt, b32 m return; } - s8_t text = s8_copy(tcx.temp, key8); + s8_t text = s8_copy(tcx->temp, key8); wasm_add_event((app_event_t){ .kind = app_event_kind_text, .text = text, @@ -134,7 +134,7 @@ fn_wasm_export b32 wasm_update(f64 time, f32 width, f32 height, f32 dpr) { wasm_frame.frame = wasm_frame_counter; b32 animating = app_update(&wasm_frame); - ma_set0(tcx.temp); + ma_set0(tcx->temp); zero_struct(&wasm_frame); wasm_last_time = wasm_time; wasm_frame_counter += 1; diff --git a/src/app/app_win32.c b/src/app/app_win32.c index 2fd2afe..fb9dad5 100644 --- a/src/app/app_win32.c +++ b/src/app/app_win32.c @@ -1,20 +1,16 @@ #include "app_win32_opengl.c" -#include "glad/glad.h" -#include "glad/glad.c" #pragma comment(linker, "/subsystem:windows") #pragma comment(lib, "gdi32.lib") #pragma comment(lib, "user32.lib") #pragma comment(lib, "winmm.lib") -fn void app_init(f32 dpr); -fn b32 app_update(app_frame_t *frame); - gb b32 w32_good_scheduling; 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 v2f32_t w32_get_window_size(HWND window) { RECT window_rect; @@ -269,8 +265,110 @@ void w32_try_resizing_canvas(w32_canvas_t *canvas, HWND window_handle) { } } +/////////////////////////////// +// dll loading + +typedef struct w32_library_t w32_library_t; +struct w32_library_t { + i32 reload_count; + HMODULE library; + i64 last_write_time; + random_seed_t seed; + + s8_t dll_name; + s8_t update_fn_name; + app_update_t *update_fn; +}; + +fn b32 os_copy(s8_t from, s8_t to, b32 overwrite) { + ma_temp_t scratch = ma_begin_scratch(); + s16_t from16 = s16_from_s8(scratch.arena, from); + s16_t to16 = s16_from_s8(scratch.arena, to); + + BOOL fail_if_exists = !overwrite; + BOOL success = CopyFileW(from16.str, to16.str, fail_if_exists); + b32 result = success ? true : false; + ma_end_scratch(scratch); + return result; +} + +fn i64 os_get_file_mod_time(s8_t file) { + FILETIME time = {0}; + WIN32_FIND_DATAW data; + + ma_temp_t scratch = ma_begin_scratch(); + s16_t wpath = s16_from_s8(scratch.arena, file); + HANDLE handle = FindFirstFileW(wpath.str, &data); + if (handle != INVALID_HANDLE_VALUE) { + FindClose(handle); + time = data.ftLastWriteTime; + } else { + return -1; + } + i64 result = (i64)time.dwHighDateTime << 32 | time.dwLowDateTime; + ma_end_scratch(scratch); + return result; +} + +fn void w32_set_event(app_frame_t *frame, app_event_t ev) { + frame->first_event = frame->last_event = NULL; + frame->event_count = 0; + w32_push_event(frame, ev); +} + +fn void w32_prepare_for_update_call(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; + } + + 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 = NULL; + } + + if (lib->seed.a == 0) { + lib->seed.a = 3242323; + } + u64 random_value = get_random_u64(&lib->seed); + + s8_t base_name = s8_chop_last_period(lib->dll_name); + s8_t new_dll_name = s8_printf(tcx->temp, "%S_temp_%u.dll", base_name, random_value); + os_copy(lib->dll_name, new_dll_name, true); + + s8_t pdb_name = s8_printf(tcx->temp, "%S.pdb", base_name); + s8_t new_pdb_name = s8_printf(tcx->temp, "%S_temp_%u.pdb", base_name, random_value); + os_copy(pdb_name, new_pdb_name, true); + + HMODULE library = LoadLibraryA(new_dll_name.str); + if (!library) { + debugf("failed to load library: %S", new_dll_name); + goto end_of_lib_load; + } + + void *update_fn = GetProcAddress(library, lib->update_fn_name.str); + if (!update_fn) { + debugf("failed to load %S function", lib->update_fn_name); + goto end_of_lib_load; + } + + lib->library = library; + lib->update_fn = update_fn; + lib->last_write_time = new_write_time; + lib->reload_count += 1; + if (lib->reload_count <= 1) { + w32_set_event(&frame, (app_event_t){.kind = app_event_kind_init}); + } else { + w32_set_event(&frame, (app_event_t){.kind = app_event_kind_reload}); + } + lib->update_fn(&app_ctx, &frame); + +end_of_lib_load:; +} + int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { - tcx.temp = ma_create(ma_default_reserve_size); + core_init(); // Set DPI aware, @todo: verify / new way to do this | @todo: get dpi ratio { @@ -346,10 +444,6 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n ShowWindow(w32_window_handle, SW_SHOW); } - if (!gladLoadGLLoader((GLADloadproc)w32_load_opengl_fn)) { - fatalf("couldn't load opengl!"); - } - if (wglSwapIntervalEXT) { wglSwapIntervalEXT(1); // vsync } @@ -360,7 +454,12 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n refresh_rate = (f64)devmodew.dmDisplayFrequency; } - app_init(w32_get_dpr(w32_window_handle)); + 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; f64 time_frame_start = w32_seconds_now(); f64 time_delta = 1.0 / refresh_rate; @@ -373,7 +472,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n for (;;) { app_frame_t frame = {0}; - b32 waited = w32_get_events(tcx.temp, &frame, wait_for_events); + b32 waited = w32_get_events(tcx->temp, &frame, wait_for_events); if (waited) { frame.delta = 0.00001; } @@ -395,12 +494,16 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n frame.update = time_update; frame.frame = frame_counter; - b32 animating = app_update(&frame); + b32 animating = true; + w32_prepare_for_update_call(&lib, frame); + if (lib.update_fn) { + animating = lib.update_fn(&app_ctx, &frame); + } wait_for_events = !animating; SwapBuffers(w32_dc); - ma_set0(tcx.temp); + ma_set0(tcx->temp); /////////////////////////////// // end of frame timings diff --git a/src/core/core_arena.c b/src/core/core_arena.c index 054a26f..ec1dfcb 100644 --- a/src/core/core_arena.c +++ b/src/core/core_arena.c @@ -140,8 +140,8 @@ fn void ma_set0(ma_arena_t *arena) { ma_set_len(arena, 0); } // Scratch arena fn ma_temp_t ma_begin_scratch_ex(ma_arena_t **conflicts, int conflict_count) { ma_arena_t *unoccupied = 0; - for (int i = 0; i < lengthof(tcx.scratch); i += 1) { - ma_arena_t *from_pool = tcx.scratch + i; + for (int i = 0; i < lengthof(tcx->scratch); i += 1) { + ma_arena_t *from_pool = tcx->scratch + i; unoccupied = from_pool; for (int conflict_i = 0; conflict_i < conflict_count; conflict_i += 1) { ma_arena_t *from_conflict = conflicts[conflict_i]; @@ -162,7 +162,7 @@ fn ma_temp_t ma_begin_scratch_ex(ma_arena_t **conflicts, int conflict_count) { } fn ma_temp_t ma_begin_scratch(void) { - ma_temp_t result = ma_begin_temp(tcx.scratch + 0); + ma_temp_t result = ma_begin_temp(tcx->scratch + 0); return result; } diff --git a/src/core/core_ctx.c b/src/core/core_ctx.c new file mode 100644 index 0000000..4386eea --- /dev/null +++ b/src/core/core_ctx.c @@ -0,0 +1,15 @@ +gb_thread thread_ctx_t global_thread_context = { + .log = { + .break_on_fatal = true, + .break_on_error = true, + .break_on_warning = true, + .log_proc = default_log_proc, + } +}; + +fn void core_init(void) { + tcx = &global_thread_context; + tcx->temp = ma_create(ma_default_reserve_size); + ma_init(&tcx->perm, ma_default_reserve_size); + os_core_init(); +} \ No newline at end of file diff --git a/src/core/core_ctx.h b/src/core/core_ctx.h index edb55d2..9b409ad 100644 --- a/src/core/core_ctx.h +++ b/src/core/core_ctx.h @@ -7,17 +7,10 @@ struct thread_ctx_t { // to functions that allocate pernament memory. temp and scratch very nicely create reusable code // perm is basically gb state so it would be nice to annotate which functions are reusable // and which functions have state - ma_arena_t _perm; - + ma_arena_t perm; + void *data[32]; logger_t log; }; -gb_thread thread_ctx_t tcx = { - .log = { - .break_on_fatal = true, - .break_on_error = true, - .break_on_warning = true, - .log_proc = default_log_proc, - } -}; +gb_thread thread_ctx_t *tcx; diff --git a/src/core/core_inc.c b/src/core/core_inc.c index 4691805..922d237 100644 --- a/src/core/core_inc.c +++ b/src/core/core_inc.c @@ -47,4 +47,5 @@ #include "core_hash_table.c" #ifndef DONT_INCLUDE_GENERATED_MATH #include "core_math.gen.c" -#endif \ No newline at end of file +#endif +#include "core_ctx.c" \ No newline at end of file diff --git a/src/core/core_log.c b/src/core/core_log.c index 03638ab..ef78d5f 100644 --- a/src/core/core_log.c +++ b/src/core/core_log.c @@ -1,7 +1,7 @@ fn void log_basef(log_level_t level, s8_t file_and_line, const char *str, ...) { ma_temp_t scratch = ma_begin_scratch(); S8_FMT(scratch.arena, str, string); - tcx.log.log_proc(level, file_and_line, string); + tcx->log.log_proc(level, file_and_line, string); ma_end_scratch(scratch); } @@ -15,11 +15,11 @@ fn void default_log_proc(log_level_t level, s8_t file_and_line, s8_t string) { } break; case log_level_warning: { os_console_log(s8_printf(scratch.arena, "%S: warning: %S\n", file_and_line, string).str); - if (tcx.log.break_on_warning) debug_break(); + if (tcx->log.break_on_warning) debug_break(); } break; case log_level_error: { os_console_log(s8_printf(scratch.arena, "%S: error: %S\n", file_and_line, string).str); - if (tcx.log.break_on_error) debug_break(); + if (tcx->log.break_on_error) debug_break(); } break; case log_level_fatal: { os_error_box(s8_printf(scratch.arena, "%S: fatal error: %S\n", file_and_line, string).str); diff --git a/src/core/core_platform_unix.c b/src/core/core_platform_unix.c index d30e0e1..19e7cf6 100644 --- a/src/core/core_platform_unix.c +++ b/src/core/core_platform_unix.c @@ -33,5 +33,5 @@ fn f64 os_parse_float(char *str) { return strtod(str, NULL); } -fn void core_init(void) { +fn void os_core_init(void) { } \ No newline at end of file diff --git a/src/core/core_platform_wasm.c b/src/core/core_platform_wasm.c index eece586..79616d6 100644 --- a/src/core/core_platform_wasm.c +++ b/src/core/core_platform_wasm.c @@ -36,7 +36,7 @@ fn f64 os_parse_float(char *str) { return wasm_parse_float((isize)str, str_len((char *)str)); } -fn void core_init(void) { +fn void os_core_init(void) { isize page_size = kib(64); isize page_count = __builtin_wasm_memory_size(0); u8 *memory = (u8 *)&__heap_base; diff --git a/src/core/core_platform_win32.c b/src/core/core_platform_win32.c index fac687e..80475f9 100644 --- a/src/core/core_platform_win32.c +++ b/src/core/core_platform_win32.c @@ -1,5 +1,5 @@ fn void *os_vmem_reserve(usize size) { - void *result = (uint8_t *)VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE); + void *result = VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE); return result; } @@ -33,5 +33,5 @@ fn f64 os_parse_float(char *str) { return strtod(str, NULL); } -fn void core_init(void) { +fn void os_core_init(void) { } \ No newline at end of file diff --git a/src/core/core_test_entry.c b/src/core/core_test_entry.c index 959285c..bf34874 100644 --- a/src/core/core_test_entry.c +++ b/src/core/core_test_entry.c @@ -151,6 +151,7 @@ void test_intern_table(void) { int main(int argc, char **argv) { + core_init(); printf("PLATFORM_WASM = %d\n", PLATFORM_WASM); printf("PLATFORM_WINDOWS = %d\n", PLATFORM_WINDOWS); printf("PLATFORM_LINUX = %d\n", PLATFORM_LINUX); diff --git a/src/meta/build_tool.c b/src/meta/build_tool.c index 944f18e..c1eaa4e 100644 --- a/src/meta/build_tool.c +++ b/src/meta/build_tool.c @@ -5031,7 +5031,7 @@ SRC_CacheEntry *cache_hash_file(S8_String file, char *parent_file) { if (token.is_system_include) continue; S8_String file_it = S8_MakeFromChar(token.string_literal); - if (s8_are_equal(file_it, file)) continue; // prevent recursing, file includes itself (stb_truetype) + if (S8_AreEqual(file_it, file, true)) continue; // prevent recursing, file includes itself (stb_truetype) SRC_CacheEntry *cache = cache_hash_file(file_it, resolved_file); if (!cache) { diff --git a/src/render/render.c b/src/render/render.c index 98df36c..55d8236 100644 --- a/src/render/render.c +++ b/src/render/render.c @@ -1,19 +1,19 @@ fn rn_cmd_t *rn_get_cmd(rn_cmd_kind_t kind) { b32 alloc_new = false; - if (rn_state.last_cmd == NULL) { + if (rn->last_cmd == NULL) { alloc_new = true; - } else if (rn_state.last_cmd->kind != kind) { + } else if (rn->last_cmd->kind != kind) { alloc_new = true; } - rn_cmd_t *result = rn_state.last_cmd; + rn_cmd_t *result = rn->last_cmd; if (alloc_new) { - result = ma_push_type(tcx.temp, rn_cmd_t); + result = ma_push_type(tcx->temp, rn_cmd_t); result->kind = kind; - SLLQ_APPEND(rn_state.first_cmd, rn_state.last_cmd, result); + SLLQ_APPEND(rn->first_cmd, rn->last_cmd, result); if (rn_cmd_kind_quad) { - result->vertex = rn_state.vertices + rn_state.len; + result->vertex = rn->vertices + rn->len; } } @@ -22,7 +22,7 @@ fn rn_cmd_t *rn_get_cmd(rn_cmd_kind_t kind) { fn rn_vertex_t *rn_push_vertex(rn_cmd_t *cmd, u32 count) { rn_vertex_t *result = cmd->vertex + cmd->len; - rn_state.len += count; + rn->len += count; cmd->len += count; return result; } @@ -63,7 +63,7 @@ fn void rn_push_quad(r2f32_t rect, r2f32_t tex, v4f32_t color) { } fn void rn_draw_rect(r2f32_t rect, v4f32_t color) { - rn_push_quad(rect, rn_state.main_font->white_texture_bounding_box, color); + rn_push_quad(rect, rn->main_font->white_texture_bounding_box, color); } fn void rn_draw_rect_border(r2f32_t rect, v4f32_t color, f32 border_thickness) { @@ -71,10 +71,10 @@ fn void rn_draw_rect_border(r2f32_t rect, v4f32_t color, f32 border_thickness) { r2f32_t right = r2f32_cut_right(&rect, border_thickness); r2f32_t top = r2f32_cut_top(&rect, border_thickness); r2f32_t bottom = r2f32_cut_bottom(&rect, border_thickness); - rn_push_quad(left, rn_state.main_font->white_texture_bounding_box, color); - rn_push_quad(right, rn_state.main_font->white_texture_bounding_box, color); - rn_push_quad(top, rn_state.main_font->white_texture_bounding_box, color); - rn_push_quad(bottom, rn_state.main_font->white_texture_bounding_box, color); + rn_push_quad(left, rn->main_font->white_texture_bounding_box, color); + rn_push_quad(right, rn->main_font->white_texture_bounding_box, color); + rn_push_quad(top, rn->main_font->white_texture_bounding_box, color); + rn_push_quad(bottom, rn->main_font->white_texture_bounding_box, color); } fn i64 rn_get_char_spacing(rn_font_t *font, u32 codepoint) { @@ -113,7 +113,7 @@ fn v2f32_t rn_draw_string(rn_font_t *font, v2f32_t pos, v4f32_t color, s8_t stri } fn v2f32_t rn_draw_stringf(rn_font_t *font, v2f32_t pos, v4f32_t color, char *str, ...) { - S8_FMT(tcx.temp, str, result); + S8_FMT(tcx->temp, str, result); return rn_draw_string(font, pos, color, result); } diff --git a/src/render/render.h b/src/render/render.h index cba91af..eba03c6 100644 --- a/src/render/render.h +++ b/src/render/render.h @@ -83,4 +83,4 @@ struct rn_state_t { u32 vao; u32 vbo; }; -gb rn_state_t rn_state; \ No newline at end of file +gb rn_state_t *rn; \ No newline at end of file diff --git a/src/render/render_inc.c b/src/render/render_inc.c index 26a48bb..3ef0272 100644 --- a/src/render/render_inc.c +++ b/src/render/render_inc.c @@ -11,7 +11,7 @@ #define STBTT_acos(x) (f64_acos(x)) #define STBTT_fabs(x) (f64_abs(x)) #define STBTT_assert(x) (assert(x)) -#define STBTT_malloc(x,u) (ma_push_size(tcx.temp, x)) +#define STBTT_malloc(x,u) (ma_push_size(tcx->temp, x)) #define STBTT_free(x,u) #define STBTT_strlen(x) (str_len(x)) #define STBTT_memcpy memory_copy diff --git a/src/render/render_opengl.c b/src/render/render_opengl.c index ea368ea..163fbcb 100644 --- a/src/render/render_opengl.c +++ b/src/render/render_opengl.c @@ -33,13 +33,13 @@ fn void gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity } fn void rn_reload_font(f32 font_size, f32 dpr) { - if (rn_state.main_font->texture_id) { - glDeleteTextures(1, &rn_state.main_font->texture_id); + if (rn->main_font->texture_id) { + glDeleteTextures(1, &rn->main_font->texture_id); } ma_temp_t scratch = ma_begin_scratch(); rn_atlas_t *atlas = rn_create_atlas(scratch.arena, (v2i32_t){2048, 2048}); - rn_reload_font_atlas(rn_state.main_font, main_font, atlas, (i32)(font_size * dpr)); + rn_reload_font_atlas(rn->main_font, main_font, atlas, (i32)(font_size * dpr)); GLint filter = GL_NEAREST; glCreateTextures(GL_TEXTURE_2D, 1, &atlas->texture_id); @@ -49,42 +49,44 @@ fn void rn_reload_font(f32 font_size, f32 dpr) { glTextureParameteri(atlas->texture_id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTextureStorage2D(atlas->texture_id, 1, GL_R8, (GLsizei)atlas->size.x, (GLsizei)atlas->size.y); glTextureSubImage2D(atlas->texture_id, 0, 0, 0, (GLsizei)atlas->size.x, (GLsizei)atlas->size.y, GL_RED, GL_UNSIGNED_BYTE, atlas->bitmap); - rn_state.main_font->texture_id = atlas->texture_id; + rn->main_font->texture_id = atlas->texture_id; ma_end_scratch(scratch); } fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) { - rn_state.cap = 1024*256; - rn_state.vertices = ma_push_array(perm, rn_vertex_t, rn_state.cap); + tcx->data[0] = ma_push_type(perm, rn_state_t); + rn = tcx->data[0]; + rn->cap = 1024*256; + rn->vertices = ma_push_array(perm, rn_vertex_t, rn->cap); - rn_state.main_font = rn_create_font(perm); + rn->main_font = rn_create_font(perm); rn_reload_font(font_size, dpr); glDebugMessageCallback(&gl_debug_callback, NULL); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); - glCreateBuffers(1, &rn_state.vbo); - glNamedBufferStorage(rn_state.vbo, rn_state.cap * sizeof(rn_vertex_t), 0, GL_DYNAMIC_STORAGE_BIT); + glCreateBuffers(1, &rn->vbo); + glNamedBufferStorage(rn->vbo, rn->cap * sizeof(rn_vertex_t), 0, GL_DYNAMIC_STORAGE_BIT); - glCreateVertexArrays(1, &rn_state.vao); + glCreateVertexArrays(1, &rn->vao); GLint vbuf_index = 0; - glVertexArrayVertexBuffer(rn_state.vao, vbuf_index, rn_state.vbo, 0, sizeof(struct rn_vertex_t)); + glVertexArrayVertexBuffer(rn->vao, vbuf_index, rn->vbo, 0, sizeof(struct rn_vertex_t)); GLint a_pos = 0; - glVertexArrayAttribFormat(rn_state.vao, a_pos, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, pos)); - glVertexArrayAttribBinding(rn_state.vao, a_pos, vbuf_index); - glEnableVertexArrayAttrib(rn_state.vao, a_pos); + glVertexArrayAttribFormat(rn->vao, a_pos, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, pos)); + glVertexArrayAttribBinding(rn->vao, a_pos, vbuf_index); + glEnableVertexArrayAttrib(rn->vao, a_pos); GLint a_tex = 1; - glVertexArrayAttribFormat(rn_state.vao, a_tex, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, tex)); - glVertexArrayAttribBinding(rn_state.vao, a_tex, vbuf_index); - glEnableVertexArrayAttrib(rn_state.vao, a_tex); + glVertexArrayAttribFormat(rn->vao, a_tex, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, tex)); + glVertexArrayAttribBinding(rn->vao, a_tex, vbuf_index); + glEnableVertexArrayAttrib(rn->vao, a_tex); GLint a_color = 2; - glVertexArrayAttribFormat(rn_state.vao, a_color, 4, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, color)); - glVertexArrayAttribBinding(rn_state.vao, a_color, vbuf_index); - glEnableVertexArrayAttrib(rn_state.vao, a_color); + glVertexArrayAttribFormat(rn->vao, a_color, 4, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, color)); + glVertexArrayAttribBinding(rn->vao, a_color, vbuf_index); + glEnableVertexArrayAttrib(rn->vao, a_color); char *glsl_vshader = "#version 450 core\n" @@ -123,17 +125,21 @@ fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) { " OutColor = c;\n" "}\n"; - rn_state.shader2d = rn_create_shader(glsl_vshader, glsl_fshader); + rn->shader2d = rn_create_shader(glsl_vshader, glsl_fshader); } -void rn_begin(app_frame_t *frame, v4f32_t clear_color) { - rn_state.frame = frame; - rn_state.clear_color = clear_color; +void rn_begin_frame(rn_state_t *rn_ctx, app_frame_t *frame) { + rn = rn_ctx; + rn->frame = frame; +} + +void rn_begin(v4f32_t clear_color) { + rn->clear_color = clear_color; } void rn_end(void) { - f32 wx = rn_state.frame->window_size.x; - f32 wy = rn_state.frame->window_size.y; + f32 wx = rn->frame->window_size.x; + f32 wy = rn->frame->window_size.y; glEnable(GL_BLEND); glEnable(GL_SCISSOR_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -142,21 +148,21 @@ void rn_end(void) { glViewport(0, 0, (GLsizei)wx, (GLsizei)wy); glScissor(0, 0, (GLsizei)wx, (GLsizei)wy); - glClearColor(rn_state.clear_color.r, rn_state.clear_color.g, rn_state.clear_color.b, rn_state.clear_color.a); + glClearColor(rn->clear_color.r, rn->clear_color.g, rn->clear_color.b, rn->clear_color.a); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Default draw using the font texture - glBindProgramPipeline(rn_state.shader2d.pipeline); + glBindProgramPipeline(rn->shader2d.pipeline); float xinverse = 1.f / (wx / 2.f); float yinverse = 1.f / (wy / 2.f); - glProgramUniform2f(rn_state.shader2d.vshader, 0, xinverse, yinverse); + glProgramUniform2f(rn->shader2d.vshader, 0, xinverse, yinverse); - for (rn_cmd_t *it = rn_state.first_cmd; it; it = it->next) { + for (rn_cmd_t *it = rn->first_cmd; it; it = it->next) { if (it->kind == rn_cmd_kind_quad) { - glNamedBufferSubData(rn_state.vbo, 0, it->len * sizeof(rn_vertex_t), it->vertex); - glBindVertexArray(rn_state.vao); + glNamedBufferSubData(rn->vbo, 0, it->len * sizeof(rn_vertex_t), it->vertex); + glBindVertexArray(rn->vao); GLint s_texture = 0; // texture unit that sampler2D will use in GLSL code - glBindTextureUnit(s_texture, rn_state.main_font->texture_id); + glBindTextureUnit(s_texture, rn->main_font->texture_id); glDrawArrays(GL_TRIANGLES, 0, it->len); } else if (it->kind == rn_cmd_kind_set_clip) { GLint x = (GLint)it->rect.min.x; @@ -169,7 +175,7 @@ void rn_end(void) { } else_is_invalid; } - rn_state.first_cmd = NULL; - rn_state.last_cmd = NULL; - rn_state.len = 0; -} + rn->first_cmd = NULL; + rn->last_cmd = NULL; + rn->len = 0; +}// diff --git a/src/render/render_wasm_canvas.c b/src/render/render_wasm_canvas.c index 4dcf3cb..03baf63 100644 --- a/src/render/render_wasm_canvas.c +++ b/src/render/render_wasm_canvas.c @@ -19,11 +19,11 @@ struct rn_state_t { rn_state_t rn_state; fn void rn_reload_font(f32 font_size, f32 dpr) { - rn_state.main_font->size = font_size * dpr; + rn->main_font->size = font_size * dpr; } fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) { - rn_state.main_font = ma_push_type(perm, rn_font_t); + rn->main_font = ma_push_type(perm, rn_font_t); rn_reload_font(font_size, dpr); } @@ -43,12 +43,12 @@ fn void rn_draw_rect_border(r2f32_t rect, v4f32_t color, f32 border_thickness) { } fn v2f32_t rn_measure_string(rn_font_t *font, s8_t string) { - f32 x = wasm_measure_text((isize)string.str, string.len, (isize) font_face.str, font_face.len, rn_state.main_font->size); - return (v2f32_t){x, rn_state.main_font->size}; + f32 x = wasm_measure_text((isize)string.str, string.len, (isize) font_face.str, font_face.len, rn->main_font->size); + return (v2f32_t){x, rn->main_font->size}; } fn v2f32_t rn_draw_string(rn_font_t *font, v2f32_t pos, v4f32_t color, s8_t string) { - wasm_draw_text((isize)string.str, string.len, pos.x, pos.y, (isize) font_face.str, font_face.len, rn_state.main_font->size, color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a); + wasm_draw_text((isize)string.str, string.len, pos.x, pos.y, (isize) font_face.str, font_face.len, rn->main_font->size, color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a); v2f32_t size = rn_measure_string(font, string); return size; } @@ -63,8 +63,8 @@ fn void rn_set_clip(r2f32_t rect) { } fn void rn_begin(app_frame_t *frame, v4f32_t clear_color) { - rn_state.frame = frame; - r2f32_t window_rect = r2f32(0, 0, rn_state.frame->window_size.x, rn_state.frame->window_size.y); + rn->frame = frame; + r2f32_t window_rect = r2f32(0, 0, rn->frame->window_size.x, rn->frame->window_size.y); wasm_clear(); rn_draw_rect(window_rect, clear_color); } diff --git a/src/ui/ui.c b/src/ui/ui.c index 4ffdb61..5cefd43 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -36,7 +36,7 @@ fn ui_caret_t ui_carets(i32 x) { } fn s8_t ui_tprint_loc(ui_code_loc_t loc) { - return s8_printf(tcx.temp, "%s(%d)", loc.file, loc.line); + return s8_printf(tcx->temp, "%s(%d)", loc.file, loc.line); } fn s8_t ui_get_display_string(s8_t string) { @@ -59,6 +59,11 @@ fn ui_id_t ui_id_from_string(s8_t string) { return result; } +fn r2f32_t window_rect_from_frame(app_frame_t *frame) { + r2f32_t window_rect = r2f32(0, 0, frame->window_size.x, frame->window_size.y); + return window_rect; +} + fn u64 ui_hash_code_loc(ui_code_loc_t loc) { u64 result = 4242843; u64 file_hash = hash_data(s8_from_char(loc.file)); @@ -97,7 +102,7 @@ fn ui_id_t ui_id(s8_t string) { } fn ui_id_t ui_idf(char *str, ...) { - S8_FMT(tcx.temp, str, string); + S8_FMT(tcx->temp, str, string); return ui_id(string); } @@ -189,7 +194,7 @@ fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, ui_box_flags_t flags, s ui_id_t id = ui_id(string); ui_box_t *box = ui_build_box_from_id(loc, flags, id); box->string = ui_get_display_string(string); - v2f32_t string_size = rn_measure_string(rn_state.main_font, box->string); + v2f32_t string_size = rn_measure_string(rn->main_font, box->string); ui_axis2_t axis = ui_axis_from_lop(ui_top_lop()); if (ui->required_size_stack && (axis == ui_axis2_x || axis == ui_axis2_y)) { string_size.e[axis] = ui_top_required_size(); @@ -207,7 +212,7 @@ fn ui_box_t *ui_build_box_from_string(ui_code_loc_t loc, ui_box_flags_t flags, s #define ui_boxf(...) ui__boxf(UILOC, __VA_ARGS__) fn ui_box_t *ui__boxf(ui_code_loc_t loc, ui_box_flags_t flags, char *str, ...) { - S8_FMT(tcx.temp, str, string); + S8_FMT(tcx->temp, str, string); flags.dont_compute_rect = true; ui_box_t *box = ui_build_box_from_string(loc, flags, string); return box; @@ -216,7 +221,7 @@ fn ui_box_t *ui__boxf(ui_code_loc_t loc, ui_box_flags_t flags, char *str, ...) { #define ui_box0(...) ui_build_box_from_id(UILOC, __VA_ARGS__, ui_null_id) #define ui_box0f(...) ui__box0f(UILOC, __VA_ARGS__) fn ui_box_t *ui__box0f(ui_code_loc_t loc, ui_box_flags_t flags, char *str, ...) { - S8_FMT(tcx.temp, str, string); + S8_FMT(tcx->temp, str, string); ui_box_t *box = ui_build_box_from_id(loc, flags, ui_null_id); box->string = string; return box; @@ -270,7 +275,7 @@ fn ui_signal_t ui_signal_from_box(ui_box_t *box) { } fn v2f32_t ui_aligned_text_pos(f32 offset, ui_text_align_t text_align, r2f32_t rect, s8_t string) { - v2f32_t string_size = rn_measure_string(rn_state.main_font, string); + v2f32_t string_size = rn_measure_string(rn->main_font, string); v2f32_t rect_size = r2f32_get_size(rect); v2f32_t rect_string_diff = v2f32_sub(rect_size, string_size); v2f32_t center_pos = v2f32_divs(rect_string_diff, 2); @@ -309,16 +314,18 @@ fn ui_draw_compute_t ui_draw_compute(ui_box_t *box) { f32 active_t = f32_ease_out_n(box->active_t, 50.f); active_t = f32_clamp01(active_t); co.background_color = v4f32_lerp(co.background_color, box->bg_active_color, 1.0); - co.text_color = v4f32_lerp(co.text_color, box->text_active_color, active_t); + // co.text_color = v4f32_lerp(co.text_color, box->text_active_color, active_t); + box->flags.draw_rect = true; } else if (ui_is_hot_box(box)) { - f32 hot_t = f32_ease_out_n(box->hot_t, 3.f); + f32 hot_t = f32_ease_out_n(box->hot_t, 50.f); hot_t = f32_clamp01(hot_t); co.background_color = v4f32_lerp(co.background_color, box->bg_hot_color, hot_t); - co.text_color = v4f32_lerp(co.background_color, box->text_hot_color, hot_t); - } else if (ui_is_focused_box(box)) { - co.background_color = ui_color_table[ui_color_focused_rect_color]; + // co.text_color = v4f32_lerp(co.background_color, box->text_hot_color, hot_t); box->flags.draw_rect = true; + } else if (ui_is_focused_box(box)) { + co.background_color = ui_color_table[ui_color_focused_rect]; // co.text_color = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.7f, 1.0f}); + box->flags.draw_rect = true; } return co; @@ -332,15 +339,15 @@ fn void ui_text_input_draw(ui_box_t *box) { ui_text_input_t *ti = box->text_input; s8_t string = s8(ti->str, ti->len); v2f32_t pos = ui_aligned_text_pos(box->string_pos_offset, box->text_align, co.rect, string); - rn_draw_string(rn_state.main_font, pos, co.text_color, string); + rn_draw_string(rn->main_font, pos, co.text_color, string); ti->caret = ui_caret_clamp(ti->caret, 0, (i32)ti->len); { s8_t string_min = s8(ti->str, ti->caret.range.min); s8_t string_max = s8(ti->str, ti->caret.range.max); - v2f32_t size_min = rn_measure_string(rn_state.main_font, string_min); - v2f32_t size_max = rn_measure_string(rn_state.main_font, string_max); + v2f32_t size_min = rn_measure_string(rn->main_font, string_min); + v2f32_t size_max = rn_measure_string(rn->main_font, string_max); r2f32_t selection_rect = r2f32(co.rect.min.x + size_min.x, co.rect.min.y, co.rect.min.x + size_max.x, co.rect.min.y + ui_em(1)); rn_draw_rect(selection_rect, v4f32(1,1,1,0.5f)); @@ -363,7 +370,7 @@ fn void ui_default_draw_box(ui_box_t *box) { } if (box->flags.draw_text) { v2f32_t text_pos = ui_aligned_text_pos(box->string_pos_offset, box->text_align, co.rect, box->string); - rn_draw_string(rn_state.main_font, text_pos, co.text_color, box->string); + rn_draw_string(rn->main_font, text_pos, co.text_color, box->string); } } @@ -452,7 +459,7 @@ fn ui_signal_t ui_text_input(ui_code_loc_t loc, ui_text_input_t *ti) { } } } - v2f32_t size = rn_measure_string(rn_state.main_font, s8_lit("_")); + v2f32_t size = rn_measure_string(rn->main_font, s8_lit("_")); v2f32_t pos = v2f32_sub(ev->mouse_pos, box->final_rect.min); i32 p = (i32)f32_round(pos.x / size.x); if (ev->kind == app_event_kind_mouse_down && ev->mouse_button == app_mouse_button_left) { @@ -468,7 +475,7 @@ fn ui_signal_t ui_text_input(ui_code_loc_t loc, ui_text_input_t *ti) { #define ui_button(...) ui__button(UILOC, __VA_ARGS__) fn ui_signal_t ui__button(ui_code_loc_t loc, char *str, ...) { - S8_FMT(tcx.temp, str, string); + S8_FMT(tcx->temp, str, string); ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_border = true, .draw_rect = true, .draw_text = true, .keyboard_nav = true }, string); ui_signal_t signal = ui_signal_from_box(box); return signal; @@ -476,7 +483,7 @@ fn ui_signal_t ui__button(ui_code_loc_t loc, char *str, ...) { #define ui_radio_button(...) ui__radio_button(UILOC, __VA_ARGS__) fn ui_signal_t ui__radio_button(ui_code_loc_t loc, i32 *value, i32 value_clicked, char *str, ...) { - S8_FMT(tcx.temp, str, string); + S8_FMT(tcx->temp, str, string); ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_border = true, .draw_rect = true, .draw_text = true, .keyboard_nav = true }, string); ui_signal_t signal = ui_signal_from_box(box); if (signal.clicked) *value = value_clicked; @@ -487,7 +494,7 @@ fn ui_signal_t ui__radio_button(ui_code_loc_t loc, i32 *value, i32 value_clicked #define ui_label_button(...) ui__label_button(UILOC, __VA_ARGS__) fn ui_signal_t ui__label_button(ui_code_loc_t loc, char *str, ...) { - S8_FMT(tcx.temp, str, string); + S8_FMT(tcx->temp, str, string); ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_text = true, .keyboard_nav = true }, string); ui_signal_t signal = ui_signal_from_box(box); return signal; @@ -495,7 +502,7 @@ fn ui_signal_t ui__label_button(ui_code_loc_t loc, char *str, ...) { #define ui_label(...) ui__label(UILOC, __VA_ARGS__) fn ui_box_t *ui__label(ui_code_loc_t loc, char *str, ...) { - S8_FMT(tcx.temp, str, string); + S8_FMT(tcx->temp, str, string); ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_text = true }, string); return box; } @@ -503,14 +510,14 @@ fn ui_box_t *ui__label(ui_code_loc_t loc, char *str, ...) { #define ui_expander(...) defer_if (ui_begin_expander(__VA_ARGS__).clicked, ui_end_expander()) #define ui_begin_expander(...) ui__begin_expander(UILOC, __VA_ARGS__) fn ui_signal_t ui__begin_expander(ui_code_loc_t loc, char *str, ...) { - S8_FMT(tcx.temp, str, string); + S8_FMT(tcx->temp, str, string); ui_box_t *box = ui_build_box_from_string(loc, (ui_box_flags_t){ .draw_text = true, .keyboard_nav = true }, string); if (box->created_new) box->expanded = true; ui_signal_t signal = ui_signal_from_box(box); if (signal.clicked) box->expanded = !box->expanded; signal.clicked = box->expanded; - if ( box->expanded) box->string = s8_printf(tcx.temp, "* %S", box->string); - if (!box->expanded) box->string = s8_printf(tcx.temp, "> %S", box->string); + if ( box->expanded) box->string = s8_printf(tcx->temp, "* %S", box->string); + if (!box->expanded) box->string = s8_printf(tcx->temp, "> %S", box->string); if (box->expanded) { ui_push_string_pos_offset(ui_top_string_pos_offset() + ui_em(0.5f)); ui_push_id(box->id); @@ -663,7 +670,8 @@ fn void ui_draw(void) { ui__draw_box(ui->frame, &ui->root); } -fn void ui_begin_frame(app_frame_t *frame) { +fn void ui_begin_frame(ui_t *ui_context, app_frame_t *frame) { + ui = ui_context; ui->frame = frame; } @@ -886,24 +894,24 @@ fn void ui_serial_subtype(void *p, type_t *type, s8_t name) { fn void ui_serial_type(void *p, type_t *type) { ui_serial_subtype(p, type, type->name); } - +// fn void ui_demo_init(ma_arena_t *arena) { - ui = ma_push_type(arena, ui_t); + tcx->data[1] = ma_push_type(arena, ui_t); + ui = tcx->data[1]; ui->box_arena = ma_push_arena(arena, mib(1)); ui_init_colors(); } gb i32 ui_g_panel = 1; -gb app_event_t ui_test_event; fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_count) { - ui_begin_frame(frame); + ui_begin_frame(tcx->data[1], frame); + rn_begin_frame(tcx->data[0], frame); for (app_event_t *ev = frame->first_event; ev; ev = ev->next) { ui_begin_build(UILOC, ev, window_rect_from_frame(frame)); ui_box_t *top_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true}); ui_set_rect(top_box, r2f32_cut_top(&ui->top->rect, ui_em(1.5f)), ui_lop_cut_top); - ui_set_padding(ui_em(3)) ui_set_text_align(ui_text_align_center) ui_set_lop(ui_lop_cut_left) @@ -950,20 +958,20 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co ui_signal_t signal = ui_label_button("%S: %f##slider%S", tweak->name, *n, tweak->name); if (signal.dragging) { f32 size = tweak->max - tweak->min; - v2f32_t string_size = rn_measure_string(rn_state.main_font, signal.box->string); + v2f32_t string_size = rn_measure_string(rn->main_font, signal.box->string); f32 delta = (signal.drag.x / string_size.x) * size; *n = CLAMP(*n + delta, tweak->min, tweak->max); } } else_is_invalid; } ui_label("allocated boxes: %d", ui->allocated_boxes); - ui_serial_type(&ui_test_event, type(app_event_t)); + // ui_serial_type(&ui_test_event, type(app_event_t)); } locl f32 scroller_value; defer_block(ui_push_top(scroller_box), ui_pop_top()) { f32 item_count = (f32)item_box->node_count; - f32 all_items_size = item_count * rn_state.main_font->size; + f32 all_items_size = item_count * rn->main_font->size; f32 item_box_size = r2f32_get_size(ui->top->full_rect).y; f32 scroller_box_size = r2f32_get_size(scroller_box->full_rect).y; f32 scroller_size = CLAMP(item_box_size / (all_items_size + frame->window_size.y), 0, 1.0f); @@ -974,7 +982,9 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co r2f32_cut_top(&ui->top->rect, scroller_percent * scroller_box_size); ui_box_t *box = ui_build_box_from_id(UILOC, (ui_box_flags_t){.draw_rect = true}, ui_idf("slider")); - box->background_color = v4f32(1,0,1,1); + box->background_color = ui_color_table[ui_color_scroller]; + box->bg_hot_color = ui_color_table[ui_color_scroller_hot]; + box->bg_active_color = ui_color_table[ui_color_scroller_active]; ui_set_rect(box, r2f32_cut_top(&ui->top->rect, scroller_size * scroller_box_size), ui_lop_cut_top); ui_signal_t signal = ui_signal_from_box(box); if (signal.dragging) { @@ -997,7 +1007,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co ui_end_build(); } - rn_begin(frame, white_color); + rn_begin(white_color); ui_draw(); rn_end(); diff --git a/src/ui/ui.gen.c b/src/ui/ui.gen.c index 9a54e14..53d629e 100644 --- a/src/ui/ui.gen.c +++ b/src/ui/ui.gen.c @@ -2,16 +2,19 @@ type_t type__ui_color_t = { type_kind_enum, s8_const_lit("ui_color_t"), sizeof(ui_color_t), .members = (type_member_t[]){ - {.name = s8_const_lit("ui_color_rect_color"), .value = ui_color_rect_color}, - {.name = s8_const_lit("ui_color_rect_hot_color"), .value = ui_color_rect_hot_color}, - {.name = s8_const_lit("ui_color_rect_active_color"), .value = ui_color_rect_active_color}, - {.name = s8_const_lit("ui_color_border_color"), .value = ui_color_border_color}, - {.name = s8_const_lit("ui_color_text_color"), .value = ui_color_text_color}, - {.name = s8_const_lit("ui_color_text_hot_color"), .value = ui_color_text_hot_color}, - {.name = s8_const_lit("ui_color_text_active_color"), .value = ui_color_text_active_color}, - {.name = s8_const_lit("ui_color_focused_rect_color"), .value = ui_color_focused_rect_color}, + {.name = s8_const_lit("ui_color_rect"), .value = ui_color_rect}, + {.name = s8_const_lit("ui_color_rect_hot"), .value = ui_color_rect_hot}, + {.name = s8_const_lit("ui_color_rect_active"), .value = ui_color_rect_active}, + {.name = s8_const_lit("ui_color_border"), .value = ui_color_border}, + {.name = s8_const_lit("ui_color_text"), .value = ui_color_text}, + {.name = s8_const_lit("ui_color_text_hot"), .value = ui_color_text_hot}, + {.name = s8_const_lit("ui_color_text_active"), .value = ui_color_text_active}, + {.name = s8_const_lit("ui_color_focused_rect"), .value = ui_color_focused_rect}, + {.name = s8_const_lit("ui_color_scroller"), .value = ui_color_scroller}, + {.name = s8_const_lit("ui_color_scroller_hot"), .value = ui_color_scroller_hot}, + {.name = s8_const_lit("ui_color_scroller_active"), .value = ui_color_scroller_active}, }, - .count = 8, + .count = 11, }; gb v4f32_t ui_color_table[] = { {0}, @@ -22,71 +25,77 @@ gb v4f32_t ui_color_table[] = { {0}, {0}, {0}, +{0}, +{0}, +{0}, }; fn void ui_init_colors(void) { -ui_color_table[ui_color_rect_color] = v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.95f, 1.0f}); -ui_color_table[ui_color_rect_hot_color] = v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.5f, 0.95f, 1.0f}); -ui_color_table[ui_color_rect_active_color] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.5f, 0.95f, 1.0f}); -ui_color_table[ui_color_border_color] = v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.70f, 1.0f}); -ui_color_table[ui_color_text_color] = v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.70f, 1.0f}); -ui_color_table[ui_color_text_hot_color] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.4f, 0.70f, 1.0f}); -ui_color_table[ui_color_text_active_color] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.5f, 0.70f, 1.0f}); -ui_color_table[ui_color_focused_rect_color] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.95f, 1.0f}); +ui_color_table[ui_color_rect] = v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.95f, 1.0f}); +ui_color_table[ui_color_rect_hot] = v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.5f, 0.95f, 1.0f}); +ui_color_table[ui_color_rect_active] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.5f, 0.95f, 1.0f}); +ui_color_table[ui_color_border] = v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.70f, 1.0f}); +ui_color_table[ui_color_text] = v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.70f, 1.0f}); +ui_color_table[ui_color_text_hot] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.4f, 0.70f, 1.0f}); +ui_color_table[ui_color_text_active] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.5f, 0.70f, 1.0f}); +ui_color_table[ui_color_focused_rect] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.95f, 1.0f}); +ui_color_table[ui_color_scroller] = ui_color_table[ui_color_text]; +ui_color_table[ui_color_scroller_hot] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.2f, 0.7f, 1.0f}); +ui_color_table[ui_color_scroller_active] = v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.2f, 0.5f, 1.0f}); } -fn void ui_push_id(ui_id_t v) { ui_id_node_t *n = ma_push_type(tcx.temp, ui_id_node_t); n->value = v; SLLS_PUSH(ui->id_stack, n); } +fn void ui_push_id(ui_id_t v) { ui_id_node_t *n = ma_push_type(tcx->temp, ui_id_node_t); n->value = v; SLLS_PUSH(ui->id_stack, n); } fn void ui_pop_id(void) { SLLS_POP(ui->id_stack); } fn ui_id_t ui_top_id(void) { return ui->id_stack->value; } #define ui_set_id(x) defer_block(ui_push_id(x), ui_pop_id()) -fn void ui_push_lop(ui_lop_t v) { ui_lop_node_t *n = ma_push_type(tcx.temp, ui_lop_node_t); n->value = v; SLLS_PUSH(ui->lop_stack, n); } +fn void ui_push_lop(ui_lop_t v) { ui_lop_node_t *n = ma_push_type(tcx->temp, ui_lop_node_t); n->value = v; SLLS_PUSH(ui->lop_stack, n); } fn void ui_pop_lop(void) { SLLS_POP(ui->lop_stack); } fn ui_lop_t ui_top_lop(void) { return ui->lop_stack->value; } #define ui_set_lop(x) defer_block(ui_push_lop(x), ui_pop_lop()) -fn void ui_push_border_thickness(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->border_thickness_stack, n); } +fn void ui_push_border_thickness(f32 v) { ui_f32_node_t *n = ma_push_type(tcx->temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->border_thickness_stack, n); } fn void ui_pop_border_thickness(void) { SLLS_POP(ui->border_thickness_stack); } fn f32 ui_top_border_thickness(void) { return ui->border_thickness_stack->value; } #define ui_set_border_thickness(x) defer_block(ui_push_border_thickness(x), ui_pop_border_thickness()) -fn void ui_push_text_align(ui_text_align_t v) { ui_text_align_node_t *n = ma_push_type(tcx.temp, ui_text_align_node_t); n->value = v; SLLS_PUSH(ui->text_align_stack, n); } +fn void ui_push_text_align(ui_text_align_t v) { ui_text_align_node_t *n = ma_push_type(tcx->temp, ui_text_align_node_t); n->value = v; SLLS_PUSH(ui->text_align_stack, n); } fn void ui_pop_text_align(void) { SLLS_POP(ui->text_align_stack); } fn ui_text_align_t ui_top_text_align(void) { return ui->text_align_stack->value; } #define ui_set_text_align(x) defer_block(ui_push_text_align(x), ui_pop_text_align()) -fn void ui_push_required_size(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->required_size_stack, n); } +fn void ui_push_required_size(f32 v) { ui_f32_node_t *n = ma_push_type(tcx->temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->required_size_stack, n); } fn void ui_pop_required_size(void) { SLLS_POP(ui->required_size_stack); } fn f32 ui_top_required_size(void) { return ui->required_size_stack->value; } #define ui_set_required_size(x) defer_block(ui_push_required_size(x), ui_pop_required_size()) -fn void ui_push_padding(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->padding_stack, n); } +fn void ui_push_padding(f32 v) { ui_f32_node_t *n = ma_push_type(tcx->temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->padding_stack, n); } fn void ui_pop_padding(void) { SLLS_POP(ui->padding_stack); } fn f32 ui_top_padding(void) { return ui->padding_stack->value; } #define ui_set_padding(x) defer_block(ui_push_padding(x), ui_pop_padding()) -fn void ui_push_string_pos_offset(f32 v) { ui_f32_node_t *n = ma_push_type(tcx.temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->string_pos_offset_stack, n); } +fn void ui_push_string_pos_offset(f32 v) { ui_f32_node_t *n = ma_push_type(tcx->temp, ui_f32_node_t); n->value = v; SLLS_PUSH(ui->string_pos_offset_stack, n); } fn void ui_pop_string_pos_offset(void) { SLLS_POP(ui->string_pos_offset_stack); } fn f32 ui_top_string_pos_offset(void) { return ui->string_pos_offset_stack->value; } #define ui_set_string_pos_offset(x) defer_block(ui_push_string_pos_offset(x), ui_pop_string_pos_offset()) -fn void ui_push_background_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->background_color_stack, n); } +fn void ui_push_background_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx->temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->background_color_stack, n); } fn void ui_pop_background_color(void) { SLLS_POP(ui->background_color_stack); } fn v4f32_t ui_top_background_color(void) { return ui->background_color_stack->value; } #define ui_set_background_color(x) defer_block(ui_push_background_color(x), ui_pop_background_color()) -fn void ui_push_bg_hot_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->bg_hot_color_stack, n); } +fn void ui_push_bg_hot_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx->temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->bg_hot_color_stack, n); } fn void ui_pop_bg_hot_color(void) { SLLS_POP(ui->bg_hot_color_stack); } fn v4f32_t ui_top_bg_hot_color(void) { return ui->bg_hot_color_stack->value; } #define ui_set_bg_hot_color(x) defer_block(ui_push_bg_hot_color(x), ui_pop_bg_hot_color()) -fn void ui_push_bg_active_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->bg_active_color_stack, n); } +fn void ui_push_bg_active_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx->temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->bg_active_color_stack, n); } fn void ui_pop_bg_active_color(void) { SLLS_POP(ui->bg_active_color_stack); } fn v4f32_t ui_top_bg_active_color(void) { return ui->bg_active_color_stack->value; } #define ui_set_bg_active_color(x) defer_block(ui_push_bg_active_color(x), ui_pop_bg_active_color()) -fn void ui_push_border_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->border_color_stack, n); } +fn void ui_push_border_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx->temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->border_color_stack, n); } fn void ui_pop_border_color(void) { SLLS_POP(ui->border_color_stack); } fn v4f32_t ui_top_border_color(void) { return ui->border_color_stack->value; } #define ui_set_border_color(x) defer_block(ui_push_border_color(x), ui_pop_border_color()) -fn void ui_push_text_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->text_color_stack, n); } +fn void ui_push_text_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx->temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->text_color_stack, n); } fn void ui_pop_text_color(void) { SLLS_POP(ui->text_color_stack); } fn v4f32_t ui_top_text_color(void) { return ui->text_color_stack->value; } #define ui_set_text_color(x) defer_block(ui_push_text_color(x), ui_pop_text_color()) -fn void ui_push_text_hot_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->text_hot_color_stack, n); } +fn void ui_push_text_hot_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx->temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->text_hot_color_stack, n); } fn void ui_pop_text_hot_color(void) { SLLS_POP(ui->text_hot_color_stack); } fn v4f32_t ui_top_text_hot_color(void) { return ui->text_hot_color_stack->value; } #define ui_set_text_hot_color(x) defer_block(ui_push_text_hot_color(x), ui_pop_text_hot_color()) -fn void ui_push_text_active_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->text_active_color_stack, n); } +fn void ui_push_text_active_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx->temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->text_active_color_stack, n); } fn void ui_pop_text_active_color(void) { SLLS_POP(ui->text_active_color_stack); } fn v4f32_t ui_top_text_active_color(void) { return ui->text_active_color_stack->value; } #define ui_set_text_active_color(x) defer_block(ui_push_text_active_color(x), ui_pop_text_active_color()) @@ -113,13 +122,13 @@ ui_push_lop(ui_lop_cut_top); ui_push_border_thickness(1.0f); ui_push_text_align(ui_text_align_left); ui_push_string_pos_offset(0); -ui_push_background_color(ui_color_table[ui_color_rect_color]); -ui_push_bg_hot_color(ui_color_table[ui_color_rect_hot_color]); -ui_push_bg_active_color(ui_color_table[ui_color_rect_active_color]); -ui_push_border_color(ui_color_table[ui_color_border_color]); -ui_push_text_color(ui_color_table[ui_color_text_color]); -ui_push_text_hot_color(ui_color_table[ui_color_text_hot_color]); -ui_push_text_active_color(ui_color_table[ui_color_text_active_color]); +ui_push_background_color(ui_color_table[ui_color_rect]); +ui_push_bg_hot_color(ui_color_table[ui_color_rect_hot]); +ui_push_bg_active_color(ui_color_table[ui_color_rect_active]); +ui_push_border_color(ui_color_table[ui_color_border]); +ui_push_text_color(ui_color_table[ui_color_text]); +ui_push_text_hot_color(ui_color_table[ui_color_text_hot]); +ui_push_text_active_color(ui_color_table[ui_color_text_active]); } fn void ui_pop_init_values(void) { ui_pop_id(); diff --git a/src/ui/ui.gen.h b/src/ui/ui.gen.h index 98e1e94..1cb9956 100644 --- a/src/ui/ui.gen.h +++ b/src/ui/ui.gen.h @@ -1,13 +1,16 @@ // automatically generated using: C:\dev\wasm\src/ui/ui.meta.c typedef enum { -ui_color_rect_color, -ui_color_rect_hot_color, -ui_color_rect_active_color, -ui_color_border_color, -ui_color_text_color, -ui_color_text_hot_color, -ui_color_text_active_color, -ui_color_focused_rect_color, +ui_color_rect, +ui_color_rect_hot, +ui_color_rect_active, +ui_color_border, +ui_color_text, +ui_color_text_hot, +ui_color_text_active, +ui_color_focused_rect, +ui_color_scroller, +ui_color_scroller_hot, +ui_color_scroller_active, ui_color_count, } ui_color_t; diff --git a/src/ui/ui.h b/src/ui/ui.h index 470c5a3..4ca9155 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -156,7 +156,7 @@ fn b32 ui_is_focused_box(ui_box_t *box) { return !ui_is_null_box(box) && box->id fn void ui_set_rect(ui_box_t *box, r2f32_t rect, ui_lop_t lop) { box->rect = box->full_rect = rect; box->lop = lop; } #define UILOC (ui_code_loc_t){.file = __FILE__, .line = __LINE__, .counter = __COUNTER__} -#define ui_em(x) ((x) * rn_state.main_font->size) +#define ui_em(x) ((x) * rn->main_font->size) #define ui_max 200000000.f #define ui_box_flags(...) (ui_box_flag_t){__VA_ARGS__} diff --git a/src/ui/ui.meta.c b/src/ui/ui.meta.c index 870ec80..dfd5977 100644 --- a/src/ui/ui.meta.c +++ b/src/ui/ui.meta.c @@ -1,15 +1,17 @@ fn void mt_ui_colors(ma_arena_t *arena, sb8_t *c, sb8_t *h) { ast_t *table = mtt_parse(arena, __FILE__, S8_CODE( - { name color } - { rect_color `v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.95f, 1.0f})` } - { rect_hot_color `v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.5f, 0.95f, 1.0f})` } - { rect_active_color `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.5f, 0.95f, 1.0f})` } - { border_color `v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.70f, 1.0f})` } - { text_color `v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.70f, 1.0f})` } - { text_hot_color `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.4f, 0.70f, 1.0f})` } - { text_active_color `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.5f, 0.70f, 1.0f})` } - { focused_rect_color `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.95f, 1.0f})` } - { scroller_color `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.95f, 1.0f})` } + { name color } + { rect `v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.95f, 1.0f})` } + { rect_hot `v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.5f, 0.95f, 1.0f})` } + { rect_active `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.5f, 0.95f, 1.0f})` } + { border `v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.70f, 1.0f})` } + { text `v4f32_hsla_to_rgba((v4f32_t){0.0f, 0.2f, 0.70f, 1.0f})` } + { text_hot `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.4f, 0.70f, 1.0f})` } + { text_active `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.5f, 0.70f, 1.0f})` } + { focused_rect `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.6f, 0.95f, 1.0f})` } + { scroller `ui_color_table[ui_color_text]` } + { scroller_hot `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.2f, 0.7f, 1.0f})` } + { scroller_active `v4f32_hsla_to_rgba((v4f32_t){0.1f, 0.2f, 0.5f, 1.0f})` } )); mtt_serial_enum(c, h, table, s8_lit("ui_color")); @@ -38,13 +40,13 @@ fn void mt_ui_stacks(ma_arena_t *arena, sb8_t *c, sb8_t *h) { { f32 required_size 0 x } { f32 padding 0 x } { f32 string_pos_offset 0 0 } - { v4f32_t background_color 0 `ui_color_table[ui_color_rect_color]` } - { v4f32_t bg_hot_color 0 `ui_color_table[ui_color_rect_hot_color]` } - { v4f32_t bg_active_color 0 `ui_color_table[ui_color_rect_active_color]` } - { v4f32_t border_color 0 `ui_color_table[ui_color_border_color]` } - { v4f32_t text_color 0 `ui_color_table[ui_color_text_color]` } - { v4f32_t text_hot_color 0 `ui_color_table[ui_color_text_hot_color]` } - { v4f32_t text_active_color 0 `ui_color_table[ui_color_text_active_color]` } + { v4f32_t background_color 0 `ui_color_table[ui_color_rect]` } + { v4f32_t bg_hot_color 0 `ui_color_table[ui_color_rect_hot]` } + { v4f32_t bg_active_color 0 `ui_color_table[ui_color_rect_active]` } + { v4f32_t border_color 0 `ui_color_table[ui_color_border]` } + { v4f32_t text_color 0 `ui_color_table[ui_color_text]` } + { v4f32_t text_hot_color 0 `ui_color_table[ui_color_text_hot]` } + { v4f32_t text_active_color 0 `ui_color_table[ui_color_text_active]` } )); /////////////////////////////// @@ -98,7 +100,7 @@ fn void mt_ui_stacks(ma_arena_t *arena, sb8_t *c, sb8_t *h) { /////////////////////////////// // generate stack functions for (ast_t *it = table->first; it; it = it->next) { - mt_sbprintf(c, it, "fn void ui_push_@name(@type v) { @node *n = ma_push_type(tcx.temp, @node); n->value = v; SLLS_PUSH(ui->@stack, n); }\n" + mt_sbprintf(c, it, "fn void ui_push_@name(@type v) { @node *n = ma_push_type(tcx->temp, @node); n->value = v; SLLS_PUSH(ui->@stack, n); }\n" "fn void ui_pop_@name(void) { SLLS_POP(ui->@stack); }\n" "fn @type ui_top_@name(void) { return ui->@stack->value; }\n" "#define ui_set_@name(x) defer_block(ui_push_@name(x), ui_pop_@name())\n"); diff --git a/src/wasm_app/main.c b/src/wasm_app/main.c index b56ab22..53dfdba 100644 --- a/src/wasm_app/main.c +++ b/src/wasm_app/main.c @@ -1,25 +1,49 @@ #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 "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) { - run_all_tests(); - ma_arena_t *perm = &tcx._perm; - mt_tweak_f32(font_size, 50, 4, 200); - mt_tweak_f32(_font_size, 50, 50, 50); - rn_init(perm, font_size, dpr); - ui_demo_init(perm); } -fn b32 app_update(app_frame_t *frame) { +fn_export b32 app_update(app_ctx_t *app_ctx, app_frame_t *frame) { + tcx = app_ctx->tcx; + 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); + 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(); + + return true; + } else if (frame->first_event->kind == app_event_kind_unload) { + return true; + } + if (!f32_are_equal(font_size, _font_size)) { _font_size = font_size; rn_reload_font(font_size, frame->dpr); @@ -27,3 +51,4 @@ fn b32 app_update(app_frame_t *frame) { ui_demo_update(frame, tweak_table, lengthof(tweak_table)); return true; } +// \ No newline at end of file diff --git a/src/wasm_app/win32_exe.c b/src/wasm_app/win32_exe.c new file mode 100644 index 0000000..c0e320e --- /dev/null +++ b/src/wasm_app/win32_exe.c @@ -0,0 +1,5 @@ +#include "core/core_inc.h" +#include "app/app.h" + +#include "core/core_inc.c" +#include "app/app.c" diff --git a/todo.txt b/todo.txt index b3278ee..ffe7c2a 100644 --- a/todo.txt +++ b/todo.txt @@ -50,6 +50,7 @@ [ ] ini format [x] remove dates and time from core [ ] meta + [ ] parser errors should not point to location of fatalf call but to the code!!!!!!!!! [x] prototype something like templates, readable string generation [x] search for python snippets and execute meta.py script on that file [x] simplify logging!!!!