#include "twn_engine_context_c.h" #include "twn_draw_c.h" #include "twn_draw.h" #include #include void draw_circle(Vec2 position, float radius, Color color) { CirclePrimitive circle = { .radius = radius, .color = color, .position = position, }; Primitive2D primitive = { .type = PRIMITIVE_2D_CIRCLE, .circle = circle, }; arrput(ctx.render_queue_2d, primitive); } void create_circle_geometry(Vec2 position, float radius, size_t num_vertices, Vec2 vertices[]) { SDL_assert(num_vertices <= CIRCLE_VERTICES_MAX); /* the angle (in radians) to rotate by on each iteration */ float seg_rotation_angle = (360.0f / (float)(num_vertices - 2)) * ((float)M_PI / 180); vertices[0].x = (float)position.x; vertices[0].y = (float)position.y; /* this point will rotate around the center */ float start_x = 0.0f - radius; float start_y = 0.0f; for (size_t i = 1; i < num_vertices - 1; ++i) { float final_seg_rotation_angle = (float)i * seg_rotation_angle; float c, s; sincosf(final_seg_rotation_angle, &s, &c); vertices[i].x = c * start_x - s * start_y; vertices[i].y = c * start_y + s * start_x; vertices[i].x += position.x; vertices[i].y += position.y; } // place a redundant vertex to make proper circling over shared element buffer { float final_seg_rotation_angle = (float)1 * seg_rotation_angle; float c, s; sincosf(final_seg_rotation_angle, &s, &c); vertices[num_vertices - 1].x = c * start_x - s * start_y; vertices[num_vertices - 1].y = c * start_y + s * start_x; vertices[num_vertices - 1].x += position.x; vertices[num_vertices - 1].y += position.y; } }