diff options
Diffstat (limited to 'reduced_system_layer.c')
-rwxr-xr-x | reduced_system_layer.c | 81 |
1 files changed, 77 insertions, 4 deletions
diff --git a/reduced_system_layer.c b/reduced_system_layer.c index 7963a2d..19c8a0c 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -30,7 +30,7 @@ #/ To-Do list #/ #/ - Examples -#/ - Conway's Game if Life +#/ - Conway's Game of Life #/ - Julia Set #/ - Labyrinth #/ - Landscape @@ -119,6 +119,10 @@ typedef double f64; // // ================================================================ +#ifdef __cplusplus +extern "C" { +#endif + enum { MAX_NUM_PIXELS = 10 * 1024 * 1024, MAX_INPUT_SIZE = 256, @@ -225,6 +229,9 @@ i32 p_handle_events(void); i32 p_wait_events(void); void p_render_frame(void); +// User-defined proc +void p_frame(void); + // Clipboard void p_clipboard_write(i64 size, c8 *data); @@ -238,6 +245,10 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port); extern Platform platform; +#ifdef __cplusplus +} +#endif + // ================================================================ #endif // REDUCED_SYSTEM_LAYER_HEADER_GUARD_ @@ -250,6 +261,27 @@ extern Platform platform; #ifdef REDUCED_SYSTEM_LAYER_EXAMPLE +// NOTE +// This procedure is required for the WebAssembly compatibility. +void p_frame(void) { + p_handle_events(); + + i64 w = platform.frame_width / 2; + i64 h = platform.frame_height / 2; + i64 x = w / 2; + i64 y = h / 2; + + for (i64 j = 0; j < platform.frame_height; ++j) + for (i64 i = 0; i < platform.frame_width; ++i) + if (i < x || i >= x + w || j < y || j >= y + h) + platform.pixels[j * platform.frame_width + i] = 0xa0a0a0; + else + platform.pixels[j * platform.frame_width + i] = 0x131112; + + p_render_frame(); + p_sleep_for(0); +} + i32 main(i32 argc, c8 **argv) { (void) argc; (void) argv; @@ -263,9 +295,7 @@ i32 main(i32 argc, c8 **argv) { p_init(); while (!platform.done) { - p_handle_events(); - p_render_frame(); - p_sleep_for(0); + p_frame(); } p_cleanup(); @@ -1164,6 +1194,49 @@ void p_queue_sound(i64 delay, i64 num_samples, f32 *samples) { #endif // ================================================================ +// +// WebAssembly +// +// ================================================================ + +#ifdef __EMSCRIPTEN__ + +static u32 _buffer[MAX_NUM_PIXELS] = {0}; + +void p_render_frame_impl(void); + +void p_render_frame(void) { + // Make the canvas data opaque. + i64 size = platform.frame_width * platform.frame_height; + for (i64 i = 0; i < size; ++i) + platform.pixels[i] |= 0xff000000; + + p_render_frame_impl(); +} + +void js_main(c8 *href) { + main(1, &href); +} + +void js_init(void) { + platform.pixels = _buffer; + platform.done = 1; +} + +void *js_pixels(void) { + return platform.pixels; +} + +void js_frame(i32 frame_width, i32 frame_height) { + platform.frame_width = frame_width; + platform.frame_height = frame_height; + + p_frame(); +} + +#endif // __EMSCRIPTEN__ + +// ================================================================ #endif // REDUCED_SYSTEM_LAYER_IMPL_GUARD_ #endif // REDUCED_SYSTEM_LAYER_HEADER |