wasm app, revive ui

This commit is contained in:
krzosa
2024-12-31 09:57:41 +01:00
parent 574c265073
commit 0950667bc8
10 changed files with 105 additions and 107 deletions

View File

@@ -1,4 +1,4 @@
fn_wasm_import void wasm_clear();
fn_wasm_import void wasm_clear(void);
fn_wasm_import void wasm_draw_text(isize str, i32 len, f64 x, f64 y, isize font_str, i32 font_len, i32 font_size, f32 r, f32 g, f32 b, f32 a);
fn_wasm_import void wasm_draw_rect(f64 x, f64 y, f64 w, f64 h, f32 r, f32 g, f32 b, f32 a);
fn_wasm_import f64 wasm_measure_text(isize str, i32 len, isize font_str, i32 font_len, i32 font_size);
@@ -10,14 +10,19 @@ 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 char *font_face = "fira";
glb i32 font_face_len = 4;
glb s8_t font_face = s8_const_lit("open_sans_regular");
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;
typedef struct wasm_cached_t wasm_cached_t;
struct wasm_cached_t {
v2f64_t mouse_pos;
b8 ctrl, alt, meta, shift;
} wasm_cached;
fn void app_update(app_event_t *events, i32 event_count);
fn void set_clip(r2f64_t rect) {
@@ -25,11 +30,11 @@ fn void set_clip(r2f64_t rect) {
}
fn f64 get_font_height(void) {
return wasm_get_font_height((isize) font_face, font_face_len, 20*wasm_dpr) / wasm_dpr;
return wasm_get_font_height((isize) font_face.str, font_face.len, 20*wasm_dpr) / wasm_dpr;
}
fn f64 measure_text_ex(char *str, i32 len) {
return wasm_measure_text((isize)str, len, (isize) font_face, font_face_len, 20*wasm_dpr) / wasm_dpr;
return wasm_measure_text((isize)str, len, (isize) font_face.str, font_face.len, 20*wasm_dpr) / wasm_dpr;
}
fn f64 measure_text(char *str) {
@@ -37,7 +42,7 @@ fn f64 measure_text(char *str) {
}
fn void draw_text(v2f64_t pos, v4f32_t color, s8_t string) {
wasm_draw_text((isize)string.str, string.len, wasm_dpr * pos.x, wasm_dpr * pos.y, (isize) font_face, font_face_len, 20*wasm_dpr, color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a);
wasm_draw_text((isize)string.str, string.len, wasm_dpr * pos.x, wasm_dpr * pos.y, (isize) font_face.str, font_face.len, 20*wasm_dpr, color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a);
}
fn void draw_textf(v2f64_t pos, char *str, ...) {
@@ -47,7 +52,7 @@ fn void draw_textf(v2f64_t pos, char *str, ...) {
i32 len = stbsp_vsnprintf(buff, sizeof(buff), str, args);
va_end(args);
wasm_draw_text((isize)buff, len, wasm_dpr * pos.x, wasm_dpr * pos.y, (isize) font_face, font_face_len, 20*wasm_dpr, 0, 0, 0, 1);
wasm_draw_text((isize)buff, len, wasm_dpr * pos.x, wasm_dpr * pos.y, (isize) font_face.str, font_face.len, 20*wasm_dpr, 0, 0, 0, 1);
}
fn void draw_rect(r2f64_t rect, v4f32_t color) {
@@ -64,6 +69,8 @@ fn void wasm_add_event(app_event_t event) {
}
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,
.mouse_pos = {x, y},
@@ -77,6 +84,8 @@ fn_wasm_export void wasm_mouse_move(f64 x, f64 y, b32 ctrl, b32 shift, b32 alt,
fn_wasm_export void wasm_mouse_down(f64 x, f64 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_add_event((app_event_t){
.kind = app_event_kind_mouse_down,
.mouse_pos = {x, y},
@@ -91,6 +100,8 @@ fn_wasm_export void wasm_mouse_down(f64 x, f64 y, i32 button, b32 ctrl, b32 shif
fn_wasm_export void wasm_mouse_up(f64 x, f64 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_add_event((app_event_t){
.kind = app_event_kind_mouse_up,
.mouse_pos = {x, y},
@@ -102,10 +113,13 @@ fn_wasm_export void wasm_mouse_up(f64 x, f64 y, i32 button, b32 ctrl, b32 shift,
});
}
fn_wasm_export void wasm_mouse_wheel(f64 delta_x, f64 delta_y, f64 delta_z, b32 ctrl, b32 shift, b32 alt, b32 meta) {
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;
wasm_add_event((app_event_t){
.kind = app_event_kind_mouse_wheel,
.mouse_wheel_delta = {delta_x, delta_y, delta_z},
.mouse_pos = {x, y},
.ctrl = ctrl,
.shift = shift,
.alt = alt,
@@ -114,14 +128,15 @@ fn_wasm_export void wasm_mouse_wheel(f64 delta_x, f64 delta_y, f64 delta_z, b32
}
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;
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);
wasm_key_map_t map = wasm_map_key_string_to_app_key(key8);
if (map.key != app_key_null) {
wasm_add_event((app_event_t){
.kind = app_event_kind_key_down,
.mouse_pos = wasm_cached.mouse_pos,
.key = map.key,
.ctrl = ctrl,
.shift = shift,
@@ -138,6 +153,7 @@ fn_wasm_export void wasm_key_down(char *key, b32 ctrl, b32 shift, b32 alt, b32 m
s8_t text = s8_copy(wasm_input_text_arena, key8);
wasm_add_event((app_event_t){
.kind = app_event_kind_text,
.mouse_pos = wasm_cached.mouse_pos,
.text = text,
.ctrl = ctrl,
.shift = shift,
@@ -147,6 +163,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;
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);
@@ -155,6 +172,7 @@ fn_wasm_export void wasm_key_up(char *key, b32 ctrl, b32 shift, b32 alt, b32 met
if (map.key != app_key_null) {
wasm_add_event((app_event_t){
.kind = app_event_kind_key_up,
.mouse_pos = wasm_cached.mouse_pos,
.key = map.key,
.ctrl = ctrl,
.shift = shift,
@@ -166,7 +184,20 @@ 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);
v2f32_t window_size = (v2f32_t){width / dpr, height / dpr};
v2f64_t window_size = (v2f64_t){width / dpr, height / dpr};
if (wasm_events.len == 0) {
wasm_add_event((app_event_t){
.kind = app_event_kind_update,
.mouse_pos = wasm_cached.mouse_pos,
.alt = wasm_cached.alt,
.ctrl = wasm_cached.ctrl,
.meta = wasm_cached.meta,
.shift = wasm_cached.shift,
});
}
for (i32 i = 0; i < wasm_events.len; i += 1) {
wasm_events.data[i].dpr = dpr;
wasm_events.data[i].window_size = window_size;