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