wasm, app
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user