Commit e6a220e402d5c53e502ef67a3793ba878b3db517

Thomas de Grivel 2024-01-18T19:40:01

demo cairo mandelbrot (f128)

diff --git a/libc3/types.h b/libc3/types.h
index 4bec3df..d74e700 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -186,6 +186,8 @@ typedef u64          t_skiplist_height;
 
 /* function typedefs */
 typedef bool (* f_sequence) (s_sequence *seq);
+typedef bool (* f_sequence_button) (s_sequence *seq, u8 button, sw x,
+                                    sw y);
 
 #define CHARACTER_MAX S32_MAX
 #define SKIPLIST_HEIGHT_MAX U64_MAX
@@ -474,6 +476,7 @@ struct sequence {
   f_sequence load;
   f_sequence render;
   f_sequence unload;
+  f_sequence_button button;
 };
 
 #define TYPEDEF_SET_ITEM(name, type)                                   \
diff --git a/libc3/window/cairo/demo/mandelbrot_f128.c b/libc3/window/cairo/demo/mandelbrot_f128.c
index 5cbd1b4..0ec4c5a 100644
--- a/libc3/window/cairo/demo/mandelbrot_f128.c
+++ b/libc3/window/cairo/demo/mandelbrot_f128.c
@@ -19,6 +19,44 @@ static cairo_surface_t *g_mandelbrot_f128_surface = NULL;
 static bool mandelbrot_f128_resize (s_sequence *seq);
 static bool mandelbrot_f128_update (s_sequence *seq);
 
+bool mandelbrot_f128_button (s_sequence *seq, u8 button, sw x, sw y)
+{
+  s_map *map;
+  f128 *next_x;
+  f128 *next_y;
+  f128 *next_z;
+  s_window_cairo *win;
+  assert(seq);
+  win = seq->window;
+  assert(win);
+  assert(seq->tag.type == TAG_MAP);
+  map = &seq->tag.data.map;
+  assert(map->count == 9);
+  assert(map->key[1].type == TAG_SYM);
+  assert(map->key[1].data.sym == sym_1("next_x"));
+  assert(map->value[1].type == TAG_F128);
+  next_x = &map->value[1].data.f128;
+  assert(map->key[2].type == TAG_SYM);
+  assert(map->key[2].data.sym == sym_1("next_y"));
+  assert(map->value[2].type == TAG_F128);
+  next_y = &map->value[2].data.f128;
+  assert(map->key[3].type == TAG_SYM);
+  assert(map->key[3].data.sym == sym_1("next_z"));
+  assert(map->value[3].type == TAG_F128);
+  next_z = &map->value[3].data.f128;
+  if (button == 1) {
+    *next_x = *next_x + *next_z * (x - (f128) win->w / 2);
+    *next_y = *next_y + *next_z * (y - (f128) win->h / 2);
+  }
+  else if (button == 5) {
+    *next_z = *next_z * exp2l(0.5);
+  }
+  else if (button == 4) {
+    *next_z = *next_z * exp2l(-0.5);
+  }
+  return true;
+}
+
 bool mandelbrot_f128_load (s_sequence *seq)
 {
   s_map *map;
diff --git a/libc3/window/cairo/demo/mandelbrot_f128.h b/libc3/window/cairo/demo/mandelbrot_f128.h
index 43a2047..02414ec 100644
--- a/libc3/window/cairo/demo/mandelbrot_f128.h
+++ b/libc3/window/cairo/demo/mandelbrot_f128.h
@@ -15,6 +15,7 @@
 
 #include "../types.h"
 
+bool mandelbrot_f128_button (s_sequence *seq, u8 button, sw x, sw y);
 bool mandelbrot_f128_load (s_sequence *seq);
 bool mandelbrot_f128_render (s_sequence *seq);
 bool mandelbrot_f128_unload (s_sequence *seq);
diff --git a/libc3/window/cairo/demo/window_cairo_demo.c b/libc3/window/cairo/demo/window_cairo_demo.c
index a65ac08..f2f7067 100644
--- a/libc3/window/cairo/demo/window_cairo_demo.c
+++ b/libc3/window/cairo/demo/window_cairo_demo.c
@@ -40,6 +40,9 @@ bool window_cairo_demo_button (s_window_cairo *window, u8 button,
   io_write_1(", ");
   io_inspect_sw(&y);
   io_puts(")");
+  if (window->seq->button &&
+      ! window->seq->button(window->seq, button, x, y))
+    return false;
   return true;
 }
 
@@ -114,6 +117,7 @@ bool window_cairo_demo_load (s_window_cairo *window)
   sequence_init(window->sequence + 4, 3600.0, "05. Mandelbrot (f128)",
                 mandelbrot_f128_load, mandelbrot_f128_render,
                 mandelbrot_f128_unload, window);
+  window->sequence[4].button = mandelbrot_f128_button;
   window_set_sequence_pos((s_window *) window, 0);
   return true;
 }