e46738122a75311c93dc6cd23b7359d77e00d76e
[gbajam22] / src / gamescr.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "gbaregs.h"
4 #include "game.h"
5 #include "dma.h"
6 #include "util.h"
7 #include "intr.h"
8 #include "input.h"
9 #include "gba.h"
10 #include "sprite.h"
11 #include "timer.h"
12 #include "debug.h"
13 #include "voxscape.h"
14 #include "data.h"
15
16 #define FOV             30
17 #define NEAR    2
18 #define FAR             85
19
20 #define P_RATE  500
21 #define E_RATE  500
22
23 static int gamescr_start(void);
24 static void gamescr_stop(void);
25 static void gamescr_frame(void);
26 static void gamescr_vblank(void);
27
28 static int update(void);
29 static void draw(void);
30
31 static struct screen gamescr = {
32         "game",
33         gamescr_start,
34         gamescr_stop,
35         gamescr_frame,
36         gamescr_vblank
37 };
38
39 struct enemy {
40         struct vox_object vobj;
41         short hp;
42         short anm;
43         int last_shot;
44 };
45 #define ENEMY_VALID(e)  ((e)->anm != 0)
46
47 static uint16_t *framebuf;
48
49 static int nframes, backbuf;
50 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
51
52 static int32_t pos[2], angle, horizon = 80;
53 static unsigned long last_shot;
54
55 #define COLOR_HORIZON   192
56 #define COLOR_ZENITH    255
57
58 #define MAX_SPR         32
59 static uint16_t oam[4 * MAX_SPR];
60 static int dynspr_base, dynspr_count;
61
62
63 #define MAX_ENEMIES             (255 - CMAP_SPAWN0)
64 struct enemy enemies[MAX_ENEMIES];
65 int num_kills, total_enemies;
66 static int energy;
67 #define MAX_ENERGY      5
68
69
70 #define XFORM_PIXEL_X(x, y)     (xform_ca * (x) - xform_sa * (y) + (120 << 8))
71 #define XFORM_PIXEL_Y(x, y)     (xform_sa * (x) + xform_ca * (y) + (80 << 8))
72 static int32_t xform_sa, xform_ca;      /* for viewport bank/zoom */
73 static int xform_s;
74
75 static short vblcount;
76 static void *prev_iwram_top;
77
78
79 static inline void xform_pixel(int *xp, int *yp);
80
81
82 struct screen *init_game_screen(void)
83 {
84         return &gamescr;
85 }
86
87 static void setup_palette(void)
88 {
89         int i;
90         unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
91
92         emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
93
94         for(i=0; i<256; i++) {
95                 int r = cmap[i * 3];
96                 int g = cmap[i * 3 + 1];
97                 int b = cmap[i * 3 + 2];
98                 gba_bgpal[i] = RGB555(r, g, b);
99         }
100 }
101
102 static int gamescr_start(void)
103 {
104         int i, j, sidx;
105         uint8_t *cptr;
106         struct enemy *enemy;
107
108         prev_iwram_top = iwram_sbrk(0);
109
110         gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
111         fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
112
113         vblperf_setcolor(0);
114
115         pos[0] = pos[1] = VOX_SZ << 15;
116         angle = 0x8000;
117         last_shot = timer_msec > P_RATE ? timer_msec - P_RATE : 0;
118
119         vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
120         vox_proj(FOV, NEAR, FAR);
121         vox_view(pos[0], pos[1], -40, angle);
122
123         /* setup color image palette */
124         setup_palette();
125
126         spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
127         wait_vblank();
128         spr_clear();
129
130         for(i=0; i<MAX_SPR; i++) {
131                 spr_oam_clear(oam, i);
132         }
133
134         sidx = 0;
135         spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
136         spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
137         spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
138         spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
139         spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
140         spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
141         dynspr_base = sidx;
142
143         num_kills = total_enemies = 0;
144         energy = 5;
145
146         memset(enemies, 0, sizeof enemies);
147         cptr = color_pixels;
148         for(i=0; i<VOX_SZ; i++) {
149                 for(j=0; j<VOX_SZ; j++) {
150                         if(*cptr == 255) {
151                                 /* player spawn point */
152                                 pos[0] = j << 16;
153                                 pos[1] = i << 16;
154
155                         } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
156                                 /* enemy spawn point */
157                                 int idx = *cptr - CMAP_SPAWN0;
158                                 enemy = enemies + idx;
159                                 if(enemy->anm) {
160                                         panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
161                                                         j, i, enemy->vobj.x, enemy->vobj.y);
162                                 }
163                                 enemy->vobj.x = j;
164                                 enemy->vobj.y = i;
165                                 enemy->vobj.px = -1;
166                                 enemy->anm = 0xff;
167                                 enemy->hp = 2;
168                                 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
169                                 if(++total_enemies >= MAX_ENEMIES) {
170                                         goto endspawn;
171                                 }
172                         }
173                         cptr++;
174                 }
175         }
176 endspawn:
177         /* check continuity */
178         for(i=0; i<total_enemies; i++) {
179                 if(enemies[i].anm <= 0) {
180                         panic(get_pc(), "discontinuous enemy list");
181                 }
182                 enemies[i].anm = rand() & 7;
183         }
184
185         vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
186
187         energy = MAX_ENERGY;
188         xform_sa = 0;
189         xform_ca = 0x10000;
190         xform_s = 0x100;
191
192         vblcount = 0;
193         nframes = 0;
194         return 0;
195 }
196
197 static void gamescr_stop(void)
198 {
199         iwram_brk(prev_iwram_top);
200
201         wait_vblank();
202         /* clear sprites */
203         spr_clear();
204         /* reset background rot/scale state */
205         REG_BG2X = 0;
206         REG_BG2Y = 0;
207         REG_BG2PA = 0x100;
208         REG_BG2PB = 0;
209         REG_BG2PC = 0;
210         REG_BG2PD = 0x100;
211
212 }
213
214 static void gamescr_frame(void)
215 {
216         backbuf = ++nframes & 1;
217         framebuf = vram[backbuf];
218
219         vox_framebuf(240, 160, framebuf, horizon);
220
221         if(update() == -1) {
222                 return;
223         }
224         draw();
225
226         vblperf_end();
227         wait_vblank();
228         present(backbuf);
229
230         if(!(nframes & 15)) {
231                 emuprint("vbl: %d", vblperf_count);
232         }
233 #ifdef VBLBAR
234         vblperf_begin();
235 #else
236         vblperf_count = 0;
237 #endif
238 }
239
240 #define NS(x)   (SPRID_UINUM + ((x) << 1))
241 static int numspr[][2] = {
242         {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
243         {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
244         {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
245         {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
246         {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
247         {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
248         {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
249         {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
250 };
251
252 #define WALK_SPEED      0x40000
253 #define TURN_SPEED      0x200
254 #define ELEV_SPEED      8
255
256 static int update(void)
257 {
258         int32_t fwd[2], right[2];
259         int i, snum, ledspr;
260         struct enemy *enemy;
261
262         update_keyb();
263
264         if(KEYPRESS(BN_START)) {
265                 /* TODO pause menu */
266                 change_screen(find_screen("menu"));
267                 return -1;
268         }
269
270         if(keystate) {
271                 if(keystate & BN_LEFT) {
272                         angle += TURN_SPEED;
273                 }
274                 if(keystate & BN_RIGHT) {
275                         angle -= TURN_SPEED;
276                 }
277
278                 fwd[0] = -SIN(angle);
279                 fwd[1] = COS(angle);
280                 right[0] = fwd[1];
281                 right[1] = -fwd[0];
282
283                 if(keystate & BN_A) {
284                         pos[0] += fwd[0];
285                         pos[1] += fwd[1];
286                 }
287                 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
288                         emuprint("pew");
289                         last_shot = timer_msec;
290                         for(i=0; i<total_enemies; i++) {
291                                 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
292                                         int dx = enemies[i].vobj.px - 120;
293                                         int dy = enemies[i].vobj.py - 80;
294                                         if(abs(dx) < 10 && abs(dy) < 10) {
295                                                 enemies[i].hp--;
296                                                 break;
297                                         }
298                                 }
299                         }
300                 }
301                 if(keystate & BN_UP) {
302                         if(horizon > 40) horizon -= ELEV_SPEED;
303                 }
304                 if(keystate & BN_DOWN) {
305                         if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
306                 }
307                 if(keystate & BN_RT) {
308                         pos[0] += right[0];
309                         pos[1] += right[1];
310                 }
311                 if(keystate & BN_LT) {
312                         pos[0] -= right[0];
313                         pos[1] -= right[1];
314                 }
315
316                 vox_view(pos[0], pos[1], -40, angle);
317         }
318
319         snum = 0;
320         /* turrets number */
321         spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
322         spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
323         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
324         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
325         /* energy bar */
326         if(energy == MAX_ENERGY) {
327                 ledspr = SPRID_LEDBLU;
328         } else {
329                 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
330         }
331         for(i=0; i<5; i++) {
332                 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
333                                 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
334         }
335         /* enemy sprites */
336         /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
337         enemy = enemies;
338         for(i=0; i<total_enemies; i++) {
339                 int sid, anm, px, py;
340                 unsigned int flags;
341                 int16_t mat[4];
342                 int32_t sa, ca, scale;
343
344                 if(enemy->vobj.px >= 0) {
345                         flags = SPR_SZ32 | SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
346                         if(enemies->hp > 0) {
347                                 anm = (enemies->anm + (vblcount >> 3)) & 0xf;
348                                 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
349                                 flags |= SPR_VRECT;
350                         } else {
351                                 anm = 0;
352                                 sid = SPRID_HUSK;
353                         }
354
355                         px = enemy->vobj.px - 120;
356                         py = enemy->vobj.py - 80;
357                         xform_pixel(&px, &py);
358
359                         spr_oam(oam, dynspr_base + snum++, sid, px - 20, py - 32, flags);
360
361                         scale = enemy->vobj.scale;
362                         if(scale > 0x10000) scale = 0x10000;
363                         sa = xform_sa / scale;
364                         ca = xform_ca / scale;
365                         mat[0] = anm >= 8 ? -ca : ca;
366                         mat[1] = sa;
367                         mat[2] = -sa;
368                         mat[3] = ca;
369
370                         spr_transform(oam, 0, mat);
371                         enemy->vobj.px = -1;
372                 }
373                 enemy++;
374         }
375         for(i=snum; i<dynspr_count; i++) {
376                 spr_oam_clear(oam, dynspr_base + i);
377         }
378
379         mask(INTR_VBLANK);
380         dynspr_count = snum;
381         unmask(INTR_VBLANK);
382
383         return 0;
384 }
385
386 static void draw(void)
387 {
388         //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
389         fillblock_16byte(framebuf, 0, 240 * 160 / 16);
390
391         vox_render();
392         //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
393         //vox_sky_solid(COLOR_ZENITH);
394 }
395
396 static inline void xform_pixel(int *xp, int *yp)
397 {
398         int32_t sa = xform_sa >> 8;
399         int32_t ca = xform_ca >> 8;
400         int x = *xp;
401         int y = *yp;
402
403         *xp = (ca * x - sa * y + (120 << 8)) >> 8;
404         *yp = (sa * x + ca * y + (80 << 8)) >> 8;
405 }
406
407 #define MAXBANK         0x100
408
409 ARM_IWRAM
410 static void gamescr_vblank(void)
411 {
412         static int bank, bankdir, theta;
413         int32_t sa, ca;
414
415         vblcount++;
416
417         /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
418          * DMA them from cartridge easily
419          */
420
421         /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
422         dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
423
424         theta = -(bank << 3);
425         xform_sa = SIN(theta);
426         xform_ca = COS(theta);
427 #if 0
428         xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
429         sa = (((xform_sa) >> 8) * xform_s) >> 12;
430         ca = (((xform_ca) >> 8) * xform_s) >> 12;
431 #else
432         xform_s = (MAXBANK + (abs(bank) >> 3));
433         sa = xform_sa / xform_s;
434         ca = xform_ca / xform_s;
435 #endif
436
437         REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
438         REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
439
440         REG_BG2PA = ca;
441         REG_BG2PB = sa;
442         REG_BG2PC = -sa;
443         REG_BG2PD = ca;
444
445         if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
446                 if(bank) {
447                         bank -= bankdir << 4;
448                 }
449         } else if(keystate & BN_LEFT) {
450                 bankdir = -1;
451                 if(bank > -MAXBANK) bank -= 16;
452         } else if(keystate & BN_RIGHT) {
453                 bankdir = 1;
454                 if(bank < MAXBANK) bank += 16;
455         }
456 }