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