Adding first map print routine.
This commit is contained in:
parent
9dde2827ed
commit
98fe29c9aa
18
include/l3tde/game.h
Normal file
18
include/l3tde/game.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <l3tde/map.h>
|
||||
|
||||
typedef struct {
|
||||
L3TDEMapPtr current_map;
|
||||
size_t x_pos, y_pos;
|
||||
} L3TDEGame;
|
||||
typedef L3TDEGame *L3TDEGamePtr;
|
||||
|
||||
void l3tde_game_init (void);
|
||||
|
||||
L3TDEGamePtr
|
||||
l3tde_game_create (L3TDEMapPtr current_map,
|
||||
size_t x_pos, size_t y_pos);
|
||||
|
||||
void
|
||||
l3tde_game_destroy (L3TDEGamePtr game);
|
@ -8,6 +8,7 @@ l3tdedeps = [
|
||||
|
||||
sources = [
|
||||
'src/main.c',
|
||||
'src/game.c',
|
||||
'src/map.c',
|
||||
'src/map/warp.c',
|
||||
'src/map/node.c',
|
||||
|
188
src/game.c
Normal file
188
src/game.c
Normal file
@ -0,0 +1,188 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdlib.h>
|
||||
#include <ncurses.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <l3tde/game.h>
|
||||
#include <l3tde/map.h>
|
||||
|
||||
static L3TDEMapPtr
|
||||
l3tde_game_create_default_map (void);
|
||||
static L3TDEGamePtr
|
||||
l3tde_game_create_default_game (void);
|
||||
void
|
||||
l3tde_game_render_game (L3TDEGamePtr game);
|
||||
static size_t
|
||||
l3tde_game_render_game_get_start_node (size_t current_pos,
|
||||
size_t nodes_len, size_t max);
|
||||
static void
|
||||
l3tde_game_render_game_get_start_nodes (L3TDEGamePtr game,
|
||||
int max_x, int max_y, size_t *x, size_t *y);
|
||||
|
||||
void
|
||||
l3tde_game_init (void) {
|
||||
L3TDEGamePtr game = l3tde_game_create_default_game ();
|
||||
|
||||
initscr ();
|
||||
raw ();;
|
||||
keypad (stdscr, TRUE);
|
||||
noecho ();
|
||||
l3tde_game_render_game (game);;
|
||||
getch ();
|
||||
|
||||
refresh ();
|
||||
endwin ();
|
||||
}
|
||||
|
||||
void
|
||||
l3tde_game_render_game (L3TDEGamePtr game) {
|
||||
size_t start_node_x, start_node_y;
|
||||
size_t max_node_x, max_node_y;
|
||||
size_t nodes_x_len, nodes_y_len;
|
||||
int max_x, max_y;
|
||||
|
||||
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,
|
||||
max_x, max_y, &start_node_x, &start_node_y);
|
||||
|
||||
max_node_x = start_node_x + max_x;
|
||||
max_node_y = start_node_y + max_y;
|
||||
|
||||
if (max_node_x > nodes_x_len) {
|
||||
max_node_x = nodes_x_len;
|
||||
}
|
||||
|
||||
if (max_node_y > nodes_y_len) {
|
||||
max_node_y = nodes_y_len;
|
||||
}
|
||||
int printing_x = 0;
|
||||
for (size_t i = start_node_x; i<max_node_x; i++, printing_x++) {
|
||||
int printing_y = 0;
|
||||
for (size_t j = start_node_y; j<max_node_y; j++, printing_y++) {
|
||||
L3TDEMapNodePtr node = current_map->nodes[i][j];
|
||||
L3TDEMapTerrainPtr terrain = node->terrain;
|
||||
char symbol = terrain->symbol;
|
||||
mvprintw (printing_y, printing_x, "%c", symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
l3tde_game_render_game_get_start_nodes (L3TDEGamePtr game,
|
||||
int max_x, int max_y, size_t *x, size_t *y) {
|
||||
size_t nodes_x_len, nodes_y_len;
|
||||
size_t current_pos_x, current_pos_y;
|
||||
|
||||
L3TDEMapPtr current_map = game->current_map;
|
||||
|
||||
|
||||
current_pos_x = game->x_pos;
|
||||
current_pos_y = game->y_pos;
|
||||
|
||||
nodes_x_len = current_map->nodes_x_len;
|
||||
nodes_y_len = current_map->nodes_y_len;
|
||||
|
||||
*x = l3tde_game_render_game_get_start_node (current_pos_x,
|
||||
nodes_x_len, max_x);
|
||||
*y = l3tde_game_render_game_get_start_node (current_pos_y,
|
||||
nodes_y_len, max_y);
|
||||
}
|
||||
|
||||
static size_t
|
||||
l3tde_game_render_game_get_start_node (size_t current_pos,
|
||||
size_t nodes_len, size_t max) {
|
||||
int center = max / 2;
|
||||
size_t draw_center;
|
||||
size_t start_pos;
|
||||
|
||||
if (current_pos < center) {
|
||||
draw_center = center;
|
||||
} else if (nodes_len - current_pos >= center ) {
|
||||
draw_center = nodes_len - center;
|
||||
} else {
|
||||
draw_center = current_pos;
|
||||
}
|
||||
|
||||
if (center > draw_center) {
|
||||
start_pos = 0;
|
||||
} else {
|
||||
start_pos = draw_center - center;
|
||||
}
|
||||
return start_pos;
|
||||
}
|
||||
|
||||
static L3TDEGamePtr
|
||||
l3tde_game_create_default_game (void) {
|
||||
L3TDEMapPtr map = l3tde_game_create_default_map ();
|
||||
L3TDEGamePtr game = l3tde_game_create (map, 0, 0);
|
||||
return game;
|
||||
}
|
||||
|
||||
static L3TDEMapPtr
|
||||
l3tde_game_create_default_map (void) {
|
||||
L3TDEMapNodePtr **nodes;
|
||||
L3TDEMapHeaderPtr header;
|
||||
L3TDEMapTerrainPtr terrain;
|
||||
L3TDEMapPtr map;
|
||||
|
||||
const char *terrain_name = "grass";
|
||||
const char *header_name = "all_green";
|
||||
const char *header_description = "This is dummy map where all is green.";
|
||||
|
||||
size_t nodes_len = 80;
|
||||
size_t terrain_name_len = strlen (terrain_name);
|
||||
size_t header_name_len = strlen (header_name);
|
||||
size_t header_descrition_len = strlen (header_description);
|
||||
|
||||
nodes = malloc (sizeof *nodes * nodes_len);
|
||||
|
||||
header = l3tde_map_header_create (
|
||||
header_name, header_name_len,
|
||||
header_description, header_descrition_len);
|
||||
|
||||
terrain = l3tde_map_terrain_create (
|
||||
terrain_name, terrain_name_len, '.', 0, 0);
|
||||
|
||||
for (int i = 0; i < nodes_len; i++) {
|
||||
nodes[i] = malloc (sizeof **nodes * nodes_len);
|
||||
L3TDEMapNodePtr *y_nodes = nodes[i];
|
||||
for (int j = 0; j < nodes_len; j++) {
|
||||
y_nodes[j] = l3tde_map_node_create (terrain,
|
||||
L3TDE_MAP_NODE_NAVIGATION_WALKABLE);
|
||||
}
|
||||
}
|
||||
|
||||
map = l3tde_map_create (header, nodes, nodes_len, nodes_len, NULL, 0);
|
||||
return map;
|
||||
}
|
||||
|
||||
L3TDEGamePtr
|
||||
l3tde_game_create (L3TDEMapPtr map,
|
||||
size_t x, size_t y) {
|
||||
L3TDEGamePtr self = malloc (sizeof *self);
|
||||
|
||||
self->current_map = map;
|
||||
self->x_pos = x;
|
||||
self->y_pos = y;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
void
|
||||
l3tde_game_destroy (L3TDEGamePtr self) {
|
||||
if (self->current_map) {
|
||||
l3tde_map_destroy (self->current_map);
|
||||
self->current_map = NULL;
|
||||
}
|
||||
free (self);
|
||||
}
|
||||
|
||||
void l3tde_game_render () {
|
||||
|
||||
}
|
58
src/main.c
58
src/main.c
@ -1,60 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <ncurses.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <l3tde/map.h>
|
||||
#include <l3tde/map/terrain.h>
|
||||
#include <l3tde/map/node.h>
|
||||
#include <l3tde/map/header.h>
|
||||
#include <l3tde/game.h>
|
||||
|
||||
int
|
||||
main () {
|
||||
L3TDEMapNodePtr **nodes;
|
||||
L3TDEMapHeaderPtr header;
|
||||
L3TDEMapTerrainPtr terrain;
|
||||
L3TDEMapPtr map;
|
||||
|
||||
const char *terrain_name = "grass";
|
||||
const char *header_name = "all_green";
|
||||
const char *header_description = "This is dummy map where all is green.";
|
||||
|
||||
size_t nodes_len = 80;
|
||||
size_t terrain_name_len = strlen (terrain_name);
|
||||
size_t header_name_len = strlen (header_name);
|
||||
size_t header_descrition_len = strlen (header_description);
|
||||
|
||||
nodes = malloc (sizeof *nodes * nodes_len);
|
||||
|
||||
header = l3tde_map_header_create (
|
||||
header_name, header_name_len,
|
||||
header_description, header_descrition_len);
|
||||
|
||||
terrain = l3tde_map_terrain_create (
|
||||
terrain_name, terrain_name_len, '.', 0, 0);
|
||||
|
||||
for (int i = 0; i < nodes_len; i++) {
|
||||
nodes[i] = malloc (sizeof **nodes * nodes_len);
|
||||
L3TDEMapNodePtr *y_nodes = nodes[i];
|
||||
for (int j = 0; j < nodes_len; j++) {
|
||||
y_nodes[j] = l3tde_map_node_create (terrain,
|
||||
L3TDE_MAP_NODE_NAVIGATION_WALKABLE);
|
||||
}
|
||||
}
|
||||
|
||||
map = l3tde_map_create (header, nodes, nodes_len, nodes_len, NULL, 0);
|
||||
|
||||
initscr ();
|
||||
raw ();;
|
||||
keypad (stdscr, TRUE);
|
||||
noecho ();
|
||||
|
||||
refresh ();
|
||||
endwin ();
|
||||
|
||||
l3tde_map_destroy (map);
|
||||
l3tde_map_terrain_destroy (terrain);
|
||||
|
||||
map = NULL;
|
||||
terrain = NULL;
|
||||
return 0;
|
||||
l3tde_game_init ();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user