diff --git a/kubo_command.c b/kubo_command.c index 51be490..c37f0c6 100644 --- a/kubo_command.c +++ b/kubo_command.c @@ -19,22 +19,19 @@ #include "kubo_command.h" static inline void kubo_command_exit(struct kubo_context *context); +static inline void kubo_command_write(struct kubo_context *context); static const struct kubo_command_data kubo_commands[] = { - {":q", kubo_command_exit}}; + {":q", kubo_command_exit}, {":w", kubo_command_write}}; static const size_t kubo_commands_size = sizeof(kubo_commands) / sizeof(kubo_commands[0]); static struct kubo_char_arr command; -void kubo_command_init() { - kubo_char_arr_init(&command); -} +void kubo_command_init() { kubo_char_arr_init(&command); } -void kubo_command_cleanup() { - kubo_char_arr_free(&command); -} +void kubo_command_cleanup() { kubo_char_arr_free(&command); } void kubo_command_append_char(int c) { kubo_char_arr_add(&command, c); } @@ -61,3 +58,7 @@ void kubo_command_clear() { kubo_char_arr_clear(&command); } static inline void kubo_command_exit(struct kubo_context *context) { context->exit_pending = true; } + +static inline void kubo_command_write(struct kubo_context *context) { + kubo_file_write(context); +} diff --git a/kubo_command.h b/kubo_command.h index 3e8d896..635756c 100644 --- a/kubo_command.h +++ b/kubo_command.h @@ -24,6 +24,7 @@ #include "kubo_char_arr.h" #include "kubo_context.h" +#include "kubo_file.h" typedef void (*kubo_command_function)(struct kubo_context *context); diff --git a/kubo_file.c b/kubo_file.c index b947619..959b29f 100644 --- a/kubo_file.c +++ b/kubo_file.c @@ -47,10 +47,11 @@ void kubo_file_write(struct kubo_context *context) { 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); + 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++) { @@ -66,4 +67,10 @@ void kubo_file_write(struct kubo_context *context) { 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); }