From b217a72e6d2df28105977188914484300dbd49ea Mon Sep 17 00:00:00 2001 From: Luka Jankovic Date: Mon, 16 Jun 2025 23:44:44 +0200 Subject: [PATCH] Drawing wall splines --- kubo_context.c | 14 +++++++++----- kubo_wall.c | 2 ++ kubo_wall.h | 4 ++-- kubo_window.c | 36 ++++++++++++++---------------------- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/kubo_context.c b/kubo_context.c index f6ebe11..a22596c 100644 --- a/kubo_context.c +++ b/kubo_context.c @@ -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) { - if (context->num_walls > 0 && - (context->walls[context->num_walls - 1]->wall_state == KUBO_WALL_INIT || - context->walls[context->num_walls - 1]->wall_state == - KUBO_WALL_STARTED)) { - return context->walls[context->num_walls - 1]; + if (context->num_walls > 0) { + switch (context->walls[context->num_walls - 1]->state) { + case KUBO_WALL_INIT: + case KUBO_WALL_DRAWING: + return context->walls[context->num_walls - 1]; + break; + default: + break; + } } struct kubo_wall *wall = kubo_wall_init(); diff --git a/kubo_wall.c b/kubo_wall.c index abe856b..5f4bbbd 100644 --- a/kubo_wall.c +++ b/kubo_wall.c @@ -8,6 +8,8 @@ struct kubo_wall *kubo_wall_init() { wall->num_vertices_alloc = KUBO_WALL_INIT_VERTICES; wall->vertices = malloc(sizeof(Vector2) * wall->num_vertices_alloc); + wall->num_vertices = 0; + wall->state = KUBO_WALL_INIT; return wall; } diff --git a/kubo_wall.h b/kubo_wall.h index f7e7ac3..3e39e8a 100644 --- a/kubo_wall.h +++ b/kubo_wall.h @@ -8,7 +8,7 @@ enum kubo_wall_state { KUBO_WALL_INIT, - KUBO_WALL_STARTED, + KUBO_WALL_DRAWING, KUBO_WALL_DONE }; @@ -17,7 +17,7 @@ struct kubo_wall { unsigned int num_vertices; unsigned int num_vertices_alloc; - enum kubo_wall_state wall_state; + enum kubo_wall_state state; }; struct kubo_wall *kubo_wall_init(); diff --git a/kubo_window.c b/kubo_window.c index 7087ddc..2141b84 100644 --- a/kubo_window.c +++ b/kubo_window.c @@ -34,13 +34,15 @@ void kubo_window_render(struct kubo_context *context) { } 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 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); - } 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) { if (IsKeyPressed(KEY_Q)) { - printf("q\n"); context->exit_pending = true; } + struct kubo_wall *current_wall = kubo_context_get_pending_wall(context); + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - - struct kubo_wall *current_wall = kubo_context_get_pending_wall(context); - - if (current_wall->wall_state == KUBO_WALL_INIT) { - printf("new wall\n"); - 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"); - } + Vector2 new_pos = GetMousePosition(); + kubo_wall_append(current_wall, new_pos); + current_wall->state = KUBO_WALL_STARTED; + } else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) { + current_wall->state = KUBO_WALL_DONE; } }