From 09cc69d38da3d7f60ccea589580d532aac69f6b6 Mon Sep 17 00:00:00 2001 From: Luka Jankovic Date: Sun, 15 Jun 2025 17:11:20 +0200 Subject: [PATCH] Init, drawing walls semi-works --- CMakeLists.txt | 20 ++++++++++++++ kubo_context.c | 54 ++++++++++++++++++++++++++++++++++++ kubo_context.h | 28 +++++++++++++++++++ kubo_wall.c | 40 +++++++++++++++++++++++++++ kubo_wall.h | 28 +++++++++++++++++++ kubo_window.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ kubo_window.h | 22 +++++++++++++++ main.c | 21 ++++++++++++++ 8 files changed, 287 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 kubo_context.c create mode 100644 kubo_context.h create mode 100644 kubo_wall.c create mode 100644 kubo_wall.h create mode 100644 kubo_window.c create mode 100644 kubo_window.h create mode 100644 main.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ad37d90 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.11...3.31) + +project(kubo LANGUAGES C) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +set(RAYLIB_VERSION 5.5) +find_package(raylib ${RAYLIB_VERSION} REQUIRED) + +set(SOURCES + kubo_wall.c + kubo_window.c + kubo_context.c + main.c +) + +add_executable(kubo ${SOURCES}) + +target_link_libraries(kubo raylib) diff --git a/kubo_context.c b/kubo_context.c new file mode 100644 index 0000000..f6ebe11 --- /dev/null +++ b/kubo_context.c @@ -0,0 +1,54 @@ +#include "kubo_context.h" + +struct kubo_context *kubo_context_init() { + struct kubo_context *context = malloc(sizeof(struct kubo_context)); + + context->num_walls_alloc = KUBO_CONTEXT_NUM_WALLS_ALLOC; + context->walls = + malloc(sizeof(struct kubo_wall) * context->num_walls_alloc); + + return context; +} + +void kubo_context_cleanup(struct kubo_context *context) { + free(context->walls); + free(context); +} + +bool kubo_context_add_wall(struct kubo_context *context, + struct kubo_wall *wall) { + + struct kubo_wall **new_walls; + + if (context->num_walls == context->num_walls_alloc - 1) { + + context->num_walls_alloc *= 2; + + new_walls = realloc(context->walls, sizeof(struct kubo_wall *) * + context->num_walls_alloc); + + if (!new_walls) + return false; + + context->walls = new_walls; + } + + context->walls[context->num_walls++] = wall; + + return true; +} + +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]; + } + + struct kubo_wall *wall = kubo_wall_init(); + kubo_context_add_wall(context, wall); + + return wall; +} diff --git a/kubo_context.h b/kubo_context.h new file mode 100644 index 0000000..f260c46 --- /dev/null +++ b/kubo_context.h @@ -0,0 +1,28 @@ +#ifndef KUBO_CONTEXT_H +#define KUBO_CONTEXT_H + +#include +#include +#include + +#include "kubo_wall.h" + +#define KUBO_CONTEXT_NUM_WALLS_ALLOC 4 + +struct kubo_context { + bool exit_pending; + + struct kubo_wall **walls; + unsigned int num_walls; + unsigned int num_walls_alloc; +}; + +struct kubo_context *kubo_context_init(); +void kubo_context_cleanup(struct kubo_context *context); + +bool kubo_context_add_wall(struct kubo_context *context, + struct kubo_wall *wall); + +struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context); + +#endif diff --git a/kubo_wall.c b/kubo_wall.c new file mode 100644 index 0000000..abe856b --- /dev/null +++ b/kubo_wall.c @@ -0,0 +1,40 @@ +#include "kubo_wall.h" + +struct kubo_wall *kubo_wall_init() { + struct kubo_wall *wall = malloc(sizeof(struct kubo_wall)); + + if (!wall) + return NULL; + + wall->num_vertices_alloc = KUBO_WALL_INIT_VERTICES; + wall->vertices = malloc(sizeof(Vector2) * wall->num_vertices_alloc); + + return wall; +} + +void kubo_wall_cleanup(struct kubo_wall *wall) { + free(wall->vertices); + free(wall); +} + +bool kubo_wall_append(struct kubo_wall *wall, Vector2 vertex) { + + Vector2 *new_vertices; + + if (wall->num_vertices == wall->num_vertices_alloc - 1) { + + wall->num_vertices_alloc *= 2; + + new_vertices = + realloc(wall->vertices, sizeof(Vector2) * wall->num_vertices_alloc); + + if (!new_vertices) + return false; + + wall->vertices = new_vertices; + } + + wall->vertices[wall->num_vertices++] = vertex; + + return true; +} diff --git a/kubo_wall.h b/kubo_wall.h new file mode 100644 index 0000000..f7e7ac3 --- /dev/null +++ b/kubo_wall.h @@ -0,0 +1,28 @@ +#ifndef KUBO_WALL_H +#define KUBO_WALL_H + +#include +#include + +#define KUBO_WALL_INIT_VERTICES 4 + +enum kubo_wall_state { + KUBO_WALL_INIT, + KUBO_WALL_STARTED, + KUBO_WALL_DONE +}; + +struct kubo_wall { + Vector2 *vertices; + unsigned int num_vertices; + unsigned int num_vertices_alloc; + + enum kubo_wall_state wall_state; +}; + +struct kubo_wall *kubo_wall_init(); +void kubo_wall_cleanup(struct kubo_wall *wall); + +bool kubo_wall_append(struct kubo_wall *wall, Vector2 vertex); + +#endif diff --git a/kubo_window.c b/kubo_window.c new file mode 100644 index 0000000..7087ddc --- /dev/null +++ b/kubo_window.c @@ -0,0 +1,74 @@ +#include "kubo_window.h" + +void kubo_window_init(struct kubo_context *context) { + InitWindow(KUBO_WINDOW_WIDTH, KUBO_WINDOW_HEIGHT, "Kubo"); +} + +void kubo_window_cleanup(struct kubo_context *context) { CloseWindow(); } + +bool kubo_window_should_close(struct kubo_context *context) { + return context->exit_pending || WindowShouldClose(); +} + +void kubo_window_tick(struct kubo_context *context) { + kubo_window_render(context); + kubo_window_input(context); +} + +Vector2 start; +Vector2 end; +int state; + +void kubo_window_render(struct kubo_context *context) { + BeginDrawing(); + ClearBackground(WHITE); + + if (state == 1) { + Vector2 mouse_pos = GetMousePosition(); + Vector2 points[] = {start, mouse_pos}; + DrawSplineLinear(points, 2, 10.f, BLACK); + } else if (state == 2) { + Vector2 mouse_pos = GetMousePosition(); + Vector2 points[] = {start, end}; + DrawSplineLinear(points, 2, 10.f, BLACK); + } + + for (int i = 0; i < context->num_walls; i++) { + if (context->walls[i]->wall_state == KUBO_WALL_STARTED) { + Vector2 mouse_pos = GetMousePosition(); + Vector2 points[] = {context->walls[i]->vertices[0], 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); + } + } + + EndDrawing(); +} + +void kubo_window_input(struct kubo_context *context) { + if (IsKeyPressed(KEY_Q)) { + printf("q\n"); + context->exit_pending = true; + } + + 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"); + } + } +} diff --git a/kubo_window.h b/kubo_window.h new file mode 100644 index 0000000..f84c69a --- /dev/null +++ b/kubo_window.h @@ -0,0 +1,22 @@ +#ifndef KUBO_WINDOW_H +#define KUBO_WINDOW_H + +#include +#include + +#include "kubo_context.h" + +#define KUBO_WINDOW_WIDTH 1000 +#define KUBO_WINDOW_HEIGHT 800 + +void kubo_window_init(struct kubo_context *context); +void kubo_window_cleanup(struct kubo_context *context); + +bool kubo_window_should_close(struct kubo_context *context); + +void kubo_window_tick(struct kubo_context *context); + +void kubo_window_render(struct kubo_context *context); +void kubo_window_input(struct kubo_context *context); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..60bde30 --- /dev/null +++ b/main.c @@ -0,0 +1,21 @@ +#include +#include + +#include "kubo_context.h" +#include "kubo_window.h" + +int main(int argc, char *argv[]) { + + struct kubo_context *context = kubo_context_init(); + + kubo_window_init(context); + + while (!kubo_window_should_close(context)) { + kubo_window_tick(context); + } + + kubo_window_cleanup(context); + kubo_context_cleanup(context); + + return 0; +}