diff options
Diffstat (limited to 'reduced_system_layer.c')
-rwxr-xr-x | reduced_system_layer.c | 98 |
1 files changed, 81 insertions, 17 deletions
diff --git a/reduced_system_layer.c b/reduced_system_layer.c index 5ddd3ab..7cbfd93 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -108,6 +108,13 @@ typedef double f64; // // ================================================================ +#if !defined(__wasm__) +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include <math.h> +#endif + #ifdef __cplusplus extern "C" { #endif @@ -118,6 +125,7 @@ enum { MAX_CLIPBOARD_SIZE = 10 * 1024 * 1024, MAX_NUM_AUDIO_SAMPLES = 0, MAX_NUM_SOCKETS = 64, + MAX_NUM_KEYS = 512, AUDIO_NUM_CHANNELS = 2, AUDIO_SAMPLE_RATE = 44100, @@ -186,8 +194,8 @@ typedef struct { i32 cursor_dx; i32 cursor_dy; i64 wheel_dy; - b8 key_down[512]; - b8 key_pressed[512]; + b8 key_down[MAX_NUM_KEYS]; + b8 key_pressed[MAX_NUM_KEYS]; } Platform; typedef struct { @@ -219,8 +227,9 @@ i32 p_wait_events(void); void p_render_frame(void); // User-defined proc -void p_frame(void); +void update_and_render_frame(void); +// Convenient helper for the event loop void p_event_loop(void); // Clipboard @@ -238,6 +247,14 @@ extern Platform platform; #ifdef __wasm__ i32 main(i32 argc, c8 **argv); + +f64 sqrt(f64 x); +f64 floor(f64 x); +f64 ceil(f64 x); + +#ifndef NULL +#define NULL ((void *) 0) +#endif #endif #ifdef __cplusplus @@ -258,7 +275,7 @@ i32 main(i32 argc, c8 **argv); // NOTE // This procedure is required for the WebAssembly compatibility. -void p_frame(void) { +void update_and_render_frame(void) { p_handle_events(); i64 w = platform.frame_width / 2; @@ -282,7 +299,7 @@ i32 main(i32 argc, c8 **argv) { (void) argv; platform = (Platform) { - .title = "Reduced System Layer", + .title = "Example", .frame_width = 1280, .frame_height = 720, }; @@ -308,7 +325,7 @@ Platform platform = {0}; void p_event_loop(void) { p_init(); - while (!platform.done) p_frame(); + while (!platform.done) update_and_render_frame(); p_cleanup(); } @@ -392,10 +409,6 @@ i32 utf8_write(c32 c, c8 *buffer) { #if defined(__unix__) -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include <time.h> #include <sched.h> #include <unistd.h> @@ -669,8 +682,8 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port) { #include <X11/Xutil.h> #include <X11/Xatom.h> -static i16 _key_table[512] = {0}; -static b8 _key_repeat[512] = {0}; +static i16 _key_table[MAX_NUM_KEYS] = {0}; +static b8 _key_repeat[MAX_NUM_KEYS] = {0}; static u32 _buffer[MAX_NUM_PIXELS] = {0}; static Input_Key _input[MAX_INPUT_SIZE] = {0}; static c8 _clipboard_buffer[MAX_CLIPBOARD_SIZE] = {0}; @@ -1205,12 +1218,31 @@ void p_queue_sound(i64 delay, i64 num_samples, f32 *samples) { #ifdef __wasm__ -static u32 _buffer[MAX_NUM_PIXELS] = {0}; +static u32 _buffer[MAX_NUM_PIXELS] = {0}; +static i32 _num_events = 0; +static b8 _key_pressed[MAX_NUM_KEYS] = {0}; + +i32 p_handle_events(void) { + for (i64 i = 0; i < MAX_NUM_KEYS; ++i) + platform.key_pressed[i] = _key_pressed[i]; + for (i64 i = 0; i < MAX_NUM_KEYS; ++i) + _key_pressed[i] = 0; + i32 n = _num_events; + _num_events = 0; + return n; +} + +i32 p_wait_events_impl(void); + +i32 p_wait_events(void) { + p_wait_events_impl(); + return p_handle_events(); +} void p_render_frame_impl(void); void p_render_frame(void) { - // Make the canvas data opaque. + // Make canvas pixels opaque. i64 size = platform.frame_width * platform.frame_height; for (i64 i = 0; i < size; ++i) platform.pixels[i] |= 0xff000000; @@ -1227,15 +1259,47 @@ __attribute__((export_name("js_init"))) void js_init(void) { platform.done = 1; } +__attribute__((export_name("js_title"))) c8 *js_title(void) { + return platform.title; +} + __attribute__((export_name("js_pixels"))) void *js_pixels(void) { return platform.pixels; } __attribute__((export_name("js_frame"))) void js_frame(i32 frame_width, i32 frame_height) { - platform.frame_width = frame_width; - platform.frame_height = frame_height; + if (platform.frame_width != frame_width || platform.frame_height != frame_height) { + ++_num_events; + platform.frame_width = frame_width; + platform.frame_height = frame_height; + } + + update_and_render_frame(); +} + +__attribute__((export_name("js_mousemove"))) void js_mousemove(i32 x, i32 y) { + ++_num_events; + platform.cursor_dx = x - platform.cursor_x; + platform.cursor_dy = y - platform.cursor_y; + platform.cursor_x = x; + platform.cursor_y = y; +} + +__attribute__((export_name("js_mousedown"))) void js_mousedown(u32 buttons) { + ++_num_events; + if ((buttons & 1) && !platform.key_down[BUTTON_LEFT]) _key_pressed[BUTTON_LEFT] = 1; + if ((buttons & 2) && !platform.key_down[BUTTON_RIGHT]) _key_pressed[BUTTON_RIGHT] = 1; + if ((buttons & 4) && !platform.key_down[BUTTON_MIDDLE]) _key_pressed[BUTTON_MIDDLE] = 1; + if (buttons & 1) platform.key_down[BUTTON_LEFT] = 1; + if (buttons & 2) platform.key_down[BUTTON_RIGHT] = 1; + if (buttons & 4) platform.key_down[BUTTON_MIDDLE] = 1; +} - p_frame(); +__attribute__((export_name("js_mouseup"))) void js_mouseup(u32 buttons) { + ++_num_events; + if (!(buttons & 1)) platform.key_down[BUTTON_LEFT] = 0; + if (!(buttons & 2)) platform.key_down[BUTTON_RIGHT] = 0; + if (!(buttons & 4)) platform.key_down[BUTTON_MIDDLE] = 0; } #endif // __wasm__ |