Newer
Older
#include <ncurses.h>
#include <stdint.h>
#include <limits.h>
#include "dungeon.h"
#include "draw_dungeon.h"
char monster_chars[] = "0123456789abcdef";
void init_screen() {
// Initialize ncurses screen
initscr();
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Unbuffered input
cbreak();
// Don't echo back characters that are typed
noecho();
// Hide the cursor
curs_set(0);
// Make it so keypad characters do what we expect
keypad(stdscr, TRUE);
// Clear the screen
clear();
}
void destroy_screen() {
endwin();
}
void display_message(char *message) {
move(0, 0);
clrtoeol();
mvaddstr(0, 0, message);
}
char get_map_char(dungeon_t *d, int y, int x) {
int i;
// Check each position from top down, i.e. check the layers which would
// render over other layers first.
if (y == 0 || y == (MAP_HEIGHT-1) || x == 0 || x == (MAP_WIDTH-1)) {
if (y == 0 || y == (MAP_HEIGHT - 1)) return '-';
else return '|';
}
if (d->characters[y][x]) {
return d->characters[y][x]->symbol;
}
for (i = 0; i < d->upstair_count; i++) {
if (x == d->upstair_list[i].x && y == d->upstair_list[i].y) {
return '<';
}
}
for (i = 0; i < d->downstair_count; i++) {
if (x == d->downstair_list[i].x && y == d->downstair_list[i].y) {
return '>';
}
}
for (i = 0; i < d->room_count; i++) {
room_t *r = d->room_list + i;
if (x >= r->x && x < r->x + r->w && y >= r->y && y < r->y + r->h) {
return '.';
}
}
if (d->hardness[y][x] == 0) {
return '#';
}
return ' ';
}
void draw_dungeon(dungeon_t *d) {
int i, j;
move(1, 0);
for (i = 0; i < MAP_HEIGHT; i++) {
for (j = 0; j < MAP_WIDTH; j++) {
addch(get_map_char(d, i, j));
}
}
}
void refresh_screen() {
refresh();
}
void draw_position(dungeon_t *d, int x, int y) {
mvaddch(y + 1, x, get_map_char(d, y, x));
}