Refactoring the game render to allow map editor rendering.
This commit is contained in:
parent
4e3f8400c1
commit
c9e86648f5
@ -4,6 +4,7 @@
|
|||||||
#include <form.h>
|
#include <form.h>
|
||||||
|
|
||||||
#include <l3tde/overworld.h>
|
#include <l3tde/overworld.h>
|
||||||
|
#include <l3tde/game.h>
|
||||||
|
|
||||||
#define ctrl(x) ((x) & 0x1f)
|
#define ctrl(x) ((x) & 0x1f)
|
||||||
#define KEY_INTRO '\n'
|
#define KEY_INTRO '\n'
|
||||||
@ -30,7 +31,7 @@ typedef struct {
|
|||||||
bool is_button_accept_selected;
|
bool is_button_accept_selected;
|
||||||
FIELD **fields;
|
FIELD **fields;
|
||||||
FORM *form_creation;
|
FORM *form_creation;
|
||||||
L3TDEMapPtr map;
|
L3TDEGamePtr game;
|
||||||
L3TDEOverworldPtr overworld;
|
L3TDEOverworldPtr overworld;
|
||||||
} MapEditorStatus;
|
} MapEditorStatus;
|
||||||
|
|
||||||
|
33
src/game.c
33
src/game.c
@ -104,23 +104,22 @@ l3tde_game_move_to_pos_if_posible (L3TDEGamePtr game,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
l3tde_game_render_game (L3TDEGamePtr game) {
|
l3tde_game_render_game_map (L3TDEGamePtr game) {
|
||||||
|
L3TDEMapPtr current_map = game->current_map;
|
||||||
|
|
||||||
size_t start_node_x, start_node_y;
|
size_t start_node_x, start_node_y;
|
||||||
size_t max_node_x, max_node_y;
|
size_t max_node_x, max_node_y;
|
||||||
size_t nodes_x_len, nodes_y_len;
|
size_t nodes_x_len, nodes_y_len;
|
||||||
int max_x, max_y;
|
int max_x, max_y;
|
||||||
int current_pos_map_x, current_pos_map_y;
|
|
||||||
|
|
||||||
getmaxyx(stdscr, max_y, max_x);
|
getmaxyx (stdscr, max_y, max_x);
|
||||||
|
|
||||||
L3TDEMapPtr current_map = game->current_map;
|
|
||||||
|
|
||||||
nodes_x_len = current_map->nodes_x_len;
|
|
||||||
nodes_y_len = current_map->nodes_y_len;
|
|
||||||
|
|
||||||
l3tde_game_render_game_get_start_nodes (game,
|
l3tde_game_render_game_get_start_nodes (game,
|
||||||
max_x, max_y, &start_node_x, &start_node_y);
|
max_x, max_y, &start_node_x, &start_node_y);
|
||||||
|
|
||||||
|
nodes_x_len = current_map->nodes_x_len;
|
||||||
|
nodes_y_len = current_map->nodes_y_len;
|
||||||
|
|
||||||
max_node_x = start_node_x + max_x;
|
max_node_x = start_node_x + max_x;
|
||||||
max_node_y = start_node_y + max_y;
|
max_node_y = start_node_y + max_y;
|
||||||
|
|
||||||
@ -165,7 +164,23 @@ l3tde_game_render_game (L3TDEGamePtr game) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
free (color_pairs);
|
free (color_pairs);
|
||||||
color_pairs = NULL;
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
l3tde_game_render_game (L3TDEGamePtr game) {
|
||||||
|
size_t start_node_x, start_node_y;
|
||||||
|
int max_x, max_y;
|
||||||
|
int current_pos_map_x, current_pos_map_y;
|
||||||
|
|
||||||
|
erase ();
|
||||||
|
|
||||||
|
getmaxyx (stdscr, max_y, max_x);
|
||||||
|
|
||||||
|
l3tde_game_render_game_get_start_nodes (game,
|
||||||
|
max_x, max_y, &start_node_x, &start_node_y);
|
||||||
|
|
||||||
|
l3tde_game_render_game_map (game);
|
||||||
|
|
||||||
current_pos_map_x = game->x_pos - start_node_x;
|
current_pos_map_x = game->x_pos - start_node_x;
|
||||||
current_pos_map_y = game->y_pos - start_node_y;
|
current_pos_map_y = game->y_pos - start_node_y;
|
||||||
attron (A_BOLD);
|
attron (A_BOLD);
|
||||||
|
@ -32,7 +32,7 @@ map_editor_loop () {
|
|||||||
status->current_form = SELECT_MAP;
|
status->current_form = SELECT_MAP;
|
||||||
status->form_creation = NULL;
|
status->form_creation = NULL;
|
||||||
status->fields = NULL;
|
status->fields = NULL;
|
||||||
status->map = NULL;
|
status->game = NULL;
|
||||||
while (1) {
|
while (1) {
|
||||||
map_editor_render (status);
|
map_editor_render (status);
|
||||||
map_editor_handle_input (status);
|
map_editor_handle_input (status);
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <l3tde/game/map_editor.h>
|
#include <l3tde/game/map_editor.h>
|
||||||
#include <l3tde/game/map_editor/creation_form.h>
|
#include <l3tde/game/map_editor/creation_form.h>
|
||||||
|
|
||||||
#include <l3tde/overworld.h>
|
#include <l3tde/overworld.h>
|
||||||
|
|
||||||
|
#include <l3tde/util/string.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#define BACKSPACE 127
|
#define BACKSPACE 127
|
||||||
@ -41,25 +46,30 @@ map_editor_creation_form_handle_input (MapEditorStatus *status) {
|
|||||||
case KEY_INTRO:
|
case KEY_INTRO:
|
||||||
if (status->is_button_accept_selected) {
|
if (status->is_button_accept_selected) {
|
||||||
L3TDEOverworldPtr overworld = l3tde_overworld_create ();
|
L3TDEOverworldPtr overworld = l3tde_overworld_create ();
|
||||||
char *x_str = field_buffer (status->fields[X_FIELD], 0);
|
const char *title = field_buffer (status->fields[MAP_NAME_FIELD], 0);
|
||||||
char *y_str = field_buffer (status->fields[Y_FIELD], 0);
|
const char *description = field_buffer (status->fields[MAP_DESCRIPTION_FIELD], 0);
|
||||||
|
const char *x_str = field_buffer (status->fields[X_FIELD], 0);
|
||||||
|
const char *y_str = field_buffer (status->fields[Y_FIELD], 0);
|
||||||
if (!x_str || !y_str) {
|
if (!x_str || !y_str) {
|
||||||
goto cleanup_key_intro;
|
break;
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
size_t x = strtoll (x_str, NULL, 10);
|
size_t x = strtoll (x_str, NULL, 10);
|
||||||
if (errno == EINVAL || errno == ERANGE) {
|
if (errno == EINVAL || errno == ERANGE) {
|
||||||
mvprintw (11, 0, "X is not a number.");
|
mvprintw (11, 0, "X is not a number.");
|
||||||
goto cleanup_key_intro;
|
break;
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
size_t y = strtoll (y_str, NULL, 10);
|
size_t y = strtoll (y_str, NULL, 10);
|
||||||
if (errno == EINVAL || errno == ERANGE) {
|
if (errno == EINVAL || errno == ERANGE) {
|
||||||
mvprintw (11, 0, "Y is not a number.");
|
mvprintw (11, 0, "Y is not a number.");
|
||||||
goto cleanup_key_intro;
|
break;
|
||||||
}
|
}
|
||||||
status->current_form = MAP_EDITOR;
|
status->current_form = MAP_EDITOR;
|
||||||
status->overworld = overworld;
|
status->overworld = overworld;
|
||||||
|
L3TDEMapHeaderPtr header = l3tde_map_header_create (title,
|
||||||
|
strlen (title) + 1, description,
|
||||||
|
strlen (description) + 1);
|
||||||
L3TDEMapNodePtr **nodes = malloc (sizeof *nodes * x);
|
L3TDEMapNodePtr **nodes = malloc (sizeof *nodes * x);
|
||||||
for (int i = 0; i < x; i++) {
|
for (int i = 0; i < x; i++) {
|
||||||
nodes[i] = malloc (sizeof **nodes * y);
|
nodes[i] = malloc (sizeof **nodes * y);
|
||||||
@ -67,8 +77,10 @@ map_editor_creation_form_handle_input (MapEditorStatus *status) {
|
|||||||
nodes[i][j] = l3tde_map_node_create (overworld->terrains[0], 0);
|
nodes[i][j] = l3tde_map_node_create (overworld->terrains[0], 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
L3TDEMapPtr map = l3tde_map_create (header, nodes, x, y,
|
||||||
cleanup_key_intro:
|
NULL, 0);
|
||||||
|
L3TDEGamePtr game = l3tde_game_create (map, 0, 0);
|
||||||
|
status->game = game;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KEY_TAB:
|
case KEY_TAB:
|
||||||
|
Loading…
Reference in New Issue
Block a user