-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.cpp
670 lines (553 loc) · 14.6 KB
/
render.cpp
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
Copyright (C) 1998 BJ Eirich (aka vecna)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// /---------------------------------------------------------------------\
// | The VERGE Engine |
// | Copyright (C)1998 BJ Eirich (aka vecna) |
// | Rendering module |
// \---------------------------------------------------------------------/
#include "verge.h"
#include "misc.h"
#include "tileanim.h"
#include "engine.h"
#include "Map/MapFile.h"
#include "vsp.h"
#include "Console.h"
#include <math.h>
#include <algorithm>
using Map::MapFile;
// INTERFACE DATA //////////////////////////////////////////////////////////////////////////////////
unsigned char animate = 0;
unsigned char cameratracking = 1;
int tracker = 0;
unsigned char showobs = 1;
unsigned char showzone = 0;
// IMPLEMENTATION CODE /////////////////////////////////////////////////////////////////////////////
// -------------------------- Map ----------------------------
/*
template <typename T>
void RenderLayer(int lay, T DrawTile)
{
int c;
int x;
int y;
int x_sub;
int y_sub;
int clip_width;
int clip_length;
unsigned short* source;
// validate arguments
if (lay < 0 || lay >= numlayers)
return;
// is this layer visible?
if (!layertoggle[lay])
return;
// adjust view according to parallax
x = xwin*layer[lay].pmultx/layer[lay].pdivx;
y = ywin*layer[lay].pmulty/layer[lay].pdivy;
// make my life easier; don't allow scrolling past map edges
if (x < 0)
x = 0;
if (y < 0)
y = 0;
// get subtile position while we still have pixel precision
x_sub = -(x & 15);
y_sub = -(y & 15);
// determine upper left tile coords of camera
x >>= 4;
y >>= 4;
// calculate tiled rows and columns
clip_width = (gfx.scrx + 31)/16;
clip_length = (gfx.scry + 31)/16;
// safeguard; this should never happen due to camera bounding
// ie. if a map is set as visible, there should be something to draw at all times
if (x + clip_width - 1 < 0 || x >= layer[lay].sizex
|| y + clip_length - 1 < 0 || y >= layer[lay].sizey)
{
return;
}
// clip upper left
if (x + clip_width - 1 >= layer[lay].sizex)
clip_width = layer[lay].sizex - x;
if (y + clip_length - 1 >= layer[lay].sizey)
clip_length = layer[lay].sizey - y;
// clip lower right
if (x < 0)
{
clip_width += x;
x = 0;
}
if (y < 0)
{
clip_length += y;
y = 0;
}
source = layers[lay] + y*layer[lay].sizex + x;
y = y_sub;
do
{
y_sub = x_sub; // don't try this at home
x = clip_width;
do
{
// validate tile request
c = *source;
if (c < 0 || c >= vsp->NumTiles())
c = 0;
// validate it again
c = tileanim.GetTileIdx(c);
if (c >= 0 && c < vsp->NumTiles())
{
DrawTile(y_sub, y, c);
}
source += 1;
x -= 1;
y_sub += 16; // x screen position
}
while (x);
source += (layer[lay].sizex - clip_width);
y += 16;
clip_length -= 1;
}
while (clip_length);
}*/
template <class T>
void RenderLayer(MapFile::TileLayer& l, T DrawTile)
{
const int xmax = l.Width()*16-gfx.XRes();
const int ymax = l.Height()*16-gfx.YRes();
int xw=(int)(xwin*l.parx);
int yw=(int)(ywin*l.pary);
if (xw>=xmax) xw = xmax-1;
if (yw>=ymax) yw = ymax-1;
if (xw<0) xw = 0;
if (yw<0) yw = 0;
// the tile to be drawn in the upper left corner
int xtile = xw/16;
int ytile = yw/16;
// Subtile adjustment for each tile
int xadj=-(xw&15);
int yadj=-(yw&15);
// Number of tiles to blit (on each axis)
int xlen=(gfx.XRes()+16)/16; // round up
int ylen=(gfx.YRes()+16)/16;
// Clipping
if (xtile+xlen>l.Width()) xlen = l.Width() -xtile;
if (ytile+ylen>l.Height()) ylen = l.Height()-ytile;
// current drawing location
int curx = xadj;
int cury = yadj;
MapFile::TileLayer::Iterator iter(l);
for (int y = 0; y<ylen; y++)
{
iter.MoveTo(xtile, y+ytile);
for (int x = xlen; x; x--)
{
u32 t=*iter;
// animation
t = tileanim.GetTileIdx(t);
if (t>=0 && t<vsp->NumTiles())
DrawTile(curx, cury, t);
iter++;
curx+=16;
}
curx = xadj;
cury+=16;
}
}
namespace
{
// Decorator pattern
template <class _Iter, typename T>
class AnimIter
{
_Iter iter;
public:
AnimIter(_Iter i) : iter(i){}
inline AnimIter& operator ++(int)
{
iter++;
}
inline T& operator *()
{
T t=*iter;
return tileanim.GetTileIdx(t);
}
void MoveTo(int x, int y)
{
iter.MoveTo(x, y);
}
};
class GetObs
{
public:
inline int operator ()(int x, int y)
{
return map->Obs().Get(x, y);
}
};
class GetZone
{
public:
inline int operator ()(int x, int y)
{
return map->Zone().Get(x, y);
}
};
class OpaqueBlit
{
public:
inline void operator ()(int x, int y, int t)
{
gfx.CopySprite(x, y, 16, 16, (u8*)vsp->GetTile(t));
}
};
class TransparentBlit
{
public:
inline void operator ()(int x, int y, int t)
{
if (t)
gfx.TCopySprite(x, y, 16, 16, (u8*)vsp->GetTile(t));
}
};
class LucentBlit
{
int mode;
public:
LucentBlit(int m) : mode(m) {}
inline void operator ()(int x, int y, int t)
{
if (t)
gfx.TCopySpriteLucent(x, y, 16, 16, (u8*)vsp->GetTile(t), mode);
}
};
class StippleBlit
{
public:
inline void operator ()(int x, int y, int t)
{
if (t)
gfx.BlitStipple(x, y, t);
}
};
};
void BlitLayer(int lay, bool trans)
{
if (lay < 0 || lay >= map->NumLayers())
return;
// hline takes precedence
/* if (layer[lay].hline)
{
ExecuteEvent(layer[lay].hline);
}
// solid / mask / color_mapped are backseat
else*/
{
MapFile::TileLayer& layer = map->GetLayer(lay);
if (!trans)
RenderLayer(layer, OpaqueBlit());
else if (layer.trans != 0)
RenderLayer(layer, LucentBlit(layer.trans));
else
RenderLayer(layer, TransparentBlit());
}
}
void DrawObstructions()
{
//MBG 09-mar-2020 - seems incomplete?
//RenderLayer(map->Obs(), MapFile::Layer<bool>::Iterator(map->Obs()), StippleBlit());
/* // the tile to be drawn in the upper left corner
int xtile = xwin/16;
int ytile = ywin/16;
// Subtile adjustment for each tile
int xadj=-(xwin&15);
int yadj=-(ywin&15);
// Number of tiles to blit (on each axis)
int xlen=(gfx.XRes()+16)/16; // round up
int ylen=(gfx.YRes()+16)/16;
// current drawing location
int curx = xadj;
int cury = yadj;
MapFile::Layer<bool>::Iterator iter(map->Obs(), xtile, ytile);
for (int y = 0; y<ylen; y++)
{
for (int x = xlen; x; x--)
{
bool t=*iter;
if (t!=0)
gfx.BlitStipple(curx, cury, 0);
iter++;
curx+=16;
}
curx = xadj;
cury+=16;
iter.MoveTo(xtile, y+ytile);
}*/
}
void DrawZones()
{
/* int x;
int y;
int x_sub;
int y_sub;
int temp;
int clip_width;
int clip_length;
unsigned char* source;
// debugging for now
// if (gfx.bpp>1) return;
// adjust view according to parallax (NO NO PARALLAX NOT HERE)
x = xwin;
y = ywin;
// make my life easier; don't allow scrolling past map edges
if (x < 0)
x = 0;
if (y < 0)
y = 0;
// get subtile position while we still have pixel precision
x_sub = -(x & 15);
y_sub = -(y & 15);
// determine upper left tile coords of camera
x >>= 4;
y >>= 4;
// calculate tiled rows and columns
clip_width = (gfx.XRes() + 31)/16;
clip_length = (gfx.YRes() + 31)/16;
// safeguard; this should never happen due to camera bounding
// ie. if a map is set as visible, there should be something to draw at all times
if (x + clip_width - 1 < 0 || x >= map->Width()
|| y + clip_length - 1 < 0 || y >= map->Height())
{
return;
}
// clip upper left
if (x + clip_width - 1 >= map->Width())
clip_width = map->Width() - x;
if (y + clip_length - 1 >= map->Height())
clip_length = map->Height() - y;
// clip lower right
if (x < 0)
{
clip_width += x;
x = 0;
}
if (y < 0)
{
clip_length += y;
y = 0;
}
source = zone + y*map->Width() + x;
y = y_sub;
do
{
y_sub = x_sub; // don't try this at home
x = clip_width;
do
{
if (*source)
gfx.BlitStipple(y_sub, y, *source);
source += 1;
x -= 1;
y_sub += 16; // x screen position
}
while (x);
source += (map->Width() - clip_width);
y += 16;
clip_length -= 1;
}
while (clip_length);
for (temp = 0; temp < entities; temp++)
{
x = ents[temp].x - xwin;
y = ents[temp].y - ywin;
gfx.BlitStipple(x, y, 32);
}*/
}
void HookScriptThing(unsigned int &rpos)
{
int mark = rpos+1;
while (
rpos < map->rstring.length() &&
('X' != map->rstring[rpos] && 'x' != map->rstring[rpos])
)
{
rpos++;
}
int ev = atoi(map->rstring.substr(mark, rpos - mark).c_str());
ExecuteEvent(ev);
}
void CameraFocusOn(Entity* focus)
{
if (!focus)
return;
xwin = focus->x - gfx.scrx/2;
ywin = focus->y - gfx.scry/2;
// in case the map is smaller than the screen
const int maxx = map->Width()*16 - gfx.scrx;
const int maxy = map->Height()*16 - gfx.scry;
if (xwin>maxx) xwin = maxx;
if (ywin>maxy) ywin = maxy;
if (xwin<0) xwin = 0;
if (ywin<0) ywin = 0;
}
void DoCameraTracking()
{
Entity* focus;
// there's 3 basic camera tracking modes:
// #1 focus on player
// #2 focus on specific entity; could be anyone
// #? anything else disables camera tracking
switch (cameratracking)
{
case 1:
if (playeridx != -1)
focus=&ents[playeridx];
else
return;
break;
case 2:
if (tracker != -1)
focus = &ents[tracker];
else
return;
break;
default:
return;
}
CameraFocusOn(focus);
}
int rnd(int lo, int hi)
{
hi = hi - lo + 1;
if (hi > 0)
hi = rand() % hi;
else
hi = 0;
return lo + hi;
}
// ----------------------------- Entities
void DrawEntity(int i)
{
Entity& ent = ents[i];
if (!ent.visible || !ent.on)
return;
int dx = ent.x-xwin;
int dy = ent.y-ywin;
if (ent.chrindex < 0 || ent.chrindex >= numchrs) return;
Sprite& sprite=*chr[ent.chrindex];
int frame = ent.specframe ? ent.specframe : ent.curframe;
if (frame < 0 || frame >= sprite.NumFrames())
{
Log::Write(va("DrawEntity: invalid frame request: %d (%d total)", frame, sprite.NumFrames()));
frame = 0;
}
gfx.TCopySprite(
dx - sprite.HotX(),
dy - sprite.HotY(),
sprite.Width(),
sprite.Height(),
(u8*)sprite.GetFrame(frame));
}
// if the entity is onscreen, then its index is added to the entidx vector. numentsonscreen is the number of entities onscreen.
void SiftEntities()
{
// entidx has as many elements as there are entities so that this thing isn't resized every frame. ;P
if (entidx.size()!=entities)
entidx.resize(entities);
numentsonscreen = 0;
for (int n = 0; n<entities; n++)
{
Entity& ent = ents[n];
int idx = ents[n].chrindex;
// These are the easiest to check for, so we do them first.
if (!chr[idx]) continue;
if (!ents[n].visible || !ents[n].on) continue;
int dx = ents[n].x-xwin+16;
int dy = ents[n].y-ywin+16;
if (dx<0 || dx>gfx.scrx+chr[ents[n].chrindex]->Width())
continue;
if (dy<0 || dy>gfx.scry+chr[ents[n].chrindex]->Height())
continue;
entidx[numentsonscreen++]=n;
}
}
namespace
{
class CompareEntities
{
public:
inline int operator()(int a, int b)
{
return ents[a].y<ents[b].y;
}
};
};
void RenderEntities()
{
SiftEntities();
// qsort sucks. <:D
std::sort(entidx.begin(), entidx.begin()+numentsonscreen, CompareEntities());
int n = 0;
while (n<numentsonscreen)
{
DrawEntity(entidx[n]);
n++;
}
}
// ------------------------- Main interface ------------------
void RenderMAP()
{
static int inside = 0;
unsigned int rpos = 0;
tileanim.Update(); // not ideal. Animations will stop when VC is running.
if (!map) return;
DoCameraTracking();
int layCount = 0;
while (rpos < map->rstring.length())
{
char c = map->rstring[rpos];
if (c>='1' && c<='6')
{
BlitLayer(c-'1', (layCount++) != 0);
}
else
switch (c)
{
case 'e':
case 'E': RenderEntities(); break;
case 's':
case 'S': HookScriptThing(rpos); break;
case 'r':
case 'R': if (!inside)
{
inside = 1;
HookRetrace();
inside = 0;
}
break;
}
rpos += 1;
}
if (showobs)
DrawObstructions();
if (showzone)
DrawZones();
}
void Render()
{
// DoCameraTracking();
RenderMAP();
console.Draw();
}