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