diff --git a/kubo_context.h b/kubo_context.h index eaafbdf..56f42a0 100644 --- a/kubo_context.h +++ b/kubo_context.h @@ -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; diff --git a/kubo_file.c b/kubo_file.c index 979738b..b947619 100644 --- a/kubo_file.c +++ b/kubo_file.c @@ -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); + } +} diff --git a/kubo_file.h b/kubo_file.h index edc1bf1..329760c 100644 --- a/kubo_file.h +++ b/kubo_file.h @@ -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