-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_display.c
390 lines (349 loc) · 9.69 KB
/
game_display.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "game_display.h"
#include "actions.h"
#include "player.h"
#include "options.h"
#include "screen.h"
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "debug.h"
static xy_t _ga_w = 0;
static xy_t _ga_h = 0;
void
game_win_init_views(void)
{
debug("initializing views...\n");
_ga_w = screen_data->garea_w;
_ga_h = screen_data->garea_h;
/* su_t scrt = (player.replay ? 1 : options->scroll_thresh);*/
su_t scrt = options->scroll_thresh;
for (su_t p = 0; p < 2; p++)
{
xy_t vx = map->view[p].x;
xy_t vy = map->view[p].y;
if (vx + _ga_w > MAP_W - 1)
{
vx = MAP_W - _ga_w;
}
else if (vx + _ga_w <= player.xmv[p].from_x + scrt)
{
vx = player.xmv[p].from_x - _ga_w + (scrt + 1);
if (vx + _ga_w > MAP_W)
vx = MAP_W - _ga_w;
}
if (vy + _ga_h > MAP_H)
{
vy = MAP_H - _ga_h;
}
else if (player.xmv[p].from_y + scrt >= vy + _ga_h)
{
vy = player.xmv[p].from_y - _ga_h + (scrt + 1);
if (vy + _ga_h > MAP_H)
vy = MAP_H - _ga_h;
}
map->view[p].x = vx;
map->view[p].y = vy;
}
}
void
game_win_display(void)
{
if (_ga_w != screen_data->garea_w || _ga_h != screen_data->garea_h)
game_win_init_views();
xy_t ox = map->view[player.player].x;
xy_t oy = map->view[player.player].y;
/* while(map->view[player.player].y + _ga_h > MAP_H)
map->view[player.player].y--;
while(map->view[player.player].x + _ga_w > MAP_W)
map->view[player.player].x--;
*/
for (xy_t y = 0; y < _ga_h; y++)
{
#if DEBUG
int err = 0;
#endif
for (xy_t x = 0; x < _ga_w; x++)
{
#if DEBUG
if (oy + y < MAP_H && ox + x < MAP_W)
#endif
game_win_icon_dump(x * ICON_W, y * ICON_H,
map->buf[oy + y][ox + x]);
#if DEBUG
else
err = 1;
#endif
}
#if DEBUG
if (err)
{
debug("game win out of bounds, y:%d\n", y);
}
#endif
}
wrefresh(game_win);
}
void
game_win_show(xy_t tlx, xy_t tly)
{
if (tlx + _ga_w >= MAP_W)
tlx -= (tlx + _ga_w) - MAP_W;
if (tly + _ga_h >= MAP_H)
tly -= (tly + _ga_h) - MAP_W;
for (xy_t y = 0; y < _ga_h; y++) {
for (xy_t x = 0; x < _ga_w; x++) {
game_win_icon_dump(x * ICON_W, y * ICON_H,
map->buf[tly + y][tlx + x]);
}
}
wrefresh(game_win);
}
void
game_win_swap_update(void)
{
su_t p = (player.player) ? 0 : 1;
if (player.xmv[player.player].from_x > map->view[p].x
&& player.xmv[player.player].from_x <
map->view[p].x + screen_data->garea_w - 1)
{
if (player.xmv[player.player].from_y > map->view[p].y
&& player.xmv[player.player].from_y <
map->view[p].y + screen_data->garea_h - 1)
{
map->view[player.player].x = map->view[p].x;
map->view[player.player].y = map->view[p].y;
}
}
}
void
game_win_move_player(struct xor_move *pmv)
{
xy_t vx = map->view[player.player].x;
xy_t vy = map->view[player.player].y;
xy_t ovx = vx;
xy_t ovy = vy;
su_t scrt = (player.replay ? 1 : options->scroll_thresh);
switch (pmv->dir)
{
case MV_LEFT:
if (pmv->to_x < vx + scrt && vx > 0)
vx--;
break;
case MV_RIGHT:
if (pmv->to_x > vx + _ga_w - 1 - scrt && vx + _ga_w < MAP_W)
vx++;
break;
case MV_UP:
if (pmv->to_y < vy + scrt && vy > 0)
vy--;
break;
case MV_DOWN:
if (pmv->to_y > vy + _ga_h - 1 - scrt && vy + _ga_h < MAP_H)
vy++;
break;
default:
break;
}
if (ovx != vx || ovy != vy)
{
map->view[player.player].x = vx;
map->view[player.player].y = vy;
game_win_display();
return;
}
game_win_icon_display(pmv->from_x, pmv->from_y, ICON_SPACE);
game_win_icon_display(pmv->to_x, pmv->to_y, pmv->from_obj);
wrefresh(game_win);
}
void
game_win_move_object(struct xor_move *omv)
{
game_win_icon_display(omv->from_x, omv->from_y, ICON_SPACE);
game_win_icon_display(omv->to_x, omv->to_y, omv->from_obj);
wrefresh(game_win);
}
struct xy *
game_win_map_coord(xy_t x, xy_t y)
{
struct xy *ret = malloc(sizeof(struct xy));
if (!ret)
return 0;
struct xy *pv = &map->view[player.player];
if (x >= pv->x && x < pv->x + _ga_w
&& y >= pv->y && y < pv->y + _ga_h)
{
ret->x = (x - pv->x) * ICON_W;
ret->y = (y - pv->y) * ICON_H;
return ret;
}
free(ret);
return 0;
}
void
game_win_icon_display(xy_t x, xy_t y, su_t icon)
{
xy_t vx = map->view[player.player].x;
xy_t vy = map->view[player.player].y;
if (x < vx || x >= vx + _ga_w
|| y < vy || y >= vy + _ga_h)
{
return;
}
xy_t wx = (x - vx) * ICON_W;
xy_t wy = (y - vy) * ICON_H;
wattrset(game_win, COLOR_PAIR(icon));
for (xy_t yy = 0; yy < ICON_H; yy++)
for (xy_t xx = 0; xx < ICON_W; xx++)
mvwaddch(game_win, wy + yy, wx + xx, icons[icon].chrs[yy][xx]);
}
void
game_win_icon_dump(xy_t x, xy_t y, su_t icon)
{
wattrset(game_win, COLOR_PAIR(icon));
for (xy_t yy = 0; yy < ICON_H; yy++)
for (xy_t xx = 0; xx < ICON_W; xx++)
mvwaddch(game_win, y + yy, x + xx, icons[icon].chrs[yy][xx]);
}
void
game_win_dump_map_sect(xy_t topy, su_t sect, bool show)
{
xy_t mapstx = 0;
xy_t mapsty = 0;
xy_t mapmaxx, mapmaxy;
xy_t sy = 0;
switch (sect) {
case 0:
mapstx = HMAP_W;
mapsty = topy;
mapmaxx = MAP_W;
mapmaxy = HMAP_H;
break;
case 1:
mapstx = HMAP_W;
mapsty = HMAP_H;
mapmaxx = MAP_W;
mapmaxy = MAP_H;
sy = HMAP_H - topy;
break;
case 2:
mapsty = HMAP_H;
mapmaxx = HMAP_W;
mapmaxy = MAP_H;
sy = HMAP_H - topy;
break;
case 3:
mapsty = topy;
mapmaxx = HMAP_W;
mapmaxy = HMAP_H;
break;
default:
return;
}
short ox = (screen_data->garea_w * ICON_W - MAP_W) / 2;
short oy = (screen_data->garea_h * ICON_H - MAP_H) / 2;
if (oy == 0)
topy = 0;
else if (oy > 0) {
topy = 0;
char spcs[MAP_W + 2 + 1];
memset(spcs, ' ', MAP_W + 2);
spcs[MAP_W + 2] = '\0';
wattrset(game_win, COLOR_PAIR(ICON_SPACE));
mvwaddstr(game_win, oy - 1, ox - 1, spcs);
mvwaddstr(game_win, oy + MAP_H, ox - 1, spcs);
}
else
oy = 0;
for (xy_t y = mapsty; y < mapmaxy; y++, sy++) {
if (ox > 0) {
wattrset(game_win, COLOR_PAIR(ICON_SPACE));
mvwaddstr(game_win, oy + sy, ox - 1, " ");
mvwaddstr(game_win, oy + sy, MAP_W + ox, " ");
}
for (xy_t x = mapstx; x < mapmaxx; x++) {
if (show) {
xy_t icon = map->buf[y][x];
switch (icon) {
case ICON_WALL:
wattrset(game_win, COLOR_PAIR(COL_I_MAP_WALL));
mvwaddch(game_win, oy + sy, ox + x,
icon_to_mapchar(icon));
break;
case ICON_MASK:
case ICON_EXIT:
wattrset(game_win, COLOR_PAIR(icon));
mvwaddch(game_win, oy + sy, ox + x,
icon_to_mapchar(icon));
break;
default:
wattrset(game_win, COLOR_PAIR(ICON_SPACE));
mvwaddch(game_win,
oy + sy, ox + x, icon_to_mapchar(ICON_SPACE));
break;
}
}
else {
wattrset(game_win, COLOR_PAIR(ICON_SPACE));
mvwaddch(game_win, oy + sy, ox + x, ' ');
}
}
}
}
void
game_win_dump_map(xy_t topy)
{
for (su_t i = 0, mb = 1; i < 4; i++, mb *= 2)
game_win_dump_map_sect(topy, i, mb & player.have_map);
wrefresh(game_win);
return;
}
void
game_win_map_display(void)
{
bool map_scrollable =
(screen_data->garea_h * ICON_H < MAP_H ? TRUE : FALSE);
xy_t y = (map_scrollable ? player.map_view_y : 0);
xy_t maxscrolly = MAP_H - ICON_H * screen_data->garea_h;
game_win_dump_map(y);
while (1) {
int key = wgetch(game_win);
if (key != ERR) {
switch (key) {
case KEY_RESIZE:
screen_resize();
map_scrollable =
(screen_data->garea_h * ICON_H < MAP_H ? TRUE : FALSE);
if (!map_scrollable)
y = 0;
else
maxscrolly = MAP_H - ICON_H * screen_data->garea_h;
if (!screen_data->scale_map)
return;
game_win_dump_map(y);
break;
case '\'':
case KEY_UP:
if (y > 0 && map_scrollable) {
y--;
game_win_dump_map(y);
}
break;
case '/':
case KEY_DOWN:
if (y < maxscrolly && map_scrollable) {
y++;
game_win_dump_map(y);
}
break;
case 'm':
case 'M':
case 'q':
case 'Q':
game_win_display();
player.map_view_y = y;
return;
}
}
}
}