summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2025-01-20 08:54:26 +0100
committerMitya Selivanov <automainint@guattari.tech>2025-01-20 08:54:26 +0100
commitfede6d46ef07c088f320db7f90a9e1479a3985bf (patch)
treea424e2ba9261a9adf0aa0c81dfcb31ba3533c6cd /examples
parentb9c569b0a32c6702312a420d1ee8a6607995bf8e (diff)
downloadreduced_system_layer-fede6d46ef07c088f320db7f90a9e1479a3985bf.zip
Fix scaling overflow
Diffstat (limited to 'examples')
-rw-r--r--examples/julia_set.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/examples/julia_set.c b/examples/julia_set.c
index 6ae8e6f..e819ca5 100644
--- a/examples/julia_set.c
+++ b/examples/julia_set.c
@@ -1,5 +1,7 @@
#include "../graphics.c"
+#define SCALE_LIMIT (0.9)
+
f64 view_x = 0.;
f64 view_y = 0.;
f64 view_s = 1.;
@@ -9,6 +11,28 @@ f64 radius = 2.;
i64 limit = 1024;
i64 time_0;
+void apply_scale(f64 delta) {
+ while (delta > SCALE_LIMIT) {
+ apply_scale(SCALE_LIMIT);
+ delta -= SCALE_LIMIT;
+ }
+ while (delta < -SCALE_LIMIT) {
+ apply_scale(-SCALE_LIMIT);
+ delta += SCALE_LIMIT;
+ }
+
+ f64 ds = view_s * delta * .1;
+ if (view_s + ds < EPSILON)
+ return;
+ f64 dx = (g_platform.cursor_x * 1. - g_platform.real_width * .5);
+ f64 dy = (g_platform.cursor_y * 1. - g_platform.real_height * .5);
+ view_x -= view_s * dx;
+ view_y -= view_s * dy;
+ view_s += ds;
+ view_x += view_s * dx;
+ view_y += view_s * dy;
+}
+
void update_and_render_frame(void) {
i32 num_events = p_handle_events();
@@ -44,13 +68,8 @@ void update_and_render_frame(void) {
}
if (g_platform.wheel_dy != 0.) {
- f64 dx = (g_platform.cursor_x * 1. - g_platform.real_width * .5);
- f64 dy = (g_platform.cursor_y * 1. - g_platform.real_height * .5);
- view_x -= view_s * dx;
- view_y -= view_s * dy;
- view_s += view_s * g_platform.wheel_dy * .1;
- view_x += view_s * dx;
- view_y += view_s * dy;
+ apply_scale(g_platform.wheel_dy / 2.0);
+ apply_scale(g_platform.wheel_dy / 2.0);
}
for (i32 j = 0; j < g_platform.frame_height; ++j)