diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2025-01-14 20:48:38 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2025-01-14 20:48:38 +0100 |
commit | a14a2e0d933e64b1c28b45ac10e651af7f749b3e (patch) | |
tree | c98bb6d14eca61f649db516e16ab92260e12c59c | |
parent | f52300e90f4bcb2092dd2beba327c2499d6fbd11 (diff) | |
download | reduced_system_layer-a14a2e0d933e64b1c28b45ac10e651af7f749b3e.zip |
Refactor
-rw-r--r-- | Dockerfile | 2 | ||||
-rwxr-xr-x | build_all.sh | 51 | ||||
-rw-r--r-- | examples/particles.c | 4 | ||||
-rw-r--r-- | examples/sinewave.c | 2 | ||||
-rwxr-xr-x | reduced_system_layer.c | 320 |
5 files changed, 243 insertions, 136 deletions
@@ -5,7 +5,7 @@ RUN apk add clang lld COPY examples /usr/examples COPY reduced_system_layer.c /usr/reduced_system_layer.c COPY graphics.c /usr/graphics.c -RUN clang --target=wasm32 -O3 -nostdlib -fno-builtin -mbulk-memory -Wl,--no-entry,--allow-undefined -o /usr/index.wasm /usr/examples/ui.c +RUN clang -D NDEBUG -O3 --target=wasm32 -nostdlib -fno-builtin -mbulk-memory -Wl,--no-entry,--allow-undefined -o /usr/index.wasm /usr/examples/ui.c FROM nginx:alpine EXPOSE 80 diff --git a/build_all.sh b/build_all.sh index dd6da5e..1cc40ee 100755 --- a/build_all.sh +++ b/build_all.sh @@ -1,4 +1,4 @@ -FLAGS=" \ +FLAGS_X11=" \ -Wall -Wextra -Werror -pedantic \ -Wno-old-style-declaration \ -Wno-missing-braces \ @@ -6,21 +6,44 @@ FLAGS=" \ -Wno-unused-but-set-variable \ -Wno-unused-parameter \ -Wno-overlength-strings \ + -D NDEBUG \ -O3 \ -fsanitize=undefined,address,leak \ -lX11 -lm -lasound" -if [ ! -d ./bin ]; then - mkdir ./bin -fi +FLAGS_WEB=" \ + -Wall -Wextra -Werror -pedantic \ + -Wno-missing-braces \ + -Wno-unused-variable \ + -Wno-unused-but-set-variable \ + -Wno-unused-parameter \ + -Wno-overlength-strings \ + -D NDEBUG \ + -O3 \ + --target=wasm32 \ + -nostdlib -fno-builtin \ + -mbulk-memory \ + -Wl,--no-entry,--allow-undefined" + +mkdir -p bin/x11 +mkdir -p bin/web + +gcc $FLAGS_X11 -o ./bin/x11/graph ./examples/graph.c +gcc $FLAGS_X11 -o ./bin/x11/particles ./examples/particles.c +gcc $FLAGS_X11 -o ./bin/x11/julia_set ./examples/julia_set.c +gcc $FLAGS_X11 -o ./bin/x11/game_of_life ./examples/game_of_life.c +gcc $FLAGS_X11 -o ./bin/x11/labyrinth ./examples/labyrinth.c +gcc $FLAGS_X11 -o ./bin/x11/sinewave ./examples/sinewave.c +gcc $FLAGS_X11 -o ./bin/x11/pixels ./examples/pixels.c +gcc $FLAGS_X11 -o ./bin/x11/ui ./examples/ui.c +gcc $FLAGS_X11 -o ./bin/x11/echo ./examples/echo.c +gcc $FLAGS_X11 -o ./bin/x11/proto ./examples/proto.c -gcc $FLAGS -o ./bin/graph ./examples/graph.c -gcc $FLAGS -o ./bin/particles ./examples/particles.c -gcc $FLAGS -o ./bin/julia_set ./examples/julia_set.c -gcc $FLAGS -o ./bin/game_of_life ./examples/game_of_life.c -gcc $FLAGS -o ./bin/labyrinth ./examples/labyrinth.c -gcc $FLAGS -o ./bin/sinewave ./examples/sinewave.c -gcc $FLAGS -o ./bin/pixels ./examples/pixels.c -gcc $FLAGS -o ./bin/ui ./examples/ui.c -gcc $FLAGS -o ./bin/echo ./examples/echo.c -gcc $FLAGS -o ./bin/proto ./examples/proto.c +clang $FLAGS_WEB -o ./bin/web/graph.wasm ./examples/graph.c +clang $FLAGS_WEB -o ./bin/web/julia_set.wasm ./examples/julia_set.c +clang $FLAGS_WEB -o ./bin/web/game_of_life.wasm ./examples/game_of_life.c +clang $FLAGS_WEB -o ./bin/web/labyrinth.wasm ./examples/labyrinth.c +clang $FLAGS_WEB -o ./bin/web/sinewave.wasm ./examples/sinewave.c +clang $FLAGS_WEB -o ./bin/web/pixels.wasm ./examples/pixels.c +clang $FLAGS_WEB -o ./bin/web/ui.wasm ./examples/ui.c +clang $FLAGS_WEB -o ./bin/web/proto.wasm ./examples/proto.c diff --git a/examples/particles.c b/examples/particles.c index 03c51f7..130b5bf 100644 --- a/examples/particles.c +++ b/examples/particles.c @@ -1,3 +1,7 @@ +#if defined(__wasm__) +#error Not implemented! +#endif + #include "../graphics.c" typedef struct { diff --git a/examples/sinewave.c b/examples/sinewave.c index 4f8bbe0..f2b2580 100644 --- a/examples/sinewave.c +++ b/examples/sinewave.c @@ -34,7 +34,7 @@ void update_and_render_frame(void) { i64 samples_elapsed = ((p_time() - time_0) * SOUND_SAMPLE_RATE) / 1000 - audio_samples; audio_samples += samples_elapsed; - p_handle_audio(samples_elapsed); + p_handle_sound(); p_render_frame(); p_sleep_for(0); } diff --git a/reduced_system_layer.c b/reduced_system_layer.c index 4bcd3ac..d86945e 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -155,22 +155,10 @@ typedef struct { f64 v[16]; } mat4; // ================================================================ // -// PLATFORM API +// Options // // ================================================================ -#if !defined(__wasm__) -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif -#include <stdlib.h> -#include <math.h> -#endif - -#ifdef __cplusplus -extern "C" { -#endif - #ifndef MAX_NUM_PIXELS #define MAX_NUM_PIXELS (10 * 1024 * 1024) #endif @@ -231,6 +219,24 @@ extern "C" { #define MAX_NUM_SOUND_FRAMES (10 * SOUND_SAMPLE_RATE * NUM_SOUND_CHANNELS) #endif +// ================================================================ +// +// PLATFORM API +// +// ================================================================ + +#if !defined(__wasm__) +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include <stdlib.h> +#include <math.h> +#endif + +#ifdef __cplusplus +extern "C" { +#endif + enum { IPv4_UDP = 1, IPv6_UDP = 2, @@ -373,28 +379,34 @@ typedef struct { } Input_Key; typedef struct { - c8 * title; - i32 real_width; - i32 real_height; - i32 frame_width; - i32 frame_height; - vec4_f32 * pixels; - i64 input_size; - Input_Key *input; - i64 clipboard_size; - c8 * clipboard; - b8 done; - b8 exact_resolution; - b8 graceful_exit; - b8 has_focus; - b8 has_cursor; - i32 cursor_x; - i32 cursor_y; - i32 cursor_dx; - i32 cursor_dy; - i64 wheel_dy; - b8 key_down[MAX_NUM_KEYS]; - b8 key_pressed[MAX_NUM_KEYS]; + c8 *title; + i32 frame_width; + i32 frame_height; + b8 exact_resolution; + b8 graceful_exit; + + b8 done; + b8 has_focus; + b8 has_cursor; + i32 real_width; + i32 real_height; + i64 input_size; + i64 clipboard_size; + i32 cursor_x; + i32 cursor_y; + i32 cursor_dx; + i32 cursor_dy; + i64 wheel_dx; + i64 wheel_dy; + i64 sound_clock_time; + i64 sound_clock_carry; + + vec4_f32 pixels [MAX_NUM_PIXELS]; + f32 sound_ring [MAX_NUM_SOUND_FRAMES]; + Input_Key input [MAX_INPUT_SIZE]; + c8 clipboard [MAX_CLIPBOARD_SIZE]; + b8 key_down [MAX_NUM_KEYS]; + b8 key_pressed [MAX_NUM_KEYS]; } Platform; typedef struct { @@ -440,7 +452,7 @@ void p_event_loop(void); void p_clipboard_write(i64 size, c8 *data); // Sound -void p_handle_audio(i64 samples_elapsed); +void p_handle_sound(void); void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames); // UDP sockets @@ -450,7 +462,9 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port); extern Platform g_platform; #if defined(__wasm__) + i32 main(i32 argc, c8 **argv); + f64 floor(f64 x); f64 ceil(f64 x); f64 sqrt(f64 x); @@ -467,6 +481,7 @@ f64 asin(f64 x); f64 acos(f64 x); f64 atan(f64 x); f64 atan2(f64 y, f64 x); + #endif // defined(__wasm__) #ifndef NULL @@ -474,7 +489,63 @@ f64 atan2(f64 y, f64 x); #endif #ifndef M_PI -#define M_PI 3.14159265358979323846 +#define M_PI (3.14159265358979323846) +#endif + +#ifndef M_E +#define M_E (2.71828182845904523536) +#endif + +#ifndef M_LOG_2 +#define M_LOG_2 (0.69314718055994530941) +#endif + +#ifndef M_LOG_10 +#define M_LOG_10 (2.30258509299404568402) +#endif + +#ifndef M_LOG2_E +#define M_LOG2_E (1.44269504088896340736) +#endif + +#ifndef M_LOG10_E +#define M_LOG10_E (0.43429448190325182765) +#endif + +#ifndef M_LOG_PI +#define M_LOG_PI (1.14472988584940017414) +#endif + +#ifndef M_SQRT_2 +#define M_SQRT_2 (1.41421356237309504880) +#endif + +#ifndef M_SQRT_3 +#define M_SQRT_3 (1.73205080756887729352) +#endif + +#ifndef M_SQRT_PI +#define M_SQRT_PI (1.77245385090551602730) +#endif + +#ifndef M_SQRT_E +#define M_SQRT_E (1.64872127070012814685) +#endif + +#ifndef M_CBRT_2 +#define M_CBRT_2 (1.25992104989487316476) +#endif + +#ifndef M_CBRT_3 +#define M_CBRT_3 (1.44224957030740838232) +#endif + +#ifndef M_CBRT_PI +#define M_CBRT_PI (1.46459188756152326302) +#endif + +#ifndef M_CBRT_E +#define M_CBRT_E (1.39561242508608952863) #endif #ifdef __cplusplus @@ -641,6 +712,23 @@ vec3_f32 rgb_f32_from_u32(u32 color) { }; } +static i64 sound_samples_elapsed_(void) { + if (g_platform.sound_clock_time == 0) { + g_platform.sound_clock_time = p_time(); + g_platform.sound_clock_carry = 0; + return 0; + } + + i64 time_elapsed = p_time() - g_platform.sound_clock_time; + i64 sum = time_elapsed * SOUND_SAMPLE_RATE + g_platform.sound_clock_carry; + i64 num_samples = sum / 1000; + + g_platform.sound_clock_time += time_elapsed; + g_platform.sound_clock_carry = sum % 1000; + + return num_samples; +} + // ================================================================ // // Unix @@ -935,7 +1023,6 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port) { static b8 _sound_ready = 0; static i64 _sound_position = 0; -static f32 _sound_ring[MAX_NUM_SOUND_FRAMES] = {0}; static snd_pcm_t *_sound_out = NULL; static void sound_init(void) { @@ -1023,10 +1110,11 @@ static void sound_cleanup(void) { _sound_ready = 0; } -void p_handle_audio(i64 samples_elapsed) { +void p_handle_sound(void) { sound_init(); - i64 num_frames = samples_elapsed * NUM_SOUND_CHANNELS; + i64 samples_elapsed = sound_samples_elapsed_(); + i64 num_frames = samples_elapsed * NUM_SOUND_CHANNELS; if (num_frames > MAX_NUM_SOUND_FRAMES) { fprintf(stderr, "%s:%d, %s: Sound buffer overflow.\n", __FILE__, __LINE__, __func__); @@ -1036,25 +1124,25 @@ void p_handle_audio(i64 samples_elapsed) { i32 s; if (num_frames <= MAX_NUM_SOUND_FRAMES - _sound_position) { - s = snd_pcm_writei(_sound_out, _sound_ring + _sound_position, num_frames / NUM_SOUND_CHANNELS); + s = snd_pcm_writei(_sound_out, g_platform.sound_ring + _sound_position, num_frames / NUM_SOUND_CHANNELS); if (s < 0) fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s)); - memset(_sound_ring + _sound_position, 0, num_frames * sizeof *_sound_ring); + memset(g_platform.sound_ring + _sound_position, 0, num_frames * sizeof *g_platform.sound_ring); } else { i64 part_one = MAX_NUM_SOUND_FRAMES - _sound_position; i64 part_two = num_frames - part_one; - s = snd_pcm_writei(_sound_out, _sound_ring + _sound_position, part_one / NUM_SOUND_CHANNELS); + s = snd_pcm_writei(_sound_out, g_platform.sound_ring + _sound_position, part_one / NUM_SOUND_CHANNELS); if (s < 0) fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s)); - s = snd_pcm_writei(_sound_out, _sound_ring, part_two / NUM_SOUND_CHANNELS); + s = snd_pcm_writei(_sound_out, g_platform.sound_ring, part_two / NUM_SOUND_CHANNELS); if (s < 0) fprintf(stderr, "%s:%d, %s: snd_pcm_writei failed: %s\n", __FILE__, __LINE__, __func__, snd_strerror(s)); - memset(_sound_ring + _sound_position, 0, part_one * sizeof *_sound_ring); - memset(_sound_ring, 0, part_two * sizeof *_sound_ring); + memset(g_platform.sound_ring + _sound_position, 0, part_one * sizeof *g_platform.sound_ring); + memset(g_platform.sound_ring, 0, part_two * sizeof *g_platform.sound_ring); } _sound_position = (_sound_position + num_frames) % MAX_NUM_SOUND_FRAMES; @@ -1088,15 +1176,15 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { if (num_frames <= MAX_NUM_SOUND_FRAMES - begin) for (i64 i = 0; i < num_frames; ++i) - _sound_ring[begin + i] += frames[i]; + g_platform.sound_ring[begin + i] += frames[i]; else { i64 part_one = MAX_NUM_SOUND_FRAMES - begin; i64 part_two = num_frames - part_one; for (i64 i = 0; i < part_one; ++i) - _sound_ring[begin + i] += frames[i]; + g_platform.sound_ring[begin + i] += frames[i]; for (i64 i = 0; i < part_two; ++i) - _sound_ring[i] += frames[part_one + i]; + g_platform.sound_ring[i] += frames[part_one + i]; } } @@ -1114,26 +1202,25 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { #include <X11/Xutil.h> #include <X11/Xatom.h> -static i16 _key_table[MAX_NUM_KEYS] = {0}; -static b8 _key_repeat[MAX_NUM_KEYS] = {0}; -static i64 _frame_time = 0; -static vec4_f32 _pixels[MAX_NUM_PIXELS] = {0}; -static i32 _pixel_size = 0; -static u32 _pixels_scaled[MAX_NUM_PIXELS] = {0}; -static u32 _pixels_internal[MAX_NUM_PIXELS] = {0}; -static Input_Key _input[MAX_INPUT_SIZE] = {0}; -static c8 _clipboard_buffer[MAX_CLIPBOARD_SIZE] = {0}; -static XImage _image = {0}; -static Display * _display = NULL; -static GC _gc = NULL; -static XIM _im = NULL; -static XIC _ic = NULL; -static Window _window = 0; -static Atom _wm_delete_window = 0; -static Atom _clipboard = 0; -static Atom _targets = 0; -static Atom _utf8_string = 0; -static Atom _target = None; +static i64 _frame_time = 0; +static i32 _pixel_size = 0; +static XImage _image = {0}; +static Display *_display = NULL; +static GC _gc = NULL; +static XIM _im = NULL; +static XIC _ic = NULL; +static Window _window = 0; +static Atom _wm_delete_window = 0; +static Atom _clipboard = 0; +static Atom _targets = 0; +static Atom _utf8_string = 0; +static Atom _target = None; + +static i16 _key_table [MAX_NUM_KEYS] = {0}; +static b8 _key_repeat [MAX_NUM_KEYS] = {0}; +static u32 _pixels_scaled [MAX_NUM_PIXELS] = {0}; +static u32 _pixels_internal [MAX_NUM_PIXELS] = {0}; +static c8 _clipboard_buffer [MAX_CLIPBOARD_SIZE] = {0}; void p_init(void) { _display = XOpenDisplay(NULL); @@ -1296,10 +1383,6 @@ void p_init(void) { return; } - g_platform.pixels = _pixels; - g_platform.input = _input; - g_platform.clipboard = _clipboard_buffer; - _image = (XImage) { .width = g_platform.frame_width, .height = g_platform.frame_height, @@ -1367,6 +1450,7 @@ i32 p_handle_events(void) { g_platform.input_size = 0; g_platform.cursor_dx = 0; g_platform.cursor_dy = 0; + g_platform.wheel_dx = 0; g_platform.wheel_dy = 0; XEvent ev; @@ -1396,19 +1480,21 @@ i32 p_handle_events(void) { g_platform.cursor_y = ev.xbutton.y; switch (ev.xbutton.button) { case Button1: - g_platform.key_down[BUTTON_LEFT] = 1; + g_platform.key_down [BUTTON_LEFT] = 1; g_platform.key_pressed[BUTTON_LEFT] = 1; break; case Button2: - g_platform.key_down[BUTTON_MIDDLE] = 1; + g_platform.key_down [BUTTON_MIDDLE] = 1; g_platform.key_pressed[BUTTON_MIDDLE] = 1; break; case Button3: - g_platform.key_down[BUTTON_RIGHT] = 1; + g_platform.key_down [BUTTON_RIGHT] = 1; g_platform.key_pressed[BUTTON_RIGHT] = 1; break; - case Button4: ++g_platform.wheel_dy; break; - case Button5: --g_platform.wheel_dy; break; + case Button4: ++g_platform.wheel_dy; break; + case Button5: --g_platform.wheel_dy; break; + case Button5 + 1: --g_platform.wheel_dx; break; + case Button5 + 2: ++g_platform.wheel_dx; break; default:; } if (!requested_clipboard) { @@ -1670,11 +1756,11 @@ void p_render_frame(void) { if (g_platform.frame_width == _image.width && g_platform.frame_height == _image.height) { i64 size = g_platform.frame_width * g_platform.frame_height; for (i64 i = 0; i < size; ++i) - _pixels_internal[i] = rgb_u32_from_f32((vec3_f32) { _pixels[i].x, _pixels[i].y, _pixels[i].z }); + _pixels_internal[i] = rgb_u32_from_f32((vec3_f32) { g_platform.pixels[i].x, g_platform.pixels[i].y, g_platform.pixels[i].z }); } else { i64 size = g_platform.frame_width * g_platform.frame_height; for (i64 i = 0; i < size; ++i) - _pixels_scaled[i] = rgb_u32_from_f32((vec3_f32) { _pixels[i].x, _pixels[i].y, _pixels[i].z }); + _pixels_scaled[i] = rgb_u32_from_f32((vec3_f32) { g_platform.pixels[i].x, g_platform.pixels[i].y, g_platform.pixels[i].z }); for (i64 j = 0; j < _image.height; ++j) { i64 j0 = (j * g_platform.frame_height) / _image.height; @@ -1721,30 +1807,29 @@ void p_clipboard_write(i64 size, c8 *data) { #if defined(__wasm__) -static c8 _href[512] = {0}; -static i32 _frame_width = 0; -static i32 _frame_height = 0; -static vec4_f32 _pixels[MAX_NUM_PIXELS] = {0}; -static i32 _pixel_size = 0; -static u32 _pixels_scaled[MAX_NUM_PIXELS] = {0}; -static u32 _pixels_internal[MAX_NUM_PIXELS] = {0}; -static Input_Key _input[MAX_INPUT_SIZE] = {0}; -static c8 _clipboard_buffer[MAX_CLIPBOARD_SIZE] = {0}; -static i32 _num_events = 0; -static i32 _input_size = 0; -static b8 _key_pressed[MAX_NUM_KEYS] = {0}; -static b8 _wait_events = 0; -static i64 _timeout = 0; -static i64 _sound_position = 0; -static i64 _sound_read = 0; -static f32 _sound_ring[MAX_NUM_SOUND_FRAMES] = {0}; -static f32 _sound_buffer[MAX_NUM_SOUND_FRAMES] = {0}; +static i32 _frame_width = 0; +static i32 _frame_height = 0; +static i32 _pixel_size = 0; +static i32 _num_events = 0; +static i32 _input_size = 0; +static b8 _wait_events = 0; +static i64 _timeout = 0; +static i64 _sound_position = 0; +static i64 _sound_read = 0; + +static c8 _href [4096] = {0}; +static u32 _pixels_scaled [MAX_NUM_PIXELS] = {0}; +static u32 _pixels_internal [MAX_NUM_PIXELS] = {0}; +static b8 _key_pressed [MAX_NUM_KEYS] = {0}; +static f32 _sound_buffer [MAX_NUM_SOUND_FRAMES] = {0}; i64 p_recv(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port, IP_Address *remote_address) { + // Not implemented return 0; } i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port) { + // Not implemented return 0; } @@ -1765,12 +1850,9 @@ void p_sleep_for(i64 duration) { void p_init(void) { ++_num_events; - g_platform.pixels = _pixels; - g_platform.input = _input; - g_platform.clipboard = _clipboard_buffer; - g_platform.done = 1; - _sound_position = SOUND_AVAIL_MIN % MAX_NUM_SOUND_FRAMES; - _sound_read = 0; + g_platform.done = 1; + _sound_position = SOUND_AVAIL_MIN % MAX_NUM_SOUND_FRAMES; + _sound_read = 0; } i32 p_handle_events(void) { @@ -1797,11 +1879,11 @@ void p_render_frame(void) { if (_frame_width == g_platform.frame_width && _frame_height == g_platform.frame_height) { i64 size = g_platform.frame_width * g_platform.frame_height; for (i64 i = 0; i < size; ++i) - _pixels_internal[i] = 0xff000000u | rgb_u32_from_f32((vec3_f32) { _pixels[i].z, _pixels[i].y, _pixels[i].x }); + _pixels_internal[i] = 0xff000000u | rgb_u32_from_f32((vec3_f32) { g_platform.pixels[i].z, g_platform.pixels[i].y, g_platform.pixels[i].x }); } else { i64 size = g_platform.frame_width * g_platform.frame_height; for (i64 i = 0; i < size; ++i) - _pixels_scaled[i] = rgb_u32_from_f32((vec3_f32) { _pixels[i].z, _pixels[i].y, _pixels[i].x }); + _pixels_scaled[i] = rgb_u32_from_f32((vec3_f32) { g_platform.pixels[i].z, g_platform.pixels[i].y, g_platform.pixels[i].x }); for (i64 j = 0; j < _frame_height; ++j) { i64 j0 = (j * g_platform.frame_height) / _frame_height; @@ -1828,11 +1910,9 @@ void p_clipboard_write(i64 size, c8 *data) { p_clipboard_write_impl((i32) size, data); } -void p_handle_audio(i64 samples_elapsed) { - if (samples_elapsed <= 0) - return; - - _sound_position = (_sound_position + samples_elapsed * NUM_SOUND_CHANNELS) % MAX_NUM_SOUND_FRAMES; +void p_handle_sound(void) { + i64 samples_elapsed = sound_samples_elapsed_(); + _sound_position = (_sound_position + samples_elapsed * NUM_SOUND_CHANNELS) % MAX_NUM_SOUND_FRAMES; } void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { @@ -1856,15 +1936,15 @@ void p_queue_sound(i64 delay_in_samples, i64 num_samples, f32 *frames) { if (num_frames <= MAX_NUM_SOUND_FRAMES - begin) for (i64 i = 0; i < num_frames; ++i) - _sound_ring[begin + i] += frames[i]; + g_platform.sound_ring[begin + i] += frames[i]; else { i64 part_one = MAX_NUM_SOUND_FRAMES - begin; i64 part_two = num_frames - part_one; for (i64 i = 0; i < part_one; ++i) - _sound_ring[begin + i] += frames[i]; + g_platform.sound_ring[begin + i] += frames[i]; for (i64 i = 0; i < part_two; ++i) - _sound_ring[i] += frames[part_one + i]; + g_platform.sound_ring[i] += frames[part_one + i]; } } @@ -1877,7 +1957,7 @@ __attribute__((export_name("js_href_size"))) i32 js_href_size(void) { } __attribute__((export_name("js_main"))) void js_main(void) { - main(1, &_href); + main(1, (c8 *[1]) { _href }); } __attribute__((export_name("js_title"))) void *js_title(void) { @@ -1932,8 +2012,8 @@ __attribute__((export_name("js_frame"))) void js_frame(i32 frame_width, i32 fram for (i64 j = 0; j < NUM_SOUND_CHANNELS; ++j) for (i64 i = 0; i < num_samples; ++i) { i64 n = (_sound_read + i * NUM_SOUND_CHANNELS + j) % MAX_NUM_SOUND_FRAMES; - _sound_buffer[j * num_samples + i] = _sound_ring[n]; - _sound_ring [n] = 0.f; + _sound_buffer[j * num_samples + i] = g_platform.sound_ring[n]; + g_platform.sound_ring[n] = 0.f; } _sound_read = (_sound_read + num_samples * NUM_SOUND_CHANNELS) % MAX_NUM_SOUND_FRAMES; @@ -1975,7 +2055,7 @@ __attribute__((export_name("js_mouseup"))) void js_mouseup(u32 buttons) { __attribute__((export_name("js_keydown"))) void js_keydown(u32 key, u32 mod, u32 ch) { ++_num_events; - _key_pressed[key] = 1; + _key_pressed[key] = 1; g_platform.key_down[key] = 1; g_platform.key_down[MOD_CTRL] = (mod & 1) ? 1 : 0; g_platform.key_down[MOD_SHIFT] = (mod & 2) ? 1 : 0; @@ -2005,11 +2085,11 @@ __attribute__((export_name("js_sample_rate"))) f64 js_sample_rate(void) { return (f64) SOUND_SAMPLE_RATE; } -__attribute__((export_name("js_num_channels"))) i32 js_num_channels(void) { +__attribute__((export_name("js_num_sound_channels"))) i32 js_num_sound_channels(void) { return NUM_SOUND_CHANNELS; } -__attribute__((export_name("js_max_num_audio_frames"))) i32 js_max_num_audio_frames(void) { +__attribute__((export_name("js_max_num_sound_frames"))) i32 js_max_num_sound_frames(void) { return MAX_NUM_SOUND_FRAMES; } |