From bd77df921a9591802adfd7f17a2f9c878efda7ac Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Fri, 17 Jan 2025 21:24:49 +0100 Subject: Refactor --- reduced_system_layer.c | 123 ++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 64 deletions(-) (limited to 'reduced_system_layer.c') diff --git a/reduced_system_layer.c b/reduced_system_layer.c index b7f5954..6032ee3 100644 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -225,6 +225,8 @@ i32 main(i32 argc, c8 **argv); #ifndef AVERAGE_FRAME_BIAS #if defined(__wasm__) +// NOTE: Web frame rate is quite unstable, so +// we always pick the longest frame. #define AVERAGE_FRAME_BIAS (0.00) #else #define AVERAGE_FRAME_BIAS (0.02) @@ -280,6 +282,7 @@ i32 main(i32 argc, c8 **argv); #else f64 floor(f64 x); f64 ceil(f64 x); +f64 trunc(f64 x); f64 sqrt(f64 x); f64 cbrt(f64 x); f64 pow(f64 x, f64 y); @@ -753,10 +756,17 @@ static i64 average_frame_duration_(i64 duration) { for (i64 i = 0; i < NUM_FRAMES_AVERAGED; ++i) durs[i] = _frame_duration[i]; - // FIXME: Use better sorting algorithm. + // FIXME: Use better sorting algorithm, e.g. merge sort. for (i64 i = 0; i < (i64) NUM_FRAMES_AVERAGED; ++i) for (i64 j = i + 1; j < NUM_FRAMES_AVERAGED; ++j) - if (durs[i] < durs[j]) { i64 t = durs[i]; durs[i] = durs[j]; durs[j] = t; } + if (durs[i] < durs[j]) { + i64 t = durs[i]; + durs[i] = durs[j]; + durs[j] = t; + } + + if (AVERAGE_FRAME_BIAS < 0. || AVERAGE_FRAME_BIAS > 1.) + return durs[0]; return durs[(i64) ((NUM_FRAMES_AVERAGED - 1) * AVERAGE_FRAME_BIAS)]; } @@ -914,6 +924,48 @@ static void drop_files_set_data_(i64 index, i64 data_size) { g_platform.drop_files[index].data = _drop_files_buffer + offset; } +i8 pixel_size_update_(i32 real_width, i32 real_height) { + i8 size_changed = 0; + + i32 width = real_width; + i32 height = real_height; + + if (g_platform.pixel_size <= 0.) g_platform.pixel_size = DEFAULT_PIXEL_SIZE; + if (g_platform.pixel_size < MIN_PIXEL_SIZE) g_platform.pixel_size = MIN_PIXEL_SIZE; + if (g_platform.pixel_size > MAX_PIXEL_SIZE) g_platform.pixel_size = MAX_PIXEL_SIZE; + + if (g_platform.pixel_size > 1.) { + width = (i32) floor(((f64) real_width) / g_platform.pixel_size + .5); + height = (i32) floor(((f64) real_height) / g_platform.pixel_size + .5); + } + + if (g_platform.real_width != real_width || g_platform.real_height != real_height) { + size_changed = 1; + g_platform.real_width = real_width; + g_platform.real_height = real_height; + } + + if (g_platform.frame_width != width || g_platform.frame_height != height) { + size_changed = 1; + g_platform.frame_width = width; + g_platform.frame_height = height; + } + + return size_changed; +} + +void pixel_size_calubrate_(i64 current_frame_duration) { + if (g_platform.exact_resolution) + return; + + i64 frame_duration = average_frame_duration_(current_frame_duration); + + if (g_platform.pixel_size < MAX_PIXEL_SIZE && frame_duration > MAX_FRAME_DURATION) + g_platform.pixel_size += PIXEL_SIZE_DELTA * (frame_duration - MAX_FRAME_DURATION); + else if (g_platform.pixel_size > MIN_PIXEL_SIZE && frame_duration < MIN_FRAME_DURATION) + g_platform.pixel_size -= PIXEL_SIZE_DELTA * (MIN_FRAME_DURATION - frame_duration); +} + // ================================================================ // // Unix @@ -2108,29 +2160,7 @@ i32 p_handle_events(void) { _image.bytes_per_line = _image.width * 4; } - i32 width = _image.width; - i32 height = _image.height; - - if (g_platform.pixel_size <= 0.) g_platform.pixel_size = DEFAULT_PIXEL_SIZE; - if (g_platform.pixel_size < MIN_PIXEL_SIZE) g_platform.pixel_size = MIN_PIXEL_SIZE; - if (g_platform.pixel_size > MAX_PIXEL_SIZE) g_platform.pixel_size = MAX_PIXEL_SIZE; - - if (g_platform.pixel_size > 1.) { - width = (i32) floor(((f64) _image.width) / g_platform.pixel_size + .5); - height = (i32) floor(((f64) _image.height) / g_platform.pixel_size + .5); - } - - if (g_platform.real_width != _image.width || g_platform.real_height != _image.height) { - ++num_events; - g_platform.real_width = _image.width; - g_platform.real_height = _image.height; - } - - if (g_platform.frame_width != width || g_platform.frame_height != height) { - ++num_events; - g_platform.frame_width = width; - g_platform.frame_height = height; - } + num_events += pixel_size_update_(_image.width, _image.height); return num_events; } @@ -2173,14 +2203,7 @@ void p_render_frame(void) { XPutImage(_display, _window, _gc, &_image, 0, 0, 0, 0, _image.width, _image.height); XFlush(_display); - if (!g_platform.exact_resolution) { - i64 frame_duration = average_frame_duration_(p_time() - _frame_time); - - if (g_platform.pixel_size < MAX_PIXEL_SIZE && frame_duration > MAX_FRAME_DURATION) - g_platform.pixel_size += PIXEL_SIZE_DELTA * (frame_duration - MAX_FRAME_DURATION); - else if (g_platform.pixel_size > MIN_PIXEL_SIZE && frame_duration < MIN_FRAME_DURATION) - g_platform.pixel_size -= PIXEL_SIZE_DELTA * (MIN_FRAME_DURATION - frame_duration); - } + pixel_size_calubrate_(p_time() - _frame_time); } void p_clipboard_write(i64 size, c8 *data) { @@ -2522,29 +2545,7 @@ __attribute__((export_name("js_frame"))) void js_frame(i32 frame_width, i32 fram _frame_height = frame_height; } - i32 width = _frame_width; - i32 height = _frame_height; - - if (g_platform.pixel_size <= 0.) g_platform.pixel_size = DEFAULT_PIXEL_SIZE; - if (g_platform.pixel_size < MIN_PIXEL_SIZE) g_platform.pixel_size = MIN_PIXEL_SIZE; - if (g_platform.pixel_size > MAX_PIXEL_SIZE) g_platform.pixel_size = MAX_PIXEL_SIZE; - - if (g_platform.pixel_size > 1.) { - width = (i32) floor(((f64) _frame_width) / g_platform.pixel_size + .5); - height = (i32) floor(((f64) _frame_height) / g_platform.pixel_size + .5); - } - - if (g_platform.real_width != _frame_width || g_platform.real_height != _frame_height) { - ++_num_events; - g_platform.real_width = _frame_width; - g_platform.real_height = _frame_height; - } - - if (g_platform.frame_width != width || g_platform.frame_height != height) { - ++_num_events; - g_platform.frame_width = width; - g_platform.frame_height = height; - } + _num_size += pixel_size_update_(_frame_width, _frame_height); g_platform.done = 0; @@ -2565,14 +2566,8 @@ __attribute__((export_name("js_frame"))) void js_frame(i32 frame_width, i32 fram _sound_read = (_sound_read + num_samples * NUM_SOUND_CHANNELS) % MAX_NUM_SOUND_FRAMES; - if (!g_platform.exact_resolution && do_render) { - i64 frame_duration = average_frame_duration_(p_time() - frame_time); - - if (g_platform.pixel_size < MAX_PIXEL_SIZE && frame_duration > MAX_FRAME_DURATION) - g_platform.pixel_size += PIXEL_SIZE_DELTA * (frame_duration - MAX_FRAME_DURATION); - else if (g_platform.pixel_size > MIN_PIXEL_SIZE && frame_duration < MIN_FRAME_DURATION) - g_platform.pixel_size -= PIXEL_SIZE_DELTA * (MIN_FRAME_DURATION - frame_duration); - } + if (do_render) + pixel_size_calubrate_(p_time() - frame_time); } __attribute__((export_name("js_mousemove"))) void js_mousemove(i32 x, i32 y) { -- cgit v1.2.3