bg rotation
[gbajam21] / 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 "data.h"
7 #include "util.h"
8 #include "intr.h"
9 #include "input.h"
10 #include "sprite.h"
11 #include "debug.h"
12
13 static void draw_tunnel(void);
14 static void vblank(void);
15
16 static int nframes, backbuf;
17 static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
18 static unsigned char *tex;
19 static uint16_t bnstate;
20
21 #define MAX_SPR         4
22 static uint16_t oam[4 * MAX_SPR];
23
24 static short x = 120, y = 80;
25 static unsigned char rot;
26
27 void gamescr(void)
28 {
29         int i;
30         uint16_t *cdst;
31         unsigned char *csrc;
32
33         REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
34
35         vblperf_setcolor(0xff);
36
37         /* sprite setup */
38         spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
39
40         wait_vblank();
41         spr_clear();
42         spr_oam_clear(oam, 0);
43         spr_oam_clear(oam, 1);
44         spr_oam_clear(oam, 2);
45         spr_oam_clear(oam, 3);
46
47         cdst = (uint16_t*)CRAM_BG_ADDR;
48         csrc = tuncross_cmap;
49         for(i=0; i<256; i++) {
50                 *cdst++ = CONV_RGB24_RGB15(csrc[0], csrc[1], csrc[2]);
51                 csrc += 3;
52         }
53
54         fillblock_16byte(vram[0], 0xffffffff, 240 * 160 / 16);
55         fillblock_16byte(vram[1], 0xffffffff, 240 * 160 / 16);
56
57         tex = iwram_sbrk(32 * 32);
58         memcpy(tex, tuncross_pixels, 32 * 32);
59
60         /*select_input(BN_DPAD);*/
61
62         mask(INTR_VBLANK);
63         screen_vblank = vblank;
64         unmask(INTR_VBLANK);
65
66         nframes = 0;
67         for(;;) {
68                 backbuf = ++nframes & 1;
69
70                 bnstate = ~REG_KEYINPUT;
71
72                 draw_tunnel();
73
74                 vblperf_end();
75                 wait_vblank();
76                 present(backbuf);
77                 vblperf_begin();
78         }
79 }
80
81 static int32_t bgmat[4];
82 static int32_t bgx, bgy;
83
84 __attribute__((noinline, target("arm"), section(".iwram")))
85 static void draw_tunnel(void)
86 {
87         static short uoffs;
88         int i, j, tx, ty, angle, depth, zoffs;
89         uint16_t pptop, ppbot;
90         uint16_t *top, *bot;
91         uint32_t tun, *tunptr;
92
93         zoffs = nframes;
94
95         if(bnstate & BN_LT) uoffs++;
96         if(bnstate & BN_RT) uoffs--;
97
98         bgmat[0] = COS(uoffs) << 1;
99         bgmat[1] = -SIN(uoffs) << 1;
100         bgmat[2] = SIN(uoffs) << 1;
101         bgmat[3] = COS(uoffs) << 1;
102         bgx = (120 << 8) - bgmat[0] * 120 + bgmat[1] * -80;
103         bgy = (80 << 8) - bgmat[2] * 120 + bgmat[3] * -80;
104
105         top = vram[backbuf];
106         bot = vram[backbuf] + 159 * 240 / 2;
107         tunptr = tunmap;
108         for(i=0; i<80; i++) {
109 #ifdef VBLBAR
110                 top++;
111                 bot++;
112                 tunptr++;
113
114                 for(j=1; j<240/2; j++) {
115 #else
116                 for(j=0; j<240/2; j++) {
117 #endif
118                         tun = *tunptr++;
119
120                         angle = tun & 0xff;
121                         depth = (tun >> 8) & 0xff;
122 //                      tx = ((angle >> 1) - uoffs) & 0x1f;
123                         tx = (angle >> 1) & 0x1f;
124                         ty = ((depth >> 1) + zoffs) & 0x1f;
125                         pptop = tex[(ty << 5) + tx];
126 //                      tx = ~((angle >> 1) + uoffs) & 0x1f;
127                         tx = ~(angle >> 1) & 0x1f;
128                         ppbot = tex[(ty << 5) + tx];
129
130                         angle = (tun >> 16) & 0xff;
131                         depth = (tun >> 24) & 0xff;
132 //                      tx = ((angle >> 1) - uoffs) & 0x1f;
133                         tx = (angle >> 1) & 0x1f;
134                         ty = ((depth >> 1) + zoffs) & 0x1f;
135                         pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
136 //                      tx = ~((angle >> 1) + uoffs) & 0x1f;
137                         tx = ~(angle >> 1) & 0x1f;
138                         ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
139
140                         *top++ = pptop;
141                         *bot++ = ppbot;
142                 }
143                 bot -= 240;
144         }
145 }
146
147 __attribute__((noinline, target("arm"), section(".iwram")))
148 static void vblank(void)
149 {
150         uint16_t bnstate;
151         int16_t mat[4];
152         static short gate_speed;
153
154         REG_BG2PA = bgmat[0];
155         REG_BG2PB = bgmat[1];
156         REG_BG2PC = bgmat[2];
157         REG_BG2PD = bgmat[3];
158         REG_BG2X = bgx;
159         REG_BG2Y = bgy;
160
161         bnstate = ~REG_KEYINPUT;
162         if(bnstate & BN_DPAD) {
163                 if(gate_speed < 5) {
164                         gate_speed++;
165                 }
166
167                 if(bnstate & BN_LEFT) x -= gate_speed;
168                 if(bnstate & BN_RIGHT) x += gate_speed;
169                 if(bnstate & BN_UP) y -= gate_speed;
170                 if(bnstate & BN_DOWN) y += gate_speed;
171
172                 if(x < 0) x = 0;
173                 if(x > 239) x = 239;
174                 if(y < 0) y = 0;
175                 if(y > 159) y = 159;
176         } else {
177                 gate_speed = 0;
178         }
179
180         if(bnstate & BN_A) rot -= 2;
181         if(bnstate & BN_B) rot += 2;
182
183         spr_oam(oam, 0, 512 + 256, x - 64, y - 64, SPR_256COL | SPR_SZ64 | SPR_DBLSZ |
184                         SPR_ROTSCL | SPR_ROTSCL_SEL(0));
185
186         mat[0] = COS(rot) << 1;
187         mat[1] = -SIN(rot) << 1;
188         mat[2] = SIN(rot) << 1;
189         mat[3] = COS(rot) << 1;
190         spr_transform(oam, 0, mat);
191
192
193         dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
194 }