Drawing wall splines

This commit is contained in:
Luka Jankovic 2025-06-16 23:44:44 +02:00
parent 09cc69d38d
commit b217a72e6d
4 changed files with 27 additions and 29 deletions

View file

@ -40,11 +40,15 @@ bool kubo_context_add_wall(struct kubo_context *context,
struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) { struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) {
if (context->num_walls > 0 && if (context->num_walls > 0) {
(context->walls[context->num_walls - 1]->wall_state == KUBO_WALL_INIT || switch (context->walls[context->num_walls - 1]->state) {
context->walls[context->num_walls - 1]->wall_state == case KUBO_WALL_INIT:
KUBO_WALL_STARTED)) { case KUBO_WALL_DRAWING:
return context->walls[context->num_walls - 1]; return context->walls[context->num_walls - 1];
break;
default:
break;
}
} }
struct kubo_wall *wall = kubo_wall_init(); struct kubo_wall *wall = kubo_wall_init();

View file

@ -8,6 +8,8 @@ struct kubo_wall *kubo_wall_init() {
wall->num_vertices_alloc = KUBO_WALL_INIT_VERTICES; wall->num_vertices_alloc = KUBO_WALL_INIT_VERTICES;
wall->vertices = malloc(sizeof(Vector2) * wall->num_vertices_alloc); wall->vertices = malloc(sizeof(Vector2) * wall->num_vertices_alloc);
wall->num_vertices = 0;
wall->state = KUBO_WALL_INIT;
return wall; return wall;
} }

View file

@ -8,7 +8,7 @@
enum kubo_wall_state { enum kubo_wall_state {
KUBO_WALL_INIT, KUBO_WALL_INIT,
KUBO_WALL_STARTED, KUBO_WALL_DRAWING,
KUBO_WALL_DONE KUBO_WALL_DONE
}; };
@ -17,7 +17,7 @@ struct kubo_wall {
unsigned int num_vertices; unsigned int num_vertices;
unsigned int num_vertices_alloc; unsigned int num_vertices_alloc;
enum kubo_wall_state wall_state; enum kubo_wall_state state;
}; };
struct kubo_wall *kubo_wall_init(); struct kubo_wall *kubo_wall_init();

View file

@ -34,13 +34,15 @@ void kubo_window_render(struct kubo_context *context) {
} }
for (int i = 0; i < context->num_walls; i++) { for (int i = 0; i < context->num_walls; i++) {
if (context->walls[i]->wall_state == KUBO_WALL_STARTED) { DrawSplineLinear(context->walls[i]->vertices,
context->walls[i]->num_vertices, 10.f, BLACK);
if (context->walls[i]->state == KUBO_WALL_STARTED) {
Vector2 mouse_pos = GetMousePosition(); Vector2 mouse_pos = GetMousePosition();
Vector2 points[] = {context->walls[i]->vertices[0], mouse_pos}; Vector2 points[] = {
context->walls[i]
->vertices[context->walls[i]->num_vertices - 1],
mouse_pos};
DrawSplineLinear(points, 2, 10.f, BLACK); DrawSplineLinear(points, 2, 10.f, BLACK);
} else if (context->walls[i]->wall_state == KUBO_WALL_DONE) {
DrawSplineLinear(context->walls[i]->vertices,
context->walls[i]->num_vertices, 10.f, BLACK);
} }
} }
@ -49,26 +51,16 @@ void kubo_window_render(struct kubo_context *context) {
void kubo_window_input(struct kubo_context *context) { void kubo_window_input(struct kubo_context *context) {
if (IsKeyPressed(KEY_Q)) { if (IsKeyPressed(KEY_Q)) {
printf("q\n");
context->exit_pending = true; context->exit_pending = true;
} }
struct kubo_wall *current_wall = kubo_context_get_pending_wall(context);
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
Vector2 new_pos = GetMousePosition();
struct kubo_wall *current_wall = kubo_context_get_pending_wall(context); kubo_wall_append(current_wall, new_pos);
current_wall->state = KUBO_WALL_STARTED;
if (current_wall->wall_state == KUBO_WALL_INIT) { } else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
printf("new wall\n"); current_wall->state = KUBO_WALL_DONE;
Vector2 start_pos = GetMousePosition();
kubo_wall_append(current_wall, start_pos);
current_wall->wall_state = KUBO_WALL_STARTED;
} else if (current_wall->wall_state == KUBO_WALL_STARTED) {
printf("started wall\n");
Vector2 end_pos = GetMousePosition();
kubo_wall_append(current_wall, end_pos);
current_wall->wall_state = KUBO_WALL_DONE;
} else {
printf("neither?\n");
}
} }
} }