townengine/src/rendering/twn_circles.c

69 lines
1.9 KiB
C
Raw Normal View History

#include "twn_engine_context_c.h"
2024-10-07 14:53:09 +00:00
#include "twn_draw_c.h"
#include "twn_draw.h"
#include <SDL2/SDL.h>
#include <stb_ds.h>
2024-10-07 14:53:09 +00:00
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,
2024-10-07 14:53:09 +00:00
float radius,
size_t num_vertices,
Vec2 vertices[])
{
2024-10-19 16:16:18 +00:00
SDL_assert(num_vertices <= CIRCLE_VERTICES_MAX);
/* the angle (in radians) to rotate by on each iteration */
2024-10-19 16:16:18 +00:00
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;
2024-10-19 16:16:18 +00:00
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;
}
2024-10-19 16:16:18 +00:00
// 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;
}
}