summaryrefslogtreecommitdiff
path: root/examples/julia_set.c
blob: 0d6c5be3a910fe041b0aead2bbd0e8347f2933ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#if 0 /*
#/  ================================================================
#/
#/    julia_set.c
#/
#/  ================================================================
#/
#/    Self-compilation shell script
#/
SRC=${0##*./}
BIN=${SRC%.*}
gcc                                         \
  -Wall -Wextra -Werror -pedantic           \
  -Wno-old-style-declaration                \
  -Wno-missing-braces                       \
  -Wno-unused-variable                      \
  -Wno-unused-but-set-variable              \
  -Wno-unused-parameter                     \
  -Wno-overlength-strings                   \
  -g -O3                                    \
  -fsanitize=undefined,address,leak -mshstk \
  -lX11 -lm                                 \
  -o $BIN $SRC &&                           \
  ./$BIN $@ && rm $BIN
exit $? # */
#endif

#include "../reduced_system_layer.c"

i64 p      = 4;
f64 view_x = 0.;
f64 view_y = 0.;
f64 view_s = 1.;
f64 cx     = -.771;
f64 cy     = .1005;
f64 radius = 2.;
i64 limit  = 1024;
i64 time_0;

void update_and_render_frame(void) {
  i32 num_events = p_handle_events();

  i64 time_elapsed = p_time() - time_0;
  time_0 += time_elapsed;

  b8 left  = platform.key_down[KEY_LEFT];
  b8 right = platform.key_down[KEY_RIGHT];
  b8 up    = platform.key_down[KEY_UP];
  b8 down  = platform.key_down[KEY_DOWN];

  if (!left && !right && !up && !down && num_events == 0) {
    p_sleep_for(1);
    return;
  }

  if (platform.key_pressed['\n'])
    p = (p == 1 ? 4 : 1);

  if (platform.key_pressed[KEY_ESCAPE]) {
    view_x = 0.;
    view_y = 0.;
    view_s = 1.;
  }

  f64 d = (platform.key_down[MOD_CTRL] ? .00005 : .001) * view_s * time_elapsed;

  if (left)  { cx -= d; cy -= d; }
  if (right) { cx += d; cy += d; }
  if (up)    { cx += d; cy -= d; }
  if (down)  { cx -= d; cy += d; }

  if (platform.key_down[BUTTON_LEFT]) {
    view_x += platform.cursor_dx * view_s;
    view_y += platform.cursor_dy * view_s;
  }

  view_s += .1 * platform.wheel_dy * view_s;

  for (i32 j = 0; j + p <= platform.frame_height; j += p)
    for (i32 i = 0; i + p <= platform.frame_width; i += p) {
      f64 x = .003 * ((i - platform.frame_width  * .5) * view_s - view_x);
      f64 y = .003 * ((j - platform.frame_height * .5) * view_s - view_y);

      i64 n = 0;

      for (; x * x + y * y < radius * radius && n < limit; ++n) {
        f64 z = x * x - y * y;
        y     = 2. * x * y  + cy;
        x     = z + cx;
      }

      u32 c;

      if (n == limit)
        c = 0;
      else
        c = 0xffffff - n * 8 - n * 256 * 4;

      for (i32 jj = 0; jj < p; ++jj)
        for (i32 ii = 0; ii < p; ++ii)
          platform.pixels[(j + jj) * platform.frame_width + (i + ii)] = c;
    }

  p_render_frame();
}

i32 main(i32 argc, c8 **argv) {
  (void) argc;
  (void) argv;

  platform = (Platform) {
    .title        = "Julia Set",
    .frame_width  = 960,
    .frame_height = 720,
  };

  time_0 = p_time();

  p_event_loop();

  return 0;
}