This repository has been archived on 2025-02-04. You can view files and clone it, but cannot push or open issues or pull requests.
helpless/oogabooga/examples/growing_array_example.c
2024-07-23 21:37:34 +02:00

74 lines
No EOL
2.6 KiB
C

typedef struct Circle {
Vector2 pos;
float radius;
} Circle;
int entry(int argc, char **argv) {
window.title = STR("Minimal Game Example");
window.scaled_width = 1280; // We need to set the scaled size if we want to handle system scaling (DPI)
window.scaled_height = 720;
window.x = 200;
window.y = 90;
window.clear_color = hex_to_rgba(0x6495EDff);
Circle *circles;
growing_array_init((void**)&circles, sizeof(Circle), get_heap_allocator());
const int num_circles = 10000;
const float radius_min = 8.0;
const float radius_max = 32.0;
const float hover_radius = 200;
os_update(); // We set scaled window size, os_update updates the pixel window size values for us
for (int i = 0; i < num_circles; i++) {
Circle c;
c.radius = get_random_float32_in_range(radius_min, radius_max);
c.pos.x = get_random_float32_in_range(-(f32)window.width/2.0+c.radius, (f32)window.width/2.0-c.radius);
c.pos.y = get_random_float32_in_range(-(f32)window.height/2.0+c.radius, (f32)window.height/2.0-c.radius);
growing_array_add((void**)&circles, &c);
assert(circles[i].radius == c.radius);
assert(circles[i].pos.x == c.pos.x);
assert(circles[i].pos.y == c.pos.y);
}
float64 last_time = os_get_current_time_in_seconds();
while (!window.should_close) {
float64 now = os_get_current_time_in_seconds();
if ((int)now != (int)last_time) log("%.2f FPS\n%.2fms", 1.0/(now-last_time), (now-last_time)*1000);
last_time = now;
reset_temporary_storage();
draw_frame.projection = m4_make_orthographic_projection(window.pixel_width * -0.5, window.pixel_width * 0.5, window.pixel_height * -0.5, window.pixel_height * 0.5, -1, 10);
float mx = input_frame.mouse_x - window.width/2;
float my = input_frame.mouse_y - window.height/2;
// We build an array on the fly each frame for all the hovered circles
Circle *circles_hovered;
growing_array_init_reserve((void**)&circles_hovered, sizeof(Circle), num_circles, get_temporary_allocator());
for (int i = 0; i < num_circles; i++) {
float distance = v2_length(v2_sub(v2(mx, my), circles[i].pos));
if (distance <= hover_radius) {
growing_array_add((void**)&circles_hovered, &circles[i]);
}
}
for (int i = 0; i < growing_array_get_valid_count(circles_hovered); i++) {
Circle c = circles_hovered[i];
draw_circle(v2_sub(c.pos, v2(c.radius, c.radius)), v2(c.radius, c.radius), COLOR_GREEN);
}
os_update();
gfx_update();
}
return 0;
}