diff options
Diffstat (limited to 'examples/gravity.c')
-rwxr-xr-x | examples/gravity.c | 169 |
1 files changed, 85 insertions, 84 deletions
diff --git a/examples/gravity.c b/examples/gravity.c index 246aab1..d21a4f5 100755 --- a/examples/gravity.c +++ b/examples/gravity.c @@ -40,116 +40,117 @@ typedef struct { Entity entities[10 * 1024 * 1024]; } World; -i32 main(i32 argc, c8 **argv) { - (void) argc; - (void) argv; +World world = {0}; +u32 background; +i64 time_0; - u32 background = u32_from_rgb(.0f, .0f, .08f); +void update_and_render_frame(void) { + p_handle_events(); - platform = (Platform) { - .title = "Gravity", - .frame_width = 960, - .frame_height = 720, - }; + i64 time_elapsed = p_time() - time_0; + time_0 += time_elapsed; - p_init(); + if (platform.key_pressed[BUTTON_LEFT]) { + i64 n = world.num_entities++; - srand(time(0)); + world.entities[n] = (Entity) { + .x = (f64) platform.cursor_x - (f64) platform.frame_width / 2, + .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2), + }; + } - static World world = {0}; - i64 time_0 = p_time(); + if (time_elapsed > 0) { + if (platform.key_down[BUTTON_RIGHT]) { + for (i64 n = 0; n < time_elapsed; ++n) + world.entities[world.num_entities + n] = (Entity) { + .x = (f64) platform.cursor_x - (f64) platform.frame_width / 2, + .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2), + }; - while (!platform.done) { - p_handle_events(); + world.num_entities += time_elapsed; + } - i64 time_elapsed = p_time() - time_0; - time_0 += time_elapsed; + for (i64 n = 0; n < world.num_entities; ++n) { + Entity *e = &world.entities[n]; - if (platform.key_pressed[BUTTON_LEFT]) { - i64 n = world.num_entities++; + for (i64 k = 0; k < n; ++k) { + Entity *u = &world.entities[k]; - world.entities[n] = (Entity) { - .x = (f64) platform.cursor_x - (f64) platform.frame_width / 2, - .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2), - }; - } + f64 dx = e->x - u->x; + f64 dy = e->y - u->y; + f64 d = dx * dx + dy * dy; + if (d < 1000.) continue; - if (time_elapsed > 0) { - if (platform.key_down[BUTTON_RIGHT]) { - for (i64 n = 0; n < time_elapsed; ++n) - world.entities[world.num_entities + n] = (Entity) { - .x = (f64) platform.cursor_x - (f64) platform.frame_width / 2, - .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2), - }; + f64 r = sqrt(d); + dx /= r; + dy /= r; - world.num_entities += time_elapsed; - } + f64 a = (1. / d) * time_elapsed; - for (i64 n = 0; n < world.num_entities; ++n) { - Entity *e = &world.entities[n]; + if (d < 5000.) + a = -a; - for (i64 k = 0; k < n; ++k) { - Entity *u = &world.entities[k]; + e->vx -= dx * a; + e->vy -= dy * a; + u->vx += dx * a; + u->vy += dy * a; + } + } - f64 dx = e->x - u->x; - f64 dy = e->y - u->y; - f64 d = dx * dx + dy * dy; - if (d < 1000.) continue; + for (i64 n = 0; n < world.num_entities; ++n) { + Entity *e = &world.entities[n]; - f64 r = sqrt(d); - dx /= r; - dy /= r; + e->x += e->vx * time_elapsed; + e->y += e->vy * time_elapsed; - f64 a = (1. / d) * time_elapsed; + f64 z = (M_PI * 2.0) * (rand() % 10000) * .0001; + e->vx += .0001 * cos(z); + e->vy += .0001 * sin(z); + } - if (d < 5000.) - a = -a; - - e->vx -= dx * a; - e->vy -= dy * a; - u->vx += dx * a; - u->vy += dy * a; - } - } + world.time += time_elapsed; + } - for (i64 n = 0; n < world.num_entities; ++n) { - Entity *e = &world.entities[n]; + for (i32 j = 0; j < platform.frame_height; ++j) + for (i32 i = 0; i < platform.frame_width; ++i) + platform.pixels[j * platform.frame_width + i] = background; - e->x += e->vx * time_elapsed; - e->y += e->vy * time_elapsed; + for (i64 n = 0; n < world.num_entities; ++n) { + Entity *e = &world.entities[n]; - f64 z = (M_PI * 2.0) * (rand() % 10000) * .0001; - e->vx += .0001 * cos(z); - e->vy += .0001 * sin(z); - } + i32 x = platform.frame_width / 2 + (i32) floor(e->x + .5); + i32 y = platform.frame_height / 2 - (i32) floor(e->y + .5); - world.time += time_elapsed; + for (i32 j = y - 10; j <= y + 10; ++j) { + if (j < 0 || j >= platform.frame_height) continue; + for (i32 i = x - 10; i <= x + 10; ++i) { + if (i < 0 || i >= platform.frame_width) continue; + if ((i - x) * (i - x) + (j - y) * (j - y) > 100) continue; + f64 v = (e->vx * e->vx + e->vy * e->vy) * 8.; + platform.pixels[j * platform.frame_width + i] = u32_from_rgb(-.2 + v * 2., .1 + v * .7, 1. - v); + } } + } - for (i32 j = 0; j < platform.frame_height; ++j) - for (i32 i = 0; i < platform.frame_width; ++i) - platform.pixels[j * platform.frame_width + i] = background; + p_render_frame(); +} - for (i64 n = 0; n < world.num_entities; ++n) { - Entity *e = &world.entities[n]; +i32 main(i32 argc, c8 **argv) { + (void) argc; + (void) argv; - i32 x = platform.frame_width / 2 + (i32) floor(e->x + .5); - i32 y = platform.frame_height / 2 - (i32) floor(e->y + .5); - - for (i32 j = y - 10; j <= y + 10; ++j) { - if (j < 0 || j >= platform.frame_height) continue; - for (i32 i = x - 10; i <= x + 10; ++i) { - if (i < 0 || i >= platform.frame_width) continue; - if ((i - x) * (i - x) + (j - y) * (j - y) > 100) continue; - f64 v = (e->vx * e->vx + e->vy * e->vy) * 8.; - platform.pixels[j * platform.frame_width + i] = u32_from_rgb(-.2 + v * 2., .1 + v * .7, 1. - v); - } - } - } + platform = (Platform) { + .title = "Gravity", + .frame_width = 960, + .frame_height = 720, + }; - p_render_frame(); - } + background = u32_from_rgb(.0f, .0f, .08f); + time_0 = p_time(); + + srand(p_time()); + + p_event_loop(); - p_cleanup(); return 0; } |