-
Notifications
You must be signed in to change notification settings - Fork 1
/
ncRender.c
111 lines (96 loc) · 1.99 KB
/
ncRender.c
1
2
3
4
5
6
7
8
9
10
11
12
13
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
101
102
103
104
105
106
107
108
109
110
111
#include "puzzleData.h"
#include <curses.h>
#include <stdio.h>
#include <string.h>
char charForLoc(Runtime *rt, int loc) {
int maxHeight = -1;
int id = -1;
int currentHeight;
for (int i = 0; i < rt->objectCount; i++) {
currentHeight = objectLayer(rt->objects[i].objId);
// TODO: this is silly
int objectLoc = (rt->objects[i].y * rt->width) + rt->objects[i].x;
if (rt->objects[i].deleted == 0 && objectLoc == loc &&
currentHeight > maxHeight) {
maxHeight = currentHeight;
id = rt->objects[i].objId;
}
}
if (id == -1) {
return objectGlyph(rt->backgroundId);
}
return objectGlyph(id);
}
void renderLevel(Runtime *rt) {
clear();
int row = 0;
int col = 0;
int count = levelTileCount(rt->levelIndex);
char map[count];
for (int i = 0; i < count; i++) {
map[i] = charForLoc(rt, i);
}
// draw
for (int i = 0; i < count; i++) {
mvaddch(row, col, map[i]);
col++;
if ((i + 1) % (rt->width) == 0) {
col = 0;
row++;
}
}
refresh();
}
void renderMessage(Runtime *rt) {
clear();
char *message = levelMessage(rt->levelIndex);
int messageLength = strlen(message);
for (int i = 0; i < messageLength + 4; i++) {
mvaddch(0, i, '*');
}
mvaddch(1, 0, '*');
mvaddch(1, messageLength + 1, '*');
mvaddstr(1, 2, message);
for (int i = 0; i < messageLength + 4; i++) {
mvaddch(2, i, '*');
}
}
void render(Runtime *rt) {
switch (rt->levelType) {
case SQUARES:
renderLevel(rt);
break;
case MESSAGE_TEXT:
renderMessage(rt);
break;
}
}
void debugRender(Runtime *rt) { render(rt); }
void initRenderer() {
initscr();
cbreak();
noecho();
clear();
}
void closeRenderer() { endwin(); }
char input() {
switch (getch()) {
case 'q':
return QUIT;
case 'r':
return RESTART;
case 'w':
return UP;
case 'a':
return LEFT;
case 's':
return DOWN;
case 'd':
return RIGHT;
case 'z':
return UNDO;
case 'x':
return USE;
}
return UNSPECIFIED;
}