diff --git a/kubo_char_arr.h b/kubo_char_arr.h new file mode 100644 index 0000000..d590a79 --- /dev/null +++ b/kubo_char_arr.h @@ -0,0 +1,37 @@ +/* + * Copyright Luka Jankovic 2025 + * + * This file is part of Kubo. + * + * Kubo is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * Kubo is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * Kubo. If not, see . + */ + +#ifndef KUBO_CHAR_ARR_H +#define KUBO_CHAR_ARR_H + +#include + +#include "kubo_dynarray.h" + +KUBO_DYNARRAY_REGISTER(kubo_char_arr, char); + +static inline char *kubo_char_arr_build_str(struct kubo_char_arr *arr) { + char *res = malloc(arr->count + 1); + for (int i = 0; i < arr->count; i++) { + res[i] = kubo_char_arr_get(arr, i); + } + res[arr->count] = '\0'; + return res; +} + +#endif diff --git a/kubo_command.c b/kubo_command.c index 385cb6a..6478942 100644 --- a/kubo_command.c +++ b/kubo_command.c @@ -18,15 +18,6 @@ #include "kubo_command.h" -static char *kubo_char_arr_build_str(struct kubo_char_arr *arr) { - char *res = malloc(arr->count + 1); - for (int i = 0; i < arr->count; i++) { - res[i] = kubo_char_arr_get(arr, i); - } - res[arr->count] = '\0'; - return res; -} - void kubo_command_render(struct kubo_context *context, int x, int y, int font_size) { DrawText(kubo_char_arr_build_str(&context->command), x, y, font_size, WHITE); } diff --git a/kubo_context.c b/kubo_context.c index 536c98a..8a43b69 100644 --- a/kubo_context.c +++ b/kubo_context.c @@ -38,6 +38,7 @@ void kubo_context_cleanup(struct kubo_context *context) { void kubo_context_set_state(struct kubo_context *context, enum kubo_context_state state) { + kubo_char_arr_clear(&context->command); context->state = state; switch (context->state) { @@ -65,6 +66,11 @@ Color kubo_context_get_color(enum kubo_context_state state) { return kubo_context_colors[state]; } +void kubo_context_accept_cmd(struct kubo_context *context) { + printf("accepting cmd %s\n", kubo_char_arr_build_str(&context->command)); + kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); +} + struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) { if (context->state != KUBO_CONTEXT_WALL_NEW) { diff --git a/kubo_context.h b/kubo_context.h index 1214eda..3db109a 100644 --- a/kubo_context.h +++ b/kubo_context.h @@ -23,11 +23,11 @@ #include #include +#include "kubo_char_arr.h" #include "kubo_dynarray.h" #include "kubo_wall.h" KUBO_DYNARRAY_REGISTER(kubo_wall_arr, struct kubo_wall *); -KUBO_DYNARRAY_REGISTER(kubo_char_arr, char); enum kubo_context_state { KUBO_CONTEXT_NORMAL, @@ -36,7 +36,8 @@ enum kubo_context_state { KUBO_CONTEXT_WALL_SELECT, }; -static const char *kubo_context_labels[] = {"Normal", "Command", "Wall New", "Wall Select"}; +static const char *kubo_context_labels[] = {"Normal", "Command", "Wall New", + "Wall Select"}; static const Color kubo_context_colors[] = {BLACK, RED, GREEN, BLUE}; @@ -60,6 +61,8 @@ void kubo_context_set_state(struct kubo_context *context, const char *kubo_context_get_label(enum kubo_context_state state); Color kubo_context_get_color(enum kubo_context_state state); +void kubo_context_accept_cmd(struct kubo_context *context); + struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context); void kubo_context_select_next_wall(struct kubo_context *context); diff --git a/kubo_dynarray.h b/kubo_dynarray.h index e51648d..55e9c8c 100644 --- a/kubo_dynarray.h +++ b/kubo_dynarray.h @@ -69,6 +69,11 @@ size_t index) { \ assert(index < arr->count); \ arr->data[index] = new_val; \ + } \ + \ + static inline void name##_clear(struct name *arr) { \ + free(arr->data); \ + name##_init(arr); \ } #endif diff --git a/kubo_window.c b/kubo_window.c index 0a889e2..52033ef 100644 --- a/kubo_window.c +++ b/kubo_window.c @@ -87,11 +87,21 @@ static void window_key_input(struct kubo_context *context) { int key_code = GetKeyPressed(); - if (key_code == KEY_ESCAPE) { - kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); + if (context->state == KUBO_CONTEXT_COMMAND) { + switch (key_code) { + case KEY_ESCAPE: + kubo_context_set_state(context, KUBO_CONTEXT_NORMAL); + break; + case KEY_ENTER: + kubo_context_accept_cmd(context); + default: + break; + } + + return; } - if (!key_code || context->state == KUBO_CONTEXT_COMMAND) { + if (!key_code) { return; }