cleanup kubo_file, file_write

This commit is contained in:
Luka Jankovic 2025-07-25 16:11:48 +02:00
parent e6b0928ac5
commit 90db87bec9
3 changed files with 48 additions and 20 deletions

View file

@ -44,6 +44,8 @@ struct kubo_context_state_data {
};
struct kubo_context {
char *file_name;
bool exit_pending;
struct kubo_wall_arr walls;
struct kubo_context_state_data state;

View file

@ -1,44 +1,69 @@
#include "kubo_file.h"
struct kubo_context kubo_file_parse(char *file_name) {
static const cyaml_config_t config = {
.log_fn = cyaml_log,
.mem_fn = cyaml_mem,
.log_level = CYAML_LOG_WARNING,
};
static const cyaml_config_t config = {
.log_fn = cyaml_log,
.mem_fn = cyaml_mem,
.log_level = CYAML_LOG_WARNING,
};
struct kubo_file_scene *n;
struct kubo_context kubo_file_parse(char *file_name) {
struct kubo_file_scene *scene;
cyaml_err_t err = cyaml_load_file(file_name, &config, &top_schema,
(cyaml_data_t **)&n, NULL);
(cyaml_data_t **)&scene, NULL);
if (err != CYAML_OK) {
printf("cyaml err\n");
printf("Error loading yaml file %s\n", file_name);
}
struct kubo_context context;
kubo_context_init(&context);
context.file_name = file_name;
for (size_t i = 0; i < n->walls_count; i++) {
printf("wall:\n");
for (size_t i = 0; i < scene->walls_count; i++) {
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]
};
for (size_t j = 0; j < scene->walls[i].vertices_count; j++) {
Vector2 vertex = {.x = scene->walls[i].vertices[j][0],
.y = scene->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, scene, 0);
if (err != CYAML_OK) {
printf("cyaml free err\n");
printf("Error freeing yaml file %s\n", file_name);
}
return context;
}
void kubo_file_write(struct kubo_context *context) {
if (!context) {
return;
}
struct kubo_file_scene *scene = malloc(sizeof(struct kubo_file_scene));
scene->walls_count = context->walls.count;
scene->walls = malloc(sizeof(struct kubo_file_wall) * scene->walls_count);
for (size_t i = 0; i < scene->walls_count; i++) {
struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i);
scene->walls[i].vertices =
malloc(sizeof(kubo_file_vertex) * wall->vertices.count);
for (size_t j = 0; j < wall->vertices.count; j++) {
Vector2 vertex = kubo_vector2_arr_get(&wall->vertices, j);
scene->walls[i].vertices[j][0] = vertex.x;
scene->walls[i].vertices[j][1] = vertex.y;
}
}
cyaml_err_t err =
cyaml_save_file(context->file_name, &config, &top_schema, scene, 0);
if (err != CYAML_OK) {
printf("Error writing yaml file %s\n", context->file_name);
}
}

View file

@ -66,5 +66,6 @@ static const cyaml_schema_value_t top_schema = {
};
struct kubo_context kubo_file_parse(char *file_name);
void kubo_file_write(struct kubo_context *context);
#endif