mirroring tunnel vertically improves performance
[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 unsigned char tex[32 * 32] __attribute__((section(".iwram")));
11
12 void gamescr(void)
13 {
14         int i, j, tx, ty, angle, depth, nframes, backbuf, zoffs;
15         static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
16         uint16_t *cdst, *top, *bot;
17         unsigned char *csrc;
18         uint32_t tun, *tunptr;
19
20         REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_FB1;
21
22         vblperf_start(0xff);
23
24         cdst = (uint16_t*)CRAM_BG_ADDR;
25         csrc = tuncross_cmap;
26         for(i=0; i<256; i++) {
27                 *cdst++ = CONV_RGB24_RGB15(csrc[0], csrc[1], csrc[2]);
28                 csrc += 3;
29         }
30
31         fillblock_16byte(vram[0], 0xffffffff, 240 * 160 / 16);
32         fillblock_16byte(vram[1], 0xffffffff, 240 * 160 / 16);
33
34         memcpy(tex, tuncross_pixels, 32 * 32);
35
36         nframes = 0;
37         for(;;) {
38                 backbuf = ++nframes & 1;
39
40                 zoffs = nframes << 1;
41
42                 top = vram[backbuf];
43                 bot = vram[backbuf] + 159 * 240 / 2;
44                 tunptr = tunmap;
45                 for(i=0; i<80; i++) {
46                         for(j=0; j<240/2; j++) {
47                                 uint16_t pptop, ppbot;
48
49                                 tun = *tunptr++;
50
51                                 angle = tun & 0xff;
52                                 depth = (tun >> 8) & 0xff;
53                                 tx = ((angle >> 1) + zoffs) & 0x1f;
54                                 ty = ((depth >> 1) + zoffs) & 0x1f;
55                                 pptop = tex[(ty << 5) + tx];
56                                 tx = ((angle >> 1) - zoffs) & 0x1f;
57                                 ppbot = tex[(ty << 5) + tx];
58
59                                 angle = (tun >> 16) & 0xff;
60                                 depth = (tun >> 24) & 0xff;
61                                 tx = ((angle >> 1) + zoffs) & 0x1f;
62                                 ty = ((depth >> 1) + zoffs) & 0x1f;
63                                 pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
64                                 tx = ((angle >> 1) - zoffs) & 0x1f;
65                                 ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
66
67                                 *top++ = pptop;
68                                 *bot++ = ppbot;
69                         }
70                         bot -= 240;
71                 }
72
73                 vblperf_end();
74                 wait_vblank();
75                 present(backbuf);
76                 vblperf_begin();
77         }
78 }