2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_engine_context_c.h"
|
2024-10-07 14:53:09 +00:00
|
|
|
#include "twn_draw_c.h"
|
|
|
|
#include "twn_draw.h"
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
#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) {
|
2024-09-23 17:43:16 +00:00
|
|
|
CirclePrimitive circle = {
|
2024-07-27 12:10:19 +00:00
|
|
|
.radius = radius,
|
|
|
|
.color = color,
|
|
|
|
.position = position,
|
|
|
|
};
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Primitive2D primitive = {
|
2024-07-27 12:10:19 +00:00
|
|
|
.type = PRIMITIVE_2D_CIRCLE,
|
|
|
|
.circle = circle,
|
|
|
|
};
|
|
|
|
|
|
|
|
arrput(ctx.render_queue_2d, primitive);
|
|
|
|
}
|
|
|
|
|
2024-10-14 01:32:59 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void create_circle_geometry(Vec2 position,
|
2024-10-07 14:53:09 +00:00
|
|
|
float radius,
|
|
|
|
size_t num_vertices,
|
2024-10-17 18:01:35 +00:00
|
|
|
Vec2 vertices[])
|
2024-07-27 12:10:19 +00:00
|
|
|
{
|
2024-10-19 16:16:18 +00:00
|
|
|
SDL_assert(num_vertices <= CIRCLE_VERTICES_MAX);
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
/* 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);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-10-17 18:01:35 +00:00
|
|
|
vertices[0].x = (float)position.x;
|
|
|
|
vertices[0].y = (float)position.y;
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
/* 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) {
|
2024-07-27 12:10:19 +00:00
|
|
|
float final_seg_rotation_angle = (float)i * seg_rotation_angle;
|
|
|
|
|
2024-10-17 18:01:35 +00:00
|
|
|
float c, s;
|
|
|
|
sincosf(final_seg_rotation_angle, &s, &c);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-10-17 18:01:35 +00:00
|
|
|
vertices[i].x = c * start_x - s * start_y;
|
|
|
|
vertices[i].y = c * start_y + s * start_x;
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-10-17 18:01:35 +00:00
|
|
|
vertices[i].x += position.x;
|
|
|
|
vertices[i].y += position.y;
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|