This commit is contained in:
krzosa
2024-12-31 14:48:19 +01:00
parent 3f9f90466b
commit 05d49eefe8
19 changed files with 262 additions and 195 deletions

View File

@@ -1,13 +1,51 @@
/*
.doesn't miss events (always processes all key strokes and buttons etc.)
.replayable / deterministic (that is you can serialize all the events, replay them back and get the same results)
..rendering and update decoupled
.sleeps properly when nothing is happening
.animations probably then should be in the rendering part
void app_update(app_event_t *events, i32 event_count) {
loop (events) update(it);
{
app_event_t *ev = events + event_count - 1;
f64 delta_time = app_anim_get_delta_time();
f64 time = app_anim_get_time();
animate(ev, delta_time, time);
render(ev);
}
}
while (true) {
app_event_t *events = NULL;
if (animating) {
events = poll_events(); // non-blocking
} else {
events = wait_for_events(); // blocking
}
app_update(events, event_count);
SwapBuffers(); // vsync block
}
*/
glb_wasm_export char wasm_temp_buff1[128] = {[127] = 0x13};
glb_wasm_export i32 wasm_temp_buff1_len = 127;
glb_wasm_export char wasm_temp_buff2[128] = {[127] = 0x13};
glb_wasm_export i32 wasm_temp_buff2_len = 127;
glb f64 wasm_dpr;
glb ma_arena_t *wasm_input_text_arena;
glb STACK(app_event_t, 64) wasm_events;
glb b32 wasm_event_failed_to_queue;
glb f64 wasm_last_time = 0;
global f64 wasm_dpr;
global ma_arena_t *wasm_input_text_arena;
global STACK(app_event_t, 64) wasm_events;
global b32 wasm_event_failed_to_queue;
global f64 wasm_delta_time;
global f64 wasm_time;
global f64 wasm_last_time_milliseconds;
global f64 wasm_app_init_time_milliseconds;
typedef struct wasm_cached_t wasm_cached_t;
struct wasm_cached_t {
@@ -141,10 +179,12 @@ fn_wasm_export void wasm_key_up(char *key, b32 ctrl, b32 shift, b32 alt, b32 met
}
}
fn_wasm_export void wasm_update(f64 time, f64 width, f64 height, f64 dpr) {
f64 delta_time = (time - wasm_last_time);
v2f64_t window_size = (v2f64_t){width / dpr, height / dpr};
fn_wasm_export void wasm_update(f64 width, f64 height, f64 dpr) {
wasm_time = os_get_milliseconds();
wasm_delta_time = wasm_time - wasm_last_time_milliseconds;
v2f64_t window_size = (v2f64_t){width / dpr, height / dpr};
wasm_dpr = dpr;
if (wasm_events.len == 0) {
wasm_add_event((app_event_t){
@@ -160,15 +200,12 @@ fn_wasm_export void wasm_update(f64 time, f64 width, f64 height, f64 dpr) {
for (i32 i = 0; i < wasm_events.len; i += 1) {
wasm_events.data[i].dpr = dpr;
wasm_events.data[i].window_size = window_size;
wasm_events.data[i].time = time;
wasm_events.data[i].delta_time = delta_time;
}
wasm_dpr = dpr;
app_update(wasm_events.data, wasm_events.len);
wasm_events.len = 0;
wasm_last_time = time;
wasm_last_time_milliseconds = wasm_time;
ma_set0(wasm_input_text_arena);
}
@@ -176,4 +213,13 @@ fn_wasm_export void wasm_init(void) {
core_init();
wasm_input_text_arena = ma_push_arena(&tcx.perm, kib(1));
app_init();
wasm_app_init_time_milliseconds = os_get_milliseconds();
}
fn f64 app_get_anim_time(void) {
return wasm_time;
}
fn f64 app_get_anim_delta_time(void) {
return wasm_delta_time;
}