-
Notifications
You must be signed in to change notification settings - Fork 0
/
RENDER.Cpp
559 lines (483 loc) · 10.9 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
/*
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 <math.h>
// INTERFACE DATA //////////////////////////////////////////////////////////////////////////////////
unsigned char animate = 0;
unsigned char cameratracking = 1;
unsigned char tracker = 0;
unsigned char showobs = 0;
unsigned char showzone = 0;
// IMPLEMENTATION DATA /////////////////////////////////////////////////////////////////////////////
static unsigned char curlayer = 0;
// IMPLEMENTATION CODE /////////////////////////////////////////////////////////////////////////////
#pragma off (unreferenced);
void Map_BlitLayer(int lay, int masked, int color_mapped)
{
int c;
int x;
int y;
int x_sub;
int y_sub;
int temp;
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; any multipliers or divisors below 2 have no effect
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 = (true_screen_width + 31)/16;
clip_length = (true_screen_length + 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;
int xcount=0;
do
{
// validate tile request
c = *source;
if (c < 0 || c >= numtiles)
c = 0;
// validate it again
c = tileidx[c];
if ((masked ? c : 1) && c >= 0 && c < numtiles)
{
LFB_Blit(y_sub, y, 16, 16, vsp + (c<<8)*(hicolor?2:1), masked, color_mapped);
}
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);
}
// INTERFACE CODE //////////////////////////////////////////////////////////////////////////////////
void BlitLayer(int lay)
{
if (lay < 0 || lay >= numlayers)
return;
// hline takes precedence
if (layer[lay].hline)
{
ExecuteEvent(layer[lay].hline);
}
// solid / mask / color_mapped are backseat
else
{
Map_BlitLayer(lay, curlayer, layer[lay].trans);
}
curlayer += 1;
}
void DrawObstructions()
{
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 (hicolor) return;
// adjust view; any multipliers or divisors below 2 have no effect
/*
x = xwin;
temp = layer[0].pmultx;
if (temp > 1)
x *= temp;
temp = layer[0].pdivx;
if (temp > 1)
x /= temp;
y = ywin;
temp = layer[0].pmulty;
if (temp > 1)
y *= temp;
temp = layer[0].pdivy;
if (temp > 1)
y /= temp;
*/
x = xwin*layer[0].pmultx/layer[0].pdivx;
y = ywin*layer[0].pmulty/layer[0].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 = (true_screen_width + 31)/16;
clip_length = (true_screen_length + 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[0].sizex
|| y + clip_length - 1 < 0 || y >= layer[0].sizey)
{
return;
}
// clip upper left
if (x + clip_width - 1 >= layer[0].sizex)
clip_width = layer[0].sizex - x;
if (y + clip_length - 1 >= layer[0].sizey)
clip_length = layer[0].sizey - y;
// clip lower right
if (x < 0)
{
clip_width += x;
x = 0;
}
if (y < 0)
{
clip_length += y;
y = 0;
}
source = obstruct + y*layer[0].sizex + x;
y = y_sub;
do
{
y_sub = x_sub; // don't try this at home
x = clip_width;
int xcount=0;
do
{
if (*source)
LFB_BlitStippleTile(y_sub, y, *source);
source += 1;
x -= 1;
y_sub += 16; // x screen position
}
while (x);
source += (layer[0].sizex - clip_width);
y += 16;
clip_length -= 1;
}
while (clip_length);
}
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 (hicolor) return;
// adjust view; any multipliers or divisors below 2 have no effect
/*
x = xwin;
temp = layer[0].pmultx;
if (temp > 1)
x *= temp;
temp = layer[0].pdivx;
if (temp > 1)
x /= temp;
y = ywin;
temp = layer[0].pmulty;
if (temp > 1)
y *= temp;
temp = layer[0].pdivy;
if (temp > 1)
y /= temp;
*/
x = xwin*layer[0].pmultx/layer[0].pdivx;
y = ywin*layer[0].pmulty/layer[0].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 = (true_screen_width + 31)/16;
clip_length = (true_screen_length + 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[0].sizex
|| y + clip_length - 1 < 0 || y >= layer[0].sizey)
{
return;
}
// clip upper left
if (x + clip_width - 1 >= layer[0].sizex)
clip_width = layer[0].sizex - x;
if (y + clip_length - 1 >= layer[0].sizey)
clip_length = layer[0].sizey - y;
// clip lower right
if (x < 0)
{
clip_width += x;
x = 0;
}
if (y < 0)
{
clip_length += y;
y = 0;
}
source = zone + y*layer[0].sizex + x;
y = y_sub;
do
{
y_sub = x_sub; // don't try this at home
x = clip_width;
int xcount=0;
do
{
if (*source)
LFB_BlitStippleTile(y_sub, y, *source);
source += 1;
x -= 1;
y_sub += 16; // x screen position
}
while (x);
source += (layer[0].sizex - clip_width);
y += 16;
clip_length -= 1;
}
while (clip_length);
if (player)
{
x = player->tx*16 - xwin;
y = player->ty*16 - ywin;
LFB_BlitStippleTile(x, y, 31);
}
}
void HookScriptThing(int rpos)
{
int mark=rpos;
while (rpos<rstring.length()
&& ('X' != rstring[rpos] && 'x' != rstring[rpos]))
{
rpos++;
}
int ev=V_atoi(rstring.mid(mark,rpos-mark).c_str());
ExecuteEvent(ev);
}
void RenderMAP()
{
int rpos;
LFB_ClearScreen();
curlayer = 0;
rpos = 0;
while (rpos < rstring.length())
{
switch (rstring[rpos])
{
case '1': BlitLayer(0); break;
case '2': BlitLayer(1); break;
case '3': BlitLayer(2); break;
case '4': BlitLayer(3); break;
case '5': BlitLayer(4); break;
case '6': BlitLayer(5); break;
case 'e':
case 'E': RenderEntities(); break;
case 's':
case 'S': HookScriptThing(rpos); break;
case 'r':
case 'R': HookRetrace(); curlayer++; break;
}
rpos += 1;
}
if (showobs)
DrawObstructions();
if (showzone)
DrawZones();
}
void CameraFocusOn(entity_r* focus)
{
if (!focus)
return;
// center in on him
if (focus->x + 8 > (screen_width/2))
xwin = (focus->x + 8 - (screen_width/2));
else
xwin = 0;
if (focus->y + 8 > (screen_length/2))
ywin = (focus->y + 8 - (screen_length/2));
else
ywin =0;
if (xwin > ((layer[0].sizex << 4) - screen_width))
xwin = ((layer[0].sizex << 4) - screen_width);
if (ywin > ((layer[0].sizey << 4) - screen_length))
ywin = ((layer[0].sizey << 4) - screen_length);
}
void DoCameraTracking()
{
entity_r* 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
if ((1 == cameratracking && player) || (2 == cameratracking))
{
// determine the star of the show
if (1 == cameratracking)
focus = player;
else
focus = entity + tracker;
CameraFocusOn(focus);
}
}
void Render()
{
static inside = 0;
// for twerps who call Render() in a HookRetrace or HookTimer
if (inside)
return;
inside = 1;
DoCameraTracking();
SiftEntities();
RenderMAP();
inside = 0;
}
int rnd(int lo, int hi)
{
hi = hi - lo + 1;
if (hi > 0)
hi = rand() % hi;
else
hi = 0;
return lo + hi;
}
void AnimateTile(int i, int l)
{
if (i >= 100)
return;
if (l < 0 || l >= numtiles)
return;
switch (vspanim[i].mode)
{
case 0:
if (tileidx[l] < vspanim[i].finish)
tileidx[l] += 1;
else
tileidx[l] = vspanim[i].start;
break;
case 1:
if (tileidx[l] > vspanim[i].start)
tileidx[l] -= 1;
else
tileidx[l] = vspanim[i].finish;
break;
case 2:
tileidx[l] = (unsigned short) rnd(vspanim[i].start, vspanim[i].finish);
break;
case 3:
if (flipped[l])
{
if (tileidx[l] != vspanim[i].start)
tileidx[l] -= 1;
else
{
tileidx[l] += 1;
flipped[l] = 0;
}
}
else
{
if (tileidx[l] != vspanim[i].finish)
tileidx[l] += 1;
else
{
tileidx[l] -= 1;
flipped[l] = 1;
}
}
}
}
void Animate(int i)
{
int l;
vadelay[i] = 0;
for (l = vspanim[i].start; l <= vspanim[i].finish; l += 1)
AnimateTile(i, l);
}
void CheckTileAnimation()
{
int i;
if (!animate) return;
if (!vsp) return;
for (i = 0; i < 100; i += 1)
{
if ((vspanim[i].delay) && (vspanim[i].delay < vadelay[i]))
Animate(i);
vadelay[i] += 1;
}
}