From 024349a59f110a6078911cdd70ac43de34e49bc9 Mon Sep 17 00:00:00 2001 From: Luka Jankovic Date: Fri, 25 Jul 2025 15:30:08 +0200 Subject: [PATCH] loading scene from file --- kubo_context.c | 3 ++- kubo_file.c | 22 ++++++++++++++++++---- kubo_file.h | 13 ++++++++----- kubo_wall.c | 12 ++++-------- kubo_wall.h | 2 +- main.c | 8 ++++---- scene.yaml | 13 +++++++------ 7 files changed, 44 insertions(+), 29 deletions(-) diff --git a/kubo_context.c b/kubo_context.c index 439515c..a9b93e9 100644 --- a/kubo_context.c +++ b/kubo_context.c @@ -105,7 +105,8 @@ struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) { } } - struct kubo_wall *wall = kubo_wall_init(); + struct kubo_wall *wall = malloc(sizeof(struct kubo_wall)); + kubo_wall_init(wall); kubo_wall_arr_add(&context->walls, wall); return wall; diff --git a/kubo_file.c b/kubo_file.c index 831a478..979738b 100644 --- a/kubo_file.c +++ b/kubo_file.c @@ -1,6 +1,6 @@ #include "kubo_file.h" -void kubo_file_parse(char *file_name) { +struct kubo_context kubo_file_parse(char *file_name) { static const cyaml_config_t config = { .log_fn = cyaml_log, .mem_fn = cyaml_mem, @@ -15,16 +15,30 @@ void kubo_file_parse(char *file_name) { printf("cyaml err\n"); } - for (unsigned i = 0; i < n->walls_count; i++) { + struct kubo_context context; + kubo_context_init(&context); + + for (size_t i = 0; i < n->walls_count; i++) { printf("wall:\n"); - for (unsigned j = 0; j < n->walls[i].vertices_count; j++) { - printf("\t%f:%f\n", n->walls[i].vertices[j][0], + struct kubo_wall *wall = malloc(sizeof(struct kubo_wall)); + kubo_wall_init(wall); + for (size_t j = 0; j < n->walls[i].vertices_count; j++) { + printf("\t%i:%i\n", n->walls[i].vertices[j][0], n->walls[i].vertices[j][1]); + Vector2 vertex = { + .x = n->walls[i].vertices[j][0], + .y = n->walls[i].vertices[j][1] + }; + kubo_wall_add_vertex(wall, vertex); } + + kubo_wall_arr_add(&context.walls, wall); } err = cyaml_free(&config, &top_schema, n, 0); if (err != CYAML_OK) { printf("cyaml free err\n"); } + + return context; } diff --git a/kubo_file.h b/kubo_file.h index 48e5bcc..edc1bf1 100644 --- a/kubo_file.h +++ b/kubo_file.h @@ -23,7 +23,10 @@ #include #include -typedef float kubo_file_vertex[2]; +#include "kubo_context.h" +#include "kubo_wall.h" + +typedef int kubo_file_vertex[2]; struct kubo_file_wall { kubo_file_vertex *vertices; @@ -35,12 +38,12 @@ struct kubo_file_scene { unsigned walls_count; }; -static const cyaml_schema_value_t float_entry = { - CYAML_VALUE_FLOAT(CYAML_FLAG_DEFAULT, float), +static const cyaml_schema_value_t int_entry = { + CYAML_VALUE_INT(CYAML_FLAG_DEFAULT, int), }; static const cyaml_schema_value_t vertex_entry = { - CYAML_VALUE_SEQUENCE_FIXED(CYAML_FLAG_DEFAULT, float, &float_entry, 2), + CYAML_VALUE_SEQUENCE_FIXED(CYAML_FLAG_DEFAULT, int, &int_entry, 2), }; static const cyaml_schema_field_t wall_fields[] = { @@ -62,6 +65,6 @@ static const cyaml_schema_value_t top_schema = { scene_fields), }; -void kubo_file_parse(char *file_name); +struct kubo_context kubo_file_parse(char *file_name); #endif diff --git a/kubo_wall.c b/kubo_wall.c index 6cefa8a..4539641 100644 --- a/kubo_wall.c +++ b/kubo_wall.c @@ -18,17 +18,13 @@ #include "kubo_wall.h" -struct kubo_wall *kubo_wall_init() { - struct kubo_wall *wall = malloc(sizeof(struct kubo_wall)); - - if (!wall) - return NULL; +void kubo_wall_init(struct kubo_wall *wall) { + if (!wall) { + return; + } wall->state = KUBO_WALL_INIT; - kubo_vector2_arr_init(&wall->vertices); - - return wall; } void kubo_wall_cleanup(struct kubo_wall *wall) { diff --git a/kubo_wall.h b/kubo_wall.h index 52edf16..d9001ae 100644 --- a/kubo_wall.h +++ b/kubo_wall.h @@ -40,7 +40,7 @@ struct kubo_wall { enum kubo_wall_state state; }; -struct kubo_wall *kubo_wall_init(); +void kubo_wall_init(struct kubo_wall *wall); void kubo_wall_cleanup(struct kubo_wall *wall); bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec); diff --git a/main.c b/main.c index d6f5566..4eeac79 100644 --- a/main.c +++ b/main.c @@ -8,13 +8,13 @@ int main(int argc, char *argv[]) { - if (argc == 2) { - kubo_file_parse(argv[1]); - } - struct kubo_context context; kubo_context_init(&context); + if (argc == 2) { + context = kubo_file_parse(argv[1]); + } + kubo_window_init(); while (!kubo_window_should_close(&context)) { diff --git a/scene.yaml b/scene.yaml index 8959919..2a426ec 100644 --- a/scene.yaml +++ b/scene.yaml @@ -1,10 +1,11 @@ walls: - vertices: - - [0.0, 1.0] - - [1.0, 2.0] + - [100, 100] + - [100, 200] - vertices: - - [1.0, 3.0] - - [2.0, 1.0] + - [100, 300] + - [100, 500] + - [200, 500] - vertices: - - [3.0, 5.0] - - [2.0, 1.0] + - [300, 300] + - [500, 500]