2vbl tunnel! 30fps
[gbajam21] / src / gamescr.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "gbaregs.h"
4 #include "dma.h"
5 #include "data.h"
6 #include "util.h"
7 #include "intr.h"
8 #include "debug.h"
9
10 static void draw_tunnel(void);
11
12 static int nframes, backbuf;
13 static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
14 static unsigned char *tex;
15
16 void gamescr(void)
17 {
18         int i;
19         uint16_t *cdst;
20         unsigned char *csrc;
21
22         REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_FB1;
23
24         vblperf_setcolor(0xff);
25
26         cdst = (uint16_t*)CRAM_BG_ADDR;
27         csrc = tuncross_cmap;
28         for(i=0; i<256; i++) {
29                 *cdst++ = CONV_RGB24_RGB15(csrc[0], csrc[1], csrc[2]);
30                 csrc += 3;
31         }
32
33         fillblock_16byte(vram[0], 0xffffffff, 240 * 160 / 16);
34         fillblock_16byte(vram[1], 0xffffffff, 240 * 160 / 16);
35
36         tex = iwram_sbrk(32 * 32);
37         memcpy(tex, tuncross_pixels, 32 * 32);
38
39         nframes = 0;
40         for(;;) {
41                 backbuf = ++nframes & 1;
42
43                 draw_tunnel();
44
45                 vblperf_end();
46                 wait_vblank();
47                 present(backbuf);
48                 vblperf_begin();
49         }
50 }
51
52 __attribute__((noinline, target("arm"), section(".iwram")))
53 static void draw_tunnel(void)
54 {
55         int i, j, tx, ty, angle, depth, zoffs;
56         uint16_t pptop, ppbot;
57         uint16_t *top, *bot;
58         uint32_t tun, *tunptr;
59
60         zoffs = nframes;
61
62         top = vram[backbuf];
63         bot = vram[backbuf] + 159 * 240 / 2;
64         tunptr = tunmap;
65         for(i=0; i<80; i++) {
66                 top++;
67                 bot++;
68                 tunptr++;
69                 for(j=1; j<240/2; j++) {
70                         tun = *tunptr++;
71
72                         angle = tun & 0xff;
73                         depth = (tun >> 8) & 0xff;
74                         tx = ((angle >> 1) + zoffs) & 0x1f;
75                         ty = ((depth >> 1) + zoffs) & 0x1f;
76                         pptop = tex[(ty << 5) + tx];
77                         tx = ((angle >> 1) - zoffs) & 0x1f;
78                         ppbot = tex[(ty << 5) + tx];
79
80                         angle = (tun >> 16) & 0xff;
81                         depth = (tun >> 24) & 0xff;
82                         tx = ((angle >> 1) + zoffs) & 0x1f;
83                         ty = ((depth >> 1) + zoffs) & 0x1f;
84                         pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
85                         tx = ((angle >> 1) - zoffs) & 0x1f;
86                         ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
87
88                         *top++ = pptop;
89                         *bot++ = ppbot;
90                 }
91                 bot -= 240;
92         }
93 }