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