#include "kubo_file.h" static const cyaml_config_t config = { .log_fn = cyaml_log, .mem_fn = cyaml_mem, .log_level = CYAML_LOG_WARNING, }; 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 **)&scene, NULL); if (err != CYAML_OK) { 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 < scene->walls_count; i++) { struct kubo_wall *wall = malloc(sizeof(struct kubo_wall)); kubo_wall_init(wall); 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); } wall->state = KUBO_WALL_DONE; kubo_wall_arr_add(&context.walls, wall); } err = cyaml_free(&config, &top_schema, scene, 0); if (err != CYAML_OK) { 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) * context->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_count = wall->vertices.count; 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); } for (size_t i = 0; i < scene->walls_count; i++) { free(scene->walls[i].vertices); } free(scene); }