2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_util.h"
|
|
|
|
#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
|
|
|
Color color,
|
|
|
|
float radius,
|
|
|
|
size_t num_vertices,
|
2024-10-14 01:32:59 +00:00
|
|
|
SDL_Vertex vertices[],
|
|
|
|
int indices[])
|
2024-07-27 12:10:19 +00:00
|
|
|
{
|
|
|
|
/* the angle (in radians) to rotate by on each iteration */
|
|
|
|
float seg_rotation_angle = (360.0f / (float)num_vertices) * ((float)M_PI / 180);
|
|
|
|
|
|
|
|
vertices[0].position.x = (float)position.x;
|
|
|
|
vertices[0].position.y = (float)position.y;
|
|
|
|
vertices[0].color.r = color.r;
|
|
|
|
vertices[0].color.g = color.g;
|
|
|
|
vertices[0].color.b = color.b;
|
|
|
|
vertices[0].color.a = color.a;
|
|
|
|
vertices[0].tex_coord = (SDL_FPoint){ 0, 0 };
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
vertices[i].position.x =
|
|
|
|
cosf(final_seg_rotation_angle) * start_x -
|
|
|
|
sinf(final_seg_rotation_angle) * start_y;
|
|
|
|
vertices[i].position.y =
|
|
|
|
cosf(final_seg_rotation_angle) * start_y +
|
|
|
|
sinf(final_seg_rotation_angle) * start_x;
|
|
|
|
|
|
|
|
vertices[i].position.x += position.x;
|
|
|
|
vertices[i].position.y += position.y;
|
|
|
|
|
|
|
|
vertices[i].color.r = color.r;
|
|
|
|
vertices[i].color.g = color.g;
|
|
|
|
vertices[i].color.b = color.b;
|
|
|
|
vertices[i].color.a = color.a;
|
|
|
|
|
|
|
|
vertices[i].tex_coord = (SDL_FPoint){ 0, 0 };
|
|
|
|
|
|
|
|
|
|
|
|
size_t triangle_offset = 3 * (i - 1);
|
|
|
|
|
|
|
|
/* center point index */
|
|
|
|
indices[triangle_offset] = 0;
|
|
|
|
/* generated point index */
|
|
|
|
indices[triangle_offset + 1] = (int)i;
|
|
|
|
|
|
|
|
size_t index = (i + 1) % num_vertices;
|
|
|
|
if (index == 0)
|
|
|
|
index = num_vertices;
|
|
|
|
indices[triangle_offset + 2] = (int)index;
|
|
|
|
}
|
|
|
|
}
|