loading scene from file

This commit is contained in:
Luka Jankovic 2025-07-25 15:30:08 +02:00
parent 8554b33cd6
commit 024349a59f
7 changed files with 44 additions and 29 deletions

View file

@ -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); kubo_wall_arr_add(&context->walls, wall);
return wall; return wall;

View file

@ -1,6 +1,6 @@
#include "kubo_file.h" #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 = { static const cyaml_config_t config = {
.log_fn = cyaml_log, .log_fn = cyaml_log,
.mem_fn = cyaml_mem, .mem_fn = cyaml_mem,
@ -15,16 +15,30 @@ void kubo_file_parse(char *file_name) {
printf("cyaml err\n"); 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"); printf("wall:\n");
for (unsigned j = 0; j < n->walls[i].vertices_count; j++) { struct kubo_wall *wall = malloc(sizeof(struct kubo_wall));
printf("\t%f:%f\n", n->walls[i].vertices[j][0], 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]); 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); err = cyaml_free(&config, &top_schema, n, 0);
if (err != CYAML_OK) { if (err != CYAML_OK) {
printf("cyaml free err\n"); printf("cyaml free err\n");
} }
return context;
} }

View file

@ -23,7 +23,10 @@
#include <cyaml/cyaml.h> #include <cyaml/cyaml.h>
#include <stdio.h> #include <stdio.h>
typedef float kubo_file_vertex[2]; #include "kubo_context.h"
#include "kubo_wall.h"
typedef int kubo_file_vertex[2];
struct kubo_file_wall { struct kubo_file_wall {
kubo_file_vertex *vertices; kubo_file_vertex *vertices;
@ -35,12 +38,12 @@ struct kubo_file_scene {
unsigned walls_count; unsigned walls_count;
}; };
static const cyaml_schema_value_t float_entry = { static const cyaml_schema_value_t int_entry = {
CYAML_VALUE_FLOAT(CYAML_FLAG_DEFAULT, float), CYAML_VALUE_INT(CYAML_FLAG_DEFAULT, int),
}; };
static const cyaml_schema_value_t vertex_entry = { 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[] = { static const cyaml_schema_field_t wall_fields[] = {
@ -62,6 +65,6 @@ static const cyaml_schema_value_t top_schema = {
scene_fields), scene_fields),
}; };
void kubo_file_parse(char *file_name); struct kubo_context kubo_file_parse(char *file_name);
#endif #endif

View file

@ -18,17 +18,13 @@
#include "kubo_wall.h" #include "kubo_wall.h"
struct kubo_wall *kubo_wall_init() { void kubo_wall_init(struct kubo_wall *wall) {
struct kubo_wall *wall = malloc(sizeof(struct kubo_wall)); if (!wall) {
return;
if (!wall) }
return NULL;
wall->state = KUBO_WALL_INIT; wall->state = KUBO_WALL_INIT;
kubo_vector2_arr_init(&wall->vertices); kubo_vector2_arr_init(&wall->vertices);
return wall;
} }
void kubo_wall_cleanup(struct kubo_wall *wall) { void kubo_wall_cleanup(struct kubo_wall *wall) {

View file

@ -40,7 +40,7 @@ struct kubo_wall {
enum kubo_wall_state state; 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); void kubo_wall_cleanup(struct kubo_wall *wall);
bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec); bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec);

8
main.c
View file

@ -8,13 +8,13 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 2) {
kubo_file_parse(argv[1]);
}
struct kubo_context context; struct kubo_context context;
kubo_context_init(&context); kubo_context_init(&context);
if (argc == 2) {
context = kubo_file_parse(argv[1]);
}
kubo_window_init(); kubo_window_init();
while (!kubo_window_should_close(&context)) { while (!kubo_window_should_close(&context)) {

View file

@ -1,10 +1,11 @@
walls: walls:
- vertices: - vertices:
- [0.0, 1.0] - [100, 100]
- [1.0, 2.0] - [100, 200]
- vertices: - vertices:
- [1.0, 3.0] - [100, 300]
- [2.0, 1.0] - [100, 500]
- [200, 500]
- vertices: - vertices:
- [3.0, 5.0] - [300, 300]
- [2.0, 1.0] - [500, 500]