summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2025-01-17 21:24:49 +0100
committerMitya Selivanov <automainint@guattari.tech>2025-01-17 21:24:49 +0100
commitbd77df921a9591802adfd7f17a2f9c878efda7ac (patch)
tree92e84ecf6ec56dc542d8ef3b714078ccb2926bb0
parent8a44b6837df815fb03425f6e5f2c49fd8f5134cd (diff)
downloadreduced_system_layer-bd77df921a9591802adfd7f17a2f9c878efda7ac.zip
Refactor
-rw-r--r--graphics.c2
-rw-r--r--index.htm1
-rw-r--r--reduced_system_layer.c123
3 files changed, 62 insertions, 64 deletions
diff --git a/graphics.c b/graphics.c
index ea7edee..1650d32 100644
--- a/graphics.c
+++ b/graphics.c
@@ -309,12 +309,14 @@ void draw_pixels_(Brush brush, f64 x, f64 y, f64 width, f64 height, Pixel_Buffer
for (i64 j = j0; j <= j1; ++j) {
i64 src_j0 = (i64) floor(((j - y0) * src.height) / h + .5);
i64 src_j1 = (i64) floor(((j + 1 - y0) * src.height) / h + .5);
+ if (src_j0 == src_j1) src_j1 = src_j0 + 1;
if (src_j1 <= 0 || src_j0 >= src.height) continue;
if (src_j0 < 0) src_j0 = 0;
if (src_j1 > src.height) src_j1 = src.height;
for (i64 i = i0; i <= i1; ++i) {
i64 src_i0 = (i64) floor(((i - x0) * src.width) / w + .5);
i64 src_i1 = (i64) floor(((i + 1 - x0) * src.width) / w + .5);
+ if (src_i0 == src_i1) src_i1 = src_i0 + 1;
if (src_i1 <= 0 || src_i0 >= src.width) continue;
if (src_i0 < 0) src_i0 = 0;
if (src_i1 > src.width) src_i1 = src.width;
diff --git a/index.htm b/index.htm
index ada10e2..18c110f 100644
--- a/index.htm
+++ b/index.htm
@@ -47,6 +47,7 @@
floor : Math.floor,
ceil : Math.ceil,
+ trunc : Math.trunc,
sqrt : Math.sqrt,
cbrt : Math.cbrt,
pow : Math.pow,
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) {