diff options
Diffstat (limited to 'examples/julia_set.c')
-rwxr-xr-x | examples/julia_set.c | 146 |
1 files changed, 72 insertions, 74 deletions
diff --git a/examples/julia_set.c b/examples/julia_set.c index 747c339..0d6c5be 100755 --- a/examples/julia_set.c +++ b/examples/julia_set.c @@ -27,98 +27,96 @@ exit $? # */ #include "../reduced_system_layer.c" -i32 main(i32 argc, c8 **argv) { - (void) argc; - (void) argv; - - platform = (Platform) { - .title = "Julia Set", - .frame_width = 960, - .frame_height = 720, - }; - - p_init(); - - i64 p = 4; +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; + } - f64 view_x = 0.; - f64 view_y = 0.; - f64 view_s = 1.; + if (platform.key_pressed['\n']) + p = (p == 1 ? 4 : 1); - f64 cx = -.771; - f64 cy = .1005; - f64 radius = 2.; - i64 limit = 1024; + if (platform.key_pressed[KEY_ESCAPE]) { + view_x = 0.; + view_y = 0.; + view_s = 1.; + } - i64 time = p_time(); + f64 d = (platform.key_down[MOD_CTRL] ? .00005 : .001) * view_s * time_elapsed; - while (!platform.done) { - i32 num_events = p_handle_events(); + if (left) { cx -= d; cy -= d; } + if (right) { cx += d; cy += d; } + if (up) { cx += d; cy -= d; } + if (down) { cx -= d; cy += d; } - i64 time_elapsed = p_time() - time; - time += time_elapsed; + if (platform.key_down[BUTTON_LEFT]) { + view_x += platform.cursor_dx * view_s; + view_y += platform.cursor_dy * view_s; + } - 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]; + view_s += .1 * platform.wheel_dy * view_s; - if (!left && !right && !up && !down && num_events == 0) { - p_sleep_for(1); - continue; - } + 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); - if (platform.key_pressed['\n']) - p = (p == 1 ? 4 : 1); + i64 n = 0; - if (platform.key_pressed[KEY_ESCAPE]) { - view_x = 0.; - view_y = 0.; - view_s = 1.; - } + for (; x * x + y * y < radius * radius && n < limit; ++n) { + f64 z = x * x - y * y; + y = 2. * x * y + cy; + x = z + cx; + } - f64 d = (platform.key_down[MOD_CTRL] ? .00005 : .001) * view_s * time_elapsed; + u32 c; - 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 (n == limit) + c = 0; + else + c = 0xffffff - n * 8 - n * 256 * 4; - if (platform.key_down[BUTTON_LEFT]) { - view_x += platform.cursor_dx * view_s; - view_y += platform.cursor_dy * view_s; + for (i32 jj = 0; jj < p; ++jj) + for (i32 ii = 0; ii < p; ++ii) + platform.pixels[(j + jj) * platform.frame_width + (i + ii)] = c; } - 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; - } + p_render_frame(); +} - u32 c; +i32 main(i32 argc, c8 **argv) { + (void) argc; + (void) argv; - if (n == limit) - c = 0; - else - c = 0xffffff - n * 8 - n * 256 * 4; + platform = (Platform) { + .title = "Julia Set", + .frame_width = 960, + .frame_height = 720, + }; - for (i32 jj = 0; jj < p; ++jj) - for (i32 ii = 0; ii < p; ++ii) - platform.pixels[(j + jj) * platform.frame_width + (i + ii)] = c; - } + time_0 = p_time(); - p_render_frame(); - } + p_event_loop(); - p_cleanup(); return 0; } |