forked from EXL/P2kElfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyeti.c
588 lines (496 loc) · 13.5 KB
/
yeti.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
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
/*
Copyright (C) 2003 - Derek John Evans
This file is part of Yeti3D Portable Engine
Yeti3D 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.
Original Source: 2003 - Derek J. Evans <[email protected]>
Prepared for public release: 10/24/2003 - Derek J. Evans <[email protected]>
*/
/*
** Name: Yeti3D
** Desc: Portable GameBoy Advanced 3D Engine
** Auth: Derek J. Evans <[email protected]>
**
** Copyright (C) 2003 Derek J. Evans. All Rights Reserved.
$**
** YY YY EEEEEE TTTTTT IIIIII 33333 DDDDD
** YY YY EE TT II 33 DD DD
** YYYY EEEE TT II 333 DD DD
** YY EE TT II 33 DD DD
** YY EEEEEE TT IIIIII 33333 DDDDD
*/
#include "yeti.h"
#if defined(__GBA__)
/******************************************************************************/
/*
** Name: palette_overbright
** Desc: Converts a standard palette to a overbright palette. Use this on
** platforms that bypass the LUA.
*/
void palette_overbright(palette_t dst, palette_t src, int brightness)
{
int i;
for (i = 0; i < 256; i++)
{
int r = src[i][0] * brightness / FIXONE;
int g = src[i][1] * brightness / FIXONE;
int b = src[i][2] * brightness / FIXONE;
dst[i][0] = CLAMP(r, 0, 255);
dst[i][1] = CLAMP(g, 0, 255);
dst[i][2] = CLAMP(b, 0, 255);
}
}
/*
** Name: rgb_convert
** Desc: Converts a rgb color to another format.
*/
int rgb_convert(int color, int rmask, int gmask, int bmask)
{
return
((RGB_RED (color) * rmask / 31) & rmask) |
((RGB_GREEN(color) * gmask / 31) & gmask) |
((RGB_BLUE (color) * bmask / 31) & bmask) ;
}
/*
** Name: viewport_to_video
** Desc: The Yeti3D uses 5:5:5 colour, so if your platform uses a different
** format, then one solution is to convert the pixels when blitting to
** the video buffer.
*/
#ifdef YETI_VIDEO_LUT
void viewport_to_video(
rgb555_t* video, int pitch,
viewport_t* vp,
int rmask, int gmask, int bmask)
{
int i, y;
if (!vp->video_lut_filled)
{
vp->video_lut_filled = TRUE;
for (i = RGB_MAX; i--;)
{
vp->video_lut[i] = rgb_convert(i, rmask, gmask, bmask);
}
}
for (y = 0; y < YETI_VIEWPORT_HEIGHT; y++)
{
rgb555_t* fb = video;
rgb555_t* tb= vp->back->pixels[y];
i = YETI_VIEWPORT_WIDTH;
#define AFFINE(I) {fb[I] = vp->video_lut[*tb++];}
AFFINE_LOOP
#undef AFFINE
video = (rgb555_t*)((int)video + pitch);
}
}
#endif
#endif
/******************************************************************************/
/*
** Name: cell_init
** Desc: Initialize a yeti cell to its defaults.
*/
void cell_init(cell_t* cell, int issolid)
{
CLEARMEM(cell);
if (issolid)
{
cell->top = i2f(0);
cell->bot = i2f(0);
}
else
{
cell->top = i2f(4);
cell->bot = i2f(0);
}
#ifdef __YETI_EDITOR__
cell->tos = cell->top;
cell->bos = cell->bot;
#endif
cell->lit = YETI_LIGHT_MAX;
cell->ttx = 7;
cell->wtx = 0;
cell->btx = 28;
}
/*
** Name: yeti_entity
** Desc: Creates a new entity and returns a entity pointer.
*/
entity_t* yeti_entity(yeti_t* yeti, int x, int y, int z, entity_behaviour_t behaviour)
{
entity_t* e = &yeti->entities[yeti->nentities++];
CLEARMEM(e);
e->x = x;
e->y = y;
e->z = z;
e->ontick = behaviour;
e->radius = i2fdiv2(1);
e->life = 100;
e->yeti = yeti;
return e;
}
/*
** Name: yeti_light
** Desc: Renders a spot light at a given cell location.
*/
void yeti_light(yeti_t* yeti, int lightx, int lighty)
{
int i, lit, xhit, yhit;
for (i = 0; i < 2048; i += 8)
{
int x = lightx, xx = fixsin(i) >> 1;
int y = lighty, yy = fixcos(i) >> 1;
for (lit = 63; lit > 0; )
{
x += xx; if ((xhit = CELL_IS_SOLID(&yeti->cells[f2i(y)][f2i(x)])) != 0) xx = -xx;
y += yy; if ((yhit = CELL_IS_SOLID(&yeti->cells[f2i(y)][f2i(x)])) != 0) yy = -yy;
yeti->cells[f2i(y + FIXHALF)][f2i(x + FIXHALF)].lit += lit;
if (xhit || yhit) lit >>= 2;
}
}
}
/*
** Name: entity_motion
** Desc: Simple map to entity motion & collision.
*/
void entity_motion(entity_t* e)
{
yeti_t* yeti = e->yeti;
e->x += e->xx; e->y += e->yy; e->z += e->zz;
e->r += e->rr; e->t += e->tt; e->p += e->pp;
if (e->radius)
{
int x, y;
cell_t* cell;
#define IS_COLLISION ((e->z > cell->top) || (e->z < cell->bot))
cell = &yeti->cells[f2i(e->y)][f2i(x = e->x - e->radius)];
if (IS_COLLISION)
{
e->x += FIXONE - (x & FIXCEIL);
e->xx = 0;
}
cell = &yeti->cells[f2i(e->y)][f2i(x = e->x + e->radius)];
if (IS_COLLISION)
{
e->x -= (x & FIXCEIL);
e->xx = 0;
}
cell = &yeti->cells[f2i(y = e->y - e->radius)][f2i(e->x)];
if (IS_COLLISION)
{
e->y += FIXONE - (y & FIXCEIL);
e->yy = 0;
}
cell = &yeti->cells[f2i(y = e->y + e->radius)][f2i(e->x)];
if (IS_COLLISION)
{
e->y -= (y & FIXCEIL);
e->yy = 0;
}
}
}
/******************************************************************************/
void yeti_clear_entities(yeti_t* yeti)
{
int i;
yeti->nentities = 0;
yeti->camera = yeti_entity(yeti,
i2fdiv2(YETI_MAP_WIDTH),
i2fdiv2(YETI_MAP_HEIGHT),
i2f(0), 0);
for (i = 0; i < YETI_BULLET_MAX; i++)
{
yeti->bullets[i] = yeti_entity(yeti, 0, 0, 0, 0);
}
}
/*
** Name: yeti_load_cell
** Desc: Loads a cell from a ROM based structure.
*/
void yeti_load_cell(cell_t* dst, rom_cell_t* src)
{
dst->swi = src->swi;
dst->ent = src->ent;
dst->top = src->top;
dst->mid = src->mid;
dst->bot = src->bot;
dst->wtx = src->wtx;
dst->ttx = src->ttx;
dst->btx = src->btx;
dst->lit = src->lit;
}
/*
** Name: yeti_save_cell
** Desc: Saves a cell from a ROM based structure.
*/
void yeti_save_cell(cell_t* src, rom_cell_t* dst)
{
dst->swi = src->swi;
dst->ent = src->ent;
dst->top = src->top;
dst->mid = src->mid;
dst->bot = src->bot;
dst->wtx = src->wtx;
dst->ttx = src->ttx;
dst->btx = src->btx;
dst->lit = src->lit;
}
void yeti_load_map(yeti_t* yeti, rom_map_t* src)
{
int x, y;
for (y = 0; y < YETI_MAP_HEIGHT; y++)
{
for (x = 0; x < YETI_MAP_WIDTH; x++)
{
yeti_load_cell(&yeti->cells[y][x], &src->cells[y][x]);
}
}
if (!yeti->camera) yeti_clear_entities(yeti);
for (y = 0; y < YETI_MAP_HEIGHT; y++)
{
for (x = 0; x < YETI_MAP_WIDTH; x++)
{
if (yeti->cells[y][x].ent == 1)
{
yeti->camera->x = i2f(x) + FIXHALF;
yeti->camera->y = i2f(y) + FIXHALF;
}
}
}
yeti->camera->t = 0;
yeti->camera->r = 0;
yeti->camera->p = i2f(256);
}
#if defined(__GBA__)
/*
** Name: yeti_save_map
** Desc: Saves the current map to a rom based map.
*/
void yeti_save_map(yeti_t* yeti, rom_map_t* map)
{
int x, y;
for (y = 0; y < YETI_MAP_HEIGHT; y++)
{
for (x = 0; x < YETI_MAP_WIDTH; x++)
{
yeti_save_cell(&yeti->cells[y][x], &map->cells[y][x]);
}
}
}
#endif
/*
** Name: yeti_init_map
** Desc: Clears a yeti map to its default settings.
*/
void yeti_init_map(yeti_t* yeti)
{
int x, y;
for (y = 0; y < YETI_MAP_HEIGHT; y++)
{
for (x = 0; x < YETI_MAP_WIDTH; x++)
{
cell_t* cell = &yeti->cells[y][x];
cell_init(cell,
x == 0 || y == 0 ||
(x == YETI_MAP_WIDTH - 1) ||
(y == YETI_MAP_HEIGHT - 1));
if (!((x+8)&15) && !((y+8)&15))
{
cell->swi |= CELL_SWI_LIGHT;
}
}
}
}
/******************************************************************************/
#if defined(__GBA__)
/*
** Name: isqrt
** Desc: Integer square root. Take the square root of an integer.
*/
int isqrt(int value)
{
int root = 0;
#define STEP(shift) \
if((0x40000000 >> shift) + root <= value) \
{ \
value -= (0x40000000 >> shift) + root; \
root = (root >> 1) | (0x40000000 >> shift); \
} \
else \
{ \
root >>= 1; \
}
STEP( 0); STEP( 2); STEP( 4); STEP( 6);
STEP( 8); STEP(10); STEP(12); STEP(14);
STEP(16); STEP(18); STEP(20); STEP(22);
STEP(24); STEP(26); STEP(28); STEP(30);
// round to the nearest integer, cuts max error in half
if (root < value) root++;
return root;
}
#endif
/******************************************************************************/
/*
** Name: yeti_init
** Desc: Setup a default map and position the camera.
*/
void yeti_init(
yeti_t* yeti,
framebuffer_t* front,
framebuffer_t* back,
texture_t* textures,
palette_t palette,
lua_t lua)
{
int i;
CLEARMEM(yeti);
for (i = 0; i < YETI_TEXTURE_MAX; i++)
{
yeti->surfaces[i].xsize = 6;
yeti->surfaces[i].ysize = 6;
}
yeti->surfaces[YETI_TEXTURE_SKY].xsize = 5;
yeti->surfaces[YETI_TEXTURE_SKY].ysize = 5;
yeti->surfaces[0].xsize = 4;
yeti->surfaces[0].ysize = 4;
yeti->surfaces[2].xsize = 5;
yeti->surfaces[2].ysize = 5;
yeti_init_map(yeti);
yeti->viewport.front = front;
yeti->viewport.back = back;
yeti->textures = textures;
yeti->palette = palette;
yeti->lighting[0] = lua;
yeti->lighting[1] = lua;
yeti->lighting[2] = lua;
yeti->lighting[3] = lua;
}
void yeti_tick(yeti_t* yeti)
{
int i;
yeti->tick++;
for (i = 0; i < yeti->nentities; i++)
{
entity_t* e = &yeti->entities[i];
if (e->ontick) e->ontick(e);
}
for (i = 0; i < yeti->nentities; i++)
{
entity_t* e1 = &yeti->entities[i];
entity_motion(e1);
if (e1->radius)
{
int j;
for (j = i + 1; j < yeti->nentities; j++)
{
entity_t* e2 = &yeti->entities[j];
if (e2->radius)
{
int size = e1->radius + e2->radius;
int x = e1->x - e2->x;
int y = e1->y - e2->y;
int z = e1->z - e2->z;
if (
x < size && x > -size &&
y < size && y > -size &&
z < size && z > -size )
{
if (e1->onhit) e1->onhit(e1, e2);
if (e2->onhit) e2->onhit(e2, e1);
if (!(e1->swi & ENTITY_SWI_NO_COLLISION_RESPONSE))
{
if (x > 0) {e1->x += ( size - x) >> 1;}
if (x < 0) {e1->x += (-size - x) >> 1;}
if (y > 0) {e1->y += ( size - y) >> 1;}
if (y < 0) {e1->y += (-size - y) >> 1;}
}
if (!(e2->swi & ENTITY_SWI_NO_COLLISION_RESPONSE))
{
if (x > 0) {e2->x -= ( size - x) >> 1;}
if (x < 0) {e2->x -= (-size - x) >> 1;}
if (y > 0) {e2->y -= ( size - y) >> 1;}
if (y < 0) {e2->y -= (-size - y) >> 1;}
}
}
}
}
}
}
yeti->keyboard._a = yeti->keyboard.a;
yeti->keyboard._b = yeti->keyboard.b;
yeti->keyboard._select = yeti->keyboard.select;
yeti->keyboard._left = yeti->keyboard.left;
yeti->keyboard._right = yeti->keyboard.right;
yeti->keyboard._up = yeti->keyboard.up;
yeti->keyboard._down = yeti->keyboard.down;
yeti->keyboard._r = yeti->keyboard.r;
yeti->keyboard._l = yeti->keyboard.l;
yeti->surfaces[YETI_TEXTURE_SKY].xpan = -(yeti->camera->x>>1) + (yeti->tick<<2);
yeti->surfaces[YETI_TEXTURE_SKY].ypan = -(yeti->camera->y>>1) + (yeti->tick<<2);
yeti->surfaces[YETI_TEXTURE_WINDOW].xpan = -(yeti->camera->y>>1) + (yeti->tick<<2);
yeti->surfaces[YETI_TEXTURE_LAVA ].xpan = fixsin(yeti->tick << 4) >> 2;
yeti->surfaces[YETI_TEXTURE_LAVA ].ypan = fixcos(yeti->tick << 4) >> 2;
yeti->surfaces[YETI_TEXTURE_WATER].xpan = fixsin(yeti->tick << 4) >> 2;
yeti->surfaces[YETI_TEXTURE_WATER].ypan = fixcos(yeti->tick << 4) >> 2;
}
#if defined(__GBA__) || defined(__SDL2__)
/*
** Name: yeti_ambient_lighting
** Desc: Setup a standard ambient lighting. Current lighting is removed.
** Note: This is obsolete since we have better lighting code now.
*/
void yeti_ambient_lighting(yeti_t* yeti, int lit)
{
int x, y;
for (y = 0; y < YETI_MAP_HEIGHT; y++)
{
for (x = 0; x < YETI_MAP_WIDTH; x++)
{
yeti->cells[y][x].lit = lit;
}
}
}
/*
** Name: yeti_default_lighting
** Desc: Renders a default lighting setup for maps that dont have lights. This
** is currently the only lighting system and is designed to give good
** results for all map designs.
*/
void yeti_default_lighting(yeti_t* yeti)
{
int x, y;
yeti_ambient_lighting(yeti, i2f(0));
for (y = 1; y < YETI_MAP_HEIGHT - 1; y++)
{
for (x = 1; x < YETI_MAP_WIDTH - 1; x++)
{
cell_t* cell = &yeti->cells[y][x];
if (cell->swi & CELL_SWI_LIGHT)
{
yeti_light(yeti, i2f(x) + FIXHALF, i2f(y) + FIXHALF);
}
}
}
}
/******************************************************************************/
void* yeti_memcpy(void* d, void* s, unsigned n)
{
while (n--) ((u8*)d)[n] = ((u8*)s)[n];
return d;
}
void* yeti_memset(void *s, int c, unsigned n)
{
while (n--) ((u8*)s)[n] = c;
return s;
}
#endif
/******************************************************************************/