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