Adding initial map editor support.

This commit is contained in:
sergiotarxz 2021-12-18 10:49:45 +01:00
parent c5b7a042a7
commit 984c4c5efc
3 changed files with 201 additions and 2 deletions

View File

@ -0,0 +1,5 @@
void
map_editor_init ();
void
map_editor_loop ();

View File

@ -4,13 +4,14 @@ inc = include_directories('include')
l3tdedeps = [
dependency('ncurses'),
dependency('form'),
dependency('json-glib-1.0')
]
sources = [
'src/main.c',
'src/overworld.c',
'src/game.c',
'src/game/map_editor.c',
'src/map.c',
'src/map/warp.c',
'src/map/node.c',
@ -25,8 +26,17 @@ link_arguments = [
prefix = get_option('prefix')
datadir = get_option('datadir')
executable('l3tde_map_editor',
'src/main_map_editor.c', sources,
dependencies : l3tdedeps,
include_directories : inc,
install : true,
link_args : link_arguments,
c_args: '-DRESOURCES_PATH="'+prefix+'/share/l3tde"'
)
executable('l3tde',
sources,
'src/main.c', sources,
dependencies : l3tdedeps,
include_directories : inc,
install : true,

184
src/game/map_editor.c Normal file
View File

@ -0,0 +1,184 @@
#include <stdbool.h>
#include <stdlib.h>
#include <ncurses.h>
#include <form.h>
#include <l3tde/game/map_editor.h>
#define BACKSPACE 127
typedef enum {
SELECT_MAP,
MAP_CREATION_FORM,
MAP_SELECTION_FORM,
MAP_EDITOR,
N_FORMS
} MapEditorForms;
typedef enum {
CREATE_NEW_MAP,
OPEN_MAP,
N_OPTIONS
} MapEditorInitialFormOptions;
typedef struct {
MapEditorInitialFormOptions selected_option;
MapEditorForms current_form;
bool exit;
bool first_render_map_creation_form;
FIELD **fields;
FORM *form_creation;
} MapEditorStatus;
const char *OPTIONS_STR[N_OPTIONS] = {
"Create a new map.",
"Open an existing map."
};
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
map_editor_init () {
initscr ();
start_color ();
curs_set (0);
raw ();
keypad (stdscr, TRUE);
noecho ();
}
void
map_editor_loop () {
MapEditorStatus *status = malloc (sizeof *status);
status->selected_option = 0;
status->exit = false;
status->first_render_map_creation_form = true;
status->current_form = SELECT_MAP;
status->form_creation = NULL;
status->fields = NULL;
while (1) {
map_editor_render (status);
map_editor_handle_input (status);
if (status->exit) break;
}
endwin ();
}
static void
map_editor_handle_input_select_map (MapEditorStatus *status) {
int input = getch ();
switch (input) {
case '\n':
if (status->selected_option == CREATE_NEW_MAP) {
status->first_render_map_creation_form = true;
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':
status->exit = true;
break;
}
}
static void
map_editor_handle_input_map_editor (MapEditorStatus *status) {
int input = getch ();
switch (input) {
case 'q':
status->exit = true;
break;
}
}
static void
map_editor_handle_input (MapEditorStatus *status) {
if (status->current_form == SELECT_MAP) {
map_editor_handle_input_select_map (status);
return;
}
if (status->current_form == MAP_EDITOR) {
map_editor_handle_input_map_editor (status);
return;
}
}
static void
map_editor_render_map_creation_form (MapEditorStatus *status) {
if (status->first_render_map_creation_form) {
status->fields = malloc (sizeof *(status->fields) * 3);
FORM *form;
int ch;
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;
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));
form = new_form(status->fields);
post_form(form);
mvprintw (0, 0, "Set the x max value.");
mvprintw (2, 0, "Set the y max value.");
refresh ();
while((ch = getch()))
{ switch(ch)
{
case BACKSPACE:
form_driver(form, REQ_DEL_PREV);
break;
case KEY_LEFT:
form_driver(form, REQ_PREV_CHAR);
break;
case KEY_RIGHT:
form_driver(form, REQ_NEXT_CHAR);
break;
default:
form_driver(form, ch);
break;
}
}
status->first_render_map_creation_form = false;
}
}
static void
map_editor_render (MapEditorStatus *status) {
erase ();
if (status->current_form == SELECT_MAP) {
map_editor_print_main_menu (status);
}
if (status->current_form == MAP_CREATION_FORM) {
map_editor_render_map_creation_form (status);
}
}
static void
map_editor_print_main_menu (MapEditorStatus *status) {
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);
}
}