From 4e3f8400c11a7dbb3b08f7d937deaf91e29c0586 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Thu, 23 Dec 2021 12:53:42 +0100 Subject: [PATCH] Adding initial editor and fields for X and Y. --- include/l3tde/game/map_editor.h | 5 ++ include/l3tde/game/map_editor/editor.h | 7 +++ include/l3tde/overworld.h | 2 +- meson.build | 2 + src/game/map_editor.c | 73 +++-------------------- src/game/map_editor/creation_form.c | 80 ++++++++++++++++++++------ src/game/map_editor/editor.c | 16 ++++++ src/overworld.c | 2 +- 8 files changed, 104 insertions(+), 83 deletions(-) create mode 100644 include/l3tde/game/map_editor/editor.h create mode 100644 src/game/map_editor/editor.c diff --git a/include/l3tde/game/map_editor.h b/include/l3tde/game/map_editor.h index 47d2a18..f531c33 100644 --- a/include/l3tde/game/map_editor.h +++ b/include/l3tde/game/map_editor.h @@ -3,7 +3,10 @@ #include #include +#include + #define ctrl(x) ((x) & 0x1f) +#define KEY_INTRO '\n' typedef enum { SELECT_MAP, @@ -27,6 +30,8 @@ typedef struct { bool is_button_accept_selected; FIELD **fields; FORM *form_creation; + L3TDEMapPtr map; + L3TDEOverworldPtr overworld; } MapEditorStatus; void diff --git a/include/l3tde/game/map_editor/editor.h b/include/l3tde/game/map_editor/editor.h new file mode 100644 index 0000000..7da6e9a --- /dev/null +++ b/include/l3tde/game/map_editor/editor.h @@ -0,0 +1,7 @@ +#pragma once + +#include +void +map_editor_editor_render (MapEditorStatus *status); +void +map_editor_editor_handle_input (MapEditorStatus *status); diff --git a/include/l3tde/overworld.h b/include/l3tde/overworld.h index 3885e35..bbed614 100644 --- a/include/l3tde/overworld.h +++ b/include/l3tde/overworld.h @@ -13,7 +13,7 @@ typedef struct { typedef L3TDEOverworld * L3TDEOverworldPtr; L3TDEOverworldPtr -l3tde_overworld_create (); +l3tde_overworld_create (void); L3TDEMapPtr l3tde_overworld_get_map (L3TDEOverworldPtr self, diff --git a/meson.build b/meson.build index 07b95f0..b36a935 100644 --- a/meson.build +++ b/meson.build @@ -12,7 +12,9 @@ sources = [ 'src/overworld.c', 'src/game.c', 'src/game/map_editor.c', + 'src/game/map_editor/main_menu.c', 'src/game/map_editor/creation_form.c', + 'src/game/map_editor/editor.c', 'src/map.c', 'src/map/warp.c', 'src/map/node.c', diff --git a/src/game/map_editor.c b/src/game/map_editor.c index 763b8d7..9a6246f 100644 --- a/src/game/map_editor.c +++ b/src/game/map_editor.c @@ -5,19 +5,13 @@ #include #include +#include #include - -#define KEY_INTRO '\n' -const char *OPTIONS_STR[N_OPTIONS] = { - "Create a new map.", - "Open an existing map." -}; +#include static void map_editor_render (MapEditorStatus *status); static void -map_editor_print_main_menu (MapEditorStatus *status); -static void map_editor_handle_input (MapEditorStatus *status); void @@ -38,6 +32,7 @@ map_editor_loop () { status->current_form = SELECT_MAP; status->form_creation = NULL; status->fields = NULL; + status->map = NULL; while (1) { map_editor_render (status); map_editor_handle_input (status); @@ -46,57 +41,14 @@ map_editor_loop () { endwin (); } -static void -map_editor_handle_input_select_map (MapEditorStatus *status) { - int input = getch (); - switch (input) { - case KEY_INTRO: - if (status->selected_option == CREATE_NEW_MAP) { - status->first_render_map_creation_form = true; - status->is_button_accept_selected = false; - status->current_form = MAP_CREATION_FORM; - } - break; - case KEY_DOWN: - if (status->selected_option < N_OPTIONS - 1) { - status->selected_option++; - } else { - status->selected_option = 0; - } - break; - case KEY_UP: - if (status->selected_option > 0) { - status->selected_option--; - } else { - status->selected_option = N_OPTIONS - 1; - } - break; - case 'q': - case ctrl ('c'): - status->exit = true; - break; - } -} - -static void -map_editor_handle_input_map_editor (MapEditorStatus *status) { - int input = getch (); - switch (input) { - case ctrl ('c'): - case 'q': - status->exit = true; - break; - } -} - static void map_editor_handle_input (MapEditorStatus *status) { switch (status->current_form) { case SELECT_MAP: - map_editor_handle_input_select_map (status); + map_editor_main_menu_handle_input (status); break; case MAP_EDITOR: - map_editor_handle_input_map_editor (status); + map_editor_editor_handle_input (status); break; case MAP_CREATION_FORM: map_editor_creation_form_handle_input (status); @@ -109,24 +61,15 @@ map_editor_handle_input (MapEditorStatus *status) { - static void map_editor_render (MapEditorStatus *status) { if (status->current_form == SELECT_MAP) { - map_editor_print_main_menu (status); + map_editor_main_menu_render (status); } if (status->current_form == MAP_CREATION_FORM) { map_editor_creation_form_render (status); } -} - -static void -map_editor_print_main_menu (MapEditorStatus *status) { - erase (); - for (int i = 0; i < N_OPTIONS; i++) { - if (status->selected_option == i) attron (A_REVERSE); - mvprintw (i, 0, "%s", OPTIONS_STR[i]); - if (status->selected_option == i) attroff (A_REVERSE); + if (status->current_form == MAP_EDITOR) { + map_editor_editor_render (status); } } - diff --git a/src/game/map_editor/creation_form.c b/src/game/map_editor/creation_form.c index 677f71b..2171e17 100644 --- a/src/game/map_editor/creation_form.c +++ b/src/game/map_editor/creation_form.c @@ -2,17 +2,27 @@ #include #include +#include +#include #define BACKSPACE 127 -#define KEY_TAB '\t' +#define KEY_TAB '\t' static void map_editor_creation_form_move_to_button_accept (MapEditorStatus *status); static void map_editor_creation_form_move_to_field (MapEditorStatus *status, int move_to); -static FIELD * +static FIELD * map_editor_get_last_field (FIELD **fields); +typedef enum { + MAP_NAME_FIELD, + MAP_DESCRIPTION_FIELD, + X_FIELD, + Y_FIELD, + N_FIELDS +} FieldsFormCreation; + void map_editor_creation_form_handle_input (MapEditorStatus *status) { FORM *form = status->form_creation; @@ -28,6 +38,39 @@ map_editor_creation_form_handle_input (MapEditorStatus *status) { case KEY_RIGHT: form_driver (form, REQ_NEXT_CHAR); break; + case KEY_INTRO: + if (status->is_button_accept_selected) { + L3TDEOverworldPtr overworld = l3tde_overworld_create (); + char *x_str = field_buffer (status->fields[X_FIELD], 0); + char *y_str = field_buffer (status->fields[Y_FIELD], 0); + if (!x_str || !y_str) { + goto cleanup_key_intro; + } + errno = 0; + size_t x = strtoll (x_str, NULL, 10); + if (errno == EINVAL || errno == ERANGE) { + mvprintw (11, 0, "X is not a number."); + goto cleanup_key_intro; + } + errno = 0; + size_t y = strtoll (y_str, NULL, 10); + if (errno == EINVAL || errno == ERANGE) { + mvprintw (11, 0, "Y is not a number."); + goto cleanup_key_intro; + } + status->current_form = MAP_EDITOR; + status->overworld = overworld; + L3TDEMapNodePtr **nodes = malloc (sizeof *nodes * x); + for (int i = 0; i < x; i++) { + nodes[i] = malloc (sizeof **nodes * y); + for (int j = 0; j < y; j++) { + nodes[i][j] = l3tde_map_node_create (overworld->terrains[0], 0); + } + } + +cleanup_key_intro: + break; + } case KEY_TAB: case KEY_DOWN: if (!status->is_button_accept_selected @@ -65,27 +108,32 @@ void map_editor_creation_form_render (MapEditorStatus *status) { if (status->first_render_map_creation_form) { erase (); - status->fields = malloc (sizeof *(status->fields) * 3); + status->fields = malloc (sizeof *(status->fields) * N_FIELDS + 1); curs_set (1); - status->fields[0] = new_field (1, 10, 1, 0, 50, 0); - status->fields[1] = new_field (1, 10, 3, 0, 50, 0); - status->fields[2] = NULL; + status->fields[MAP_NAME_FIELD] = new_field (1, 10, 1, 0, 50, 0); + status->fields[MAP_DESCRIPTION_FIELD] = new_field (1, 10, 3, 0, 50, 0); + status->fields[X_FIELD] = new_field (1, 10, 5, 0, 50, 0); + status->fields[Y_FIELD] = new_field (1, 10, 7, 0, 50, 0); + status->fields[N_FIELDS] = NULL; init_pair (1, COLOR_WHITE, COLOR_BLUE); - set_field_fore (status->fields[0], COLOR_PAIR (1)); - set_field_back (status->fields[0], COLOR_PAIR (1)); - set_field_fore (status->fields[1], COLOR_PAIR (1)); - set_field_back (status->fields[1], COLOR_PAIR (1)); + for (int i = 0; i < N_FIELDS; i++) { + set_field_fore (status->fields[i], COLOR_PAIR (1)); + set_field_back (status->fields[i], COLOR_PAIR (1)); + } status->form_creation = new_form (status->fields); post_form (status->form_creation); status->first_render_map_creation_form = false; - mvprintw (0, 0, "Set the x max value."); - mvprintw (2, 0, "Set the y max value."); + mvprintw (0, 0, "Map name."); + mvprintw (2, 0, "Map description."); + mvprintw (4, 0, "Set the x max value."); + mvprintw (6, 0, "Set the y max value."); attron (COLOR_PAIR (1)); - mvprintw (5, 0, "Accept"); + mvprintw (9, 0, "Accept"); attroff (COLOR_PAIR (1)); } } + static void map_editor_creation_form_move_to_field (MapEditorStatus *status, int move_to) { FORM *form = status->form_creation; @@ -94,14 +142,14 @@ map_editor_creation_form_move_to_field (MapEditorStatus *status, int move_to) { form_driver (form, move_to); } - + static void map_editor_creation_form_move_to_button_accept (MapEditorStatus *status) { status->is_button_accept_selected = true; - wmove (stdscr, 5, 0); + wmove (stdscr, 9, 0); } -static FIELD * +static FIELD * map_editor_get_last_field (FIELD **fields) { FIELD *last_field = NULL; size_t i = 0; diff --git a/src/game/map_editor/editor.c b/src/game/map_editor/editor.c new file mode 100644 index 0000000..ae100d0 --- /dev/null +++ b/src/game/map_editor/editor.c @@ -0,0 +1,16 @@ +#include +void +map_editor_editor_handle_input (MapEditorStatus *status) { + int input = getch (); + switch (input) { + case ctrl ('c'): + case 'q': + status->exit = true; + break; + } +} + +void +map_editor_editor_render (MapEditorStatus *status) { + erase (); +} diff --git a/src/overworld.c b/src/overworld.c index e96785f..94db02e 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -30,7 +30,7 @@ l3tde_overworld_iterate_nodes_x_axys ( L3TDEOverworldPtr self, JsonArray *nodes_x_axys); L3TDEOverworldPtr -l3tde_overworld_create () { +l3tde_overworld_create (void) { L3TDEOverworldPtr self = malloc (sizeof *self); char **map_files = NULL; size_t map_files_len = 0;