diff --git a/build_file.c b/build_file.c index 01210b6..c7ab9be 100644 --- a/build_file.c +++ b/build_file.c @@ -36,7 +36,7 @@ int main(int argc, char **argv) { b32 run_server = false; b32 core_test_target = true; b32 win32_target = true; - b32 wasm_target = false; + b32 wasm_target = true; if (run_server) { os_systemf("start /D ..\\package ..\\package\\run_server.bat"); @@ -70,18 +70,24 @@ int main(int argc, char **argv) { } - if (wasm_target && cache_code_modified(s8_lit("../src/wasm_app/main.c"), s8_lit("main.wasm"))) { - os_copy("../src/app/app_wasm.html", "../package/index.html", os_copy_overwrite); - ok = os_systemf( - "clang.exe ../src/wasm_app/main.c -o main.wasm" - " -Oz -g -I../src" - " -Wall -Wno-missing-braces" - " -fdiagnostics-absolute-paths -fdiagnostics-format=msvc" - " --target=wasm32 -nostdlib -mbulk-memory -msimd128" - " -Wl,-export-dynamic,--allow-undefined,--import-memory,--no-entry,--initial-memory=131072000,--max-memory=4294967296" - ); - os_copy("main.wasm", "../package/main.wasm", os_copy_overwrite); - if (ok != 0) return ok; + if (wasm_target) { + b32 html_code_modified = cache_code_modified(s8_lit("../src/app/app_wasm.html"), s8_lit("../package/index.html")); + b32 wasm_code_modified = cache_code_modified(s8_lit("../src/wasm_app/main.c"), s8_lit("main.wasm")); + if (html_code_modified) { + os_copy("../src/app/app_wasm.html", "../package/index.html", os_copy_overwrite); + } + if (wasm_code_modified) { + ok = os_systemf( + "clang.exe ../src/wasm_app/main.c -o main.wasm" + " -Oz -g -I../src" + " -Wall -Wno-missing-braces -Wno-single-bit-bitfield-constant-conversion" + " -fdiagnostics-absolute-paths -fdiagnostics-format=msvc" + " --target=wasm32 -nostdlib -mbulk-memory -msimd128" + " -Wl,-export-dynamic,--allow-undefined,--import-memory,--no-entry,--initial-memory=131072000,--max-memory=4294967296" + ); + os_copy("main.wasm", "../package/main.wasm", os_copy_overwrite); + if (ok != 0) return ok; + } } cache_save(); diff --git a/package/index.html b/package/index.html index fd36878..c79db26 100644 --- a/package/index.html +++ b/package/index.html @@ -99,7 +99,6 @@ class memory_t { const canvas = document.getElementById("canvas"); const ctx2d = canvas.getContext('2d'); -const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 65536 })); (async function main() { if (!ctx2d) { @@ -107,6 +106,7 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655 return; } + const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 65536 })); const request = await fetch('main.wasm'); const binary = await request.arrayBuffer(); @@ -139,7 +139,6 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655 }; let wasm_app_imports = { - // core memory: mem.mem, wasm_parse_float: (str, len) => { return parseFloat(mem.read_cstr(str, len)); }, @@ -192,7 +191,7 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655 mem.exports = wasm_exports; wasm_exports['wasm_init'](); - let awake = false; + let awake = true; function wasm_update(time) { const dpr = window.devicePixelRatio; diff --git a/src/app/app_wasm.c b/src/app/app_wasm.c index a523b88..32b1e21 100644 --- a/src/app/app_wasm.c +++ b/src/app/app_wasm.c @@ -1,95 +1,78 @@ -/* - -.doesn't miss events (always processes all key strokes and buttons etc.) -.sleeps properly when nothing is happening -.replayable (that is you can serialize all the events, replay them back and get the same results) -..rendering and update decoupled -..animations probably should be in the rendering part - -void app_update(app_event_t *events, i32 event_count) { - update_result_t *state = NULL; // contains event - loop (events) { - state = update(it); - } - - { - app_event_t *ev = state->ev; - - - f64 delta_time = app_anim_get_delta_time(); - f64 time = app_anim_get_time(); - animate(state, delta_time, time); - render(state); - } -} - - -*/ gb_wasm_export char wasm_temp_buff1[128] = {[127] = 0x13}; gb_wasm_export i32 wasm_temp_buff1_len = 127; gb_wasm_export char wasm_temp_buff2[128] = {[127] = 0x13}; gb_wasm_export i32 wasm_temp_buff2_len = 127; -gb f64 wasm_dpr; -gb f64 wasm_delta_time; -gb f64 wasm_time; -gb f64 wasm_last_time; -gb app_event_list_t wasm_event_list; +gb f32 wasm_dpr; +gb f32 wasm_delta_time; +gb f32 wasm_time; +gb f32 wasm_last_time; +gb app_frame_t wasm_frame; +gb u64 wasm_frame_counter; typedef struct wasm_cached_t wasm_cached_t; struct wasm_cached_t { - v2f64_t mouse_pos; - b8 ctrl, alt, meta, shift; + v2f32_t mouse_pos; + v2f32_t mouse_delta; + b8 ctrl, alt, shift; } wasm_cached; -fn b32 app_update(app_event_list_t); -fn void app_init(void); +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); *ev = event; ev->alt = wasm_cached.alt; ev->ctrl = wasm_cached.ctrl; - ev->meta = wasm_cached.meta; ev->shift = wasm_cached.shift; ev->mouse_pos = wasm_cached.mouse_pos; - SLLQ_APPEND(wasm_event_list.first, wasm_event_list.last, ev); - wasm_event_list.len += 1; + ev->mouse_delta = wasm_cached.mouse_delta; + SLLQ_APPEND(wasm_frame.first_event, wasm_frame.last_event, ev); + wasm_frame.event_count += 1; } -fn_wasm_export void wasm_mouse_move(f64 x, f64 y, b32 ctrl, b32 shift, b32 alt, b32 meta) { - wasm_cached.mouse_pos = (v2f64_t){x, y}; - wasm_cached.ctrl = ctrl; wasm_cached.alt = alt; wasm_cached.meta = meta; wasm_cached.shift = shift; - wasm_add_event((app_event_t){ - .kind = app_event_kind_mouse_move, - }); +fn void wasm_set_cached_mouse(v2f32_t p) { + wasm_cached.mouse_delta = v2f32_sub(p, wasm_cached.mouse_pos); + wasm_cached.mouse_pos = p; } -fn_wasm_export void wasm_mouse_down(f64 x, f64 y, i32 button, b32 ctrl, b32 shift, b32 alt, b32 meta) { +fn void wasm_set_cached_buttons(b32 ctrl, b32 shift, b32 alt) { + wasm_cached.ctrl = ctrl; + wasm_cached.alt = alt; + wasm_cached.shift = shift; +} + +fn_wasm_export void wasm_mouse_move(f32 x, f32 y, b32 ctrl, b32 shift, b32 alt, b32 meta) { + wasm_set_cached_mouse((v2f32_t){x, y}); + wasm_set_cached_buttons(ctrl, shift, alt); +} + +fn_wasm_export void wasm_mouse_down(f32 x, f32 y, i32 button, b32 ctrl, b32 shift, b32 alt, b32 meta) { button += 1; assert(button >= app_mouse_button_left && button <= app_mouse_button_right); - wasm_cached.ctrl = ctrl; wasm_cached.alt = alt; wasm_cached.meta = meta; wasm_cached.shift = shift; - wasm_cached.mouse_pos = (v2f64_t){x, y}; + wasm_set_cached_buttons(ctrl, shift, alt); + wasm_set_cached_mouse((v2f32_t){x, y}); wasm_add_event((app_event_t){ .kind = app_event_kind_mouse_down, .mouse_button = button, }); } -fn_wasm_export void wasm_mouse_up(f64 x, f64 y, i32 button, b32 ctrl, b32 shift, b32 alt, b32 meta) { +fn_wasm_export void wasm_mouse_up(f32 x, f32 y, i32 button, b32 ctrl, b32 shift, b32 alt, b32 meta) { button += 1; assert(button >= app_mouse_button_left && button <= app_mouse_button_right); - wasm_cached.ctrl = ctrl; wasm_cached.alt = alt; wasm_cached.meta = meta; wasm_cached.shift = shift; - wasm_cached.mouse_pos = (v2f64_t){x, y}; + wasm_set_cached_buttons(ctrl, shift, alt); + wasm_set_cached_mouse((v2f32_t){x, y}); wasm_add_event((app_event_t){ .kind = app_event_kind_mouse_up, .mouse_button = button, }); } -fn_wasm_export void wasm_mouse_wheel(f64 x, f64 y, f64 delta_x, f64 delta_y, f64 delta_z, b32 ctrl, b32 shift, b32 alt, b32 meta) { - wasm_cached.mouse_pos = (v2f64_t){x, y}; - wasm_cached.ctrl = ctrl; wasm_cached.alt = alt; wasm_cached.meta = meta; wasm_cached.shift = shift; +fn_wasm_export void wasm_mouse_wheel(f32 x, f32 y, f32 delta_x, f32 delta_y, f32 delta_z, b32 ctrl, b32 shift, b32 alt, b32 meta) { + wasm_set_cached_mouse((v2f32_t){x, y}); + wasm_set_cached_buttons(ctrl, shift, alt); wasm_add_event((app_event_t){ .kind = app_event_kind_mouse_wheel, .mouse_wheel_delta = {delta_x, delta_y, delta_z}, @@ -97,7 +80,7 @@ fn_wasm_export void wasm_mouse_wheel(f64 x, f64 y, f64 delta_x, f64 delta_y, f64 } fn_wasm_export void wasm_key_down(char *key, b32 ctrl, b32 shift, b32 alt, b32 meta) { - wasm_cached.ctrl = ctrl; wasm_cached.alt = alt; wasm_cached.meta = meta; wasm_cached.shift = shift; + wasm_set_cached_buttons(ctrl, shift, alt); assert(wasm_temp_buff1[127] == 0x13); // make sure we didn't overwrite memory in JS assert(wasm_temp_buff2[127] == 0x13); s8_t key8 = s8_from_char(key); @@ -122,7 +105,7 @@ fn_wasm_export void wasm_key_down(char *key, b32 ctrl, b32 shift, b32 alt, b32 m } fn_wasm_export void wasm_key_up(char *key, b32 ctrl, b32 shift, b32 alt, b32 meta) { - wasm_cached.ctrl = ctrl; wasm_cached.alt = alt; wasm_cached.meta = meta; wasm_cached.shift = shift; + wasm_set_cached_buttons(ctrl, shift, alt); assert(wasm_temp_buff1[127] == 0x13); // make sure we didn't overwrite memory in JS assert(wasm_temp_buff2[127] == 0x13); s8_t key8 = s8_from_char(key); @@ -136,40 +119,36 @@ fn_wasm_export void wasm_key_up(char *key, b32 ctrl, b32 shift, b32 alt, b32 met } } -fn_wasm_export b32 wasm_update(f64 time, f64 width, f64 height, f64 dpr) { +fn_wasm_export b32 wasm_update(f64 time, f32 width, f32 height, f32 dpr) { wasm_time = time; wasm_delta_time = wasm_time - wasm_last_time; - - v2f64_t window_size = (v2f64_t){width / dpr, height / dpr}; wasm_dpr = dpr; - if (wasm_event_list.first == NULL) { - wasm_add_event((app_event_t){.kind = app_event_kind_update}); - } - - for (app_event_t *ev = wasm_event_list.first; ev; ev = ev->next) { - ev->dpr = dpr; - ev->window_size = window_size; - } - - b32 animating = app_update(wasm_event_list); + if (wasm_frame.first_event == NULL) wasm_add_event((app_event_t){.kind = app_event_kind_update}); + wasm_frame.window_size = (v2f32_t){width / dpr, height / dpr}; + wasm_frame.dpr = wasm_dpr; + wasm_frame.mouse_pos = wasm_frame.last_event->mouse_pos; + wasm_frame.delta = wasm_delta_time; + wasm_frame.frame = wasm_frame_counter; + b32 animating = app_update(&wasm_frame); ma_set0(tcx.temp); - zero_struct(&wasm_event_list); + zero_struct(&wasm_frame); wasm_last_time = wasm_time; + wasm_frame_counter += 1; return animating; } fn_wasm_export void wasm_init(void) { core_init(); - app_init(); + app_init(1.0); //@todo: dpr } -fn f64 app_get_anim_time(void) { +fn f32 app_get_anim_time(void) { return wasm_time / 1000.0; } -fn f64 app_get_anim_delta_time(void) { +fn f32 app_get_anim_delta_time(void) { return wasm_delta_time / 1000.0; } \ No newline at end of file diff --git a/src/app/app_wasm.html b/src/app/app_wasm.html index fd36878..c79db26 100644 --- a/src/app/app_wasm.html +++ b/src/app/app_wasm.html @@ -99,7 +99,6 @@ class memory_t { const canvas = document.getElementById("canvas"); const ctx2d = canvas.getContext('2d'); -const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 65536 })); (async function main() { if (!ctx2d) { @@ -107,6 +106,7 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655 return; } + const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 65536 })); const request = await fetch('main.wasm'); const binary = await request.arrayBuffer(); @@ -139,7 +139,6 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655 }; let wasm_app_imports = { - // core memory: mem.mem, wasm_parse_float: (str, len) => { return parseFloat(mem.read_cstr(str, len)); }, @@ -192,7 +191,7 @@ const mem = new memory_t(new WebAssembly['Memory']({ initial: 2000, maximum: 655 mem.exports = wasm_exports; wasm_exports['wasm_init'](); - let awake = false; + let awake = true; function wasm_update(time) { const dpr = window.devicePixelRatio; diff --git a/src/core/core_log.c b/src/core/core_log.c index dc5d66a..03638ab 100644 --- a/src/core/core_log.c +++ b/src/core/core_log.c @@ -23,8 +23,7 @@ fn void default_log_proc(log_level_t level, s8_t file_and_line, s8_t string) { } break; case log_level_fatal: { os_error_box(s8_printf(scratch.arena, "%S: fatal error: %S\n", file_and_line, string).str); - if (tcx.log.break_on_fatal) debug_break(); - exit(1); + debug_break(); } break; default_is_invalid; } diff --git a/src/render/font.c b/src/render/render_font.c similarity index 100% rename from src/render/font.c rename to src/render/render_font.c diff --git a/src/render/render_inc.c b/src/render/render_inc.c new file mode 100644 index 0000000..deecd11 --- /dev/null +++ b/src/render/render_inc.c @@ -0,0 +1,18 @@ +#define STB_TRUETYPE_IMPLEMENTATION +#define STBTT_ifloor(x) ((int)f64_floor(x)) +#define STBTT_iceil(x) ((int)f64_ceil(x)) +#define STBTT_sqrt(x) (f64_sqrt(x)) +#define STBTT_pow(x,y) (f64_pow(x,y)) +#define STBTT_fmod(x,y) (f64_mod(x,y)) +#define STBTT_cos(x) (f64_cos(x)) +#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_free(x,u) +#define STBTT_strlen(x) (str_len(x)) +#define STBTT_memcpy memory_copy +#define STBTT_memset memory_set +#include "render/stb_truetype.h" +#include "render/render_font.c" +#include "render/render_opengl.c" \ No newline at end of file diff --git a/src/wasm_app/main.c b/src/wasm_app/main.c index ea82520..cd53cc6 100644 --- a/src/wasm_app/main.c +++ b/src/wasm_app/main.c @@ -4,45 +4,29 @@ #include "core/core_inc.c" #include "app/app.c" +// #include "render/render_inc.c" -#define STB_TRUETYPE_IMPLEMENTATION -#define STBTT_ifloor(x) ((int)f64_floor(x)) -#define STBTT_iceil(x) ((int)f64_ceil(x)) -#define STBTT_sqrt(x) (f64_sqrt(x)) -#define STBTT_pow(x,y) (f64_pow(x,y)) -#define STBTT_fmod(x,y) (f64_mod(x,y)) -#define STBTT_cos(x) (f64_cos(x)) -#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_free(x,u) -#define STBTT_strlen(x) (str_len(x)) -#define STBTT_memcpy memory_copy -#define STBTT_memset memory_set -#include "render/stb_truetype.h" -#include "render/font.c" -#include "render/render_opengl.c" #include "wasm_app.gen.c" -#include "ui/ui_iter.c" -#include "ui/ui.gen.c" -#include "ui/ui.c" +// #include "ui/ui_iter.c" +// #include "ui/ui.gen.c" +// #include "ui/ui.c" fn void app_init(f32 dpr) { ma_arena_t *perm = &tcx._perm; + unused(perm); mt_embed_file(font_data, "package/liberation-mono.ttf"); mt_tweak_f32(font_size, 50, 4, 200); mt_tweak_f32(_font_size, 50, 50, 50); - rn_init(perm, font_data, font_size * dpr); - ui_demo_init(perm); + // rn_init(perm, font_data, font_size * dpr); + // ui_demo_init(perm); } fn b32 app_update(app_frame_t *frame) { if (!f32_are_equal(font_size, _font_size)) { _font_size = font_size; - rn_reload_font(font_data, font_size); + // rn_reload_font(font_data, font_size); } - ui_demo_update(frame); + // ui_demo_update(frame); return true; }