foo
[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 "player.h"
10 #include "sprite.h"
11 #include "debug.h"
12 #include "level.h"
13 #include "xgl.h"
14 #include "polyfill.h"
15
16 static void update(void);
17 static void draw(void);
18 static void vblank(void);
19
20 static int nframes, num_vbl, backbuf;
21 static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
22
23 static const char *testlvl =
24         "########\n"
25         "###   s#\n"
26         "### ####\n"
27         "###    #\n"
28         "##     #\n"
29         "##     #\n"
30         "##     #\n"
31         "## ### #\n"
32         "## ### #\n"
33         "##     #\n"
34         "#### ###\n"
35         "########\n";
36
37 static struct xvertex tm_floor[] __attribute__((section(".rodata"))) = {
38         {0x10000, -0x10000, 0x10000,    0, 0x10000, 0,  210},
39         {-0x10000, -0x10000, 0x10000,   0, 0x10000, 0,  210},
40         {-0x10000, -0x10000, -0x10000,  0, 0x10000, 0,  210},
41         {0x10000, -0x10000, -0x10000,   0, 0x10000, 0,  210}
42 };
43
44
45 static struct level *lvl;
46
47 static struct player player;
48
49
50 void gamescr(void)
51 {
52         int i;
53         unsigned char *fb;
54         uint16_t *cmap;
55
56         REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
57
58         vblperf_setcolor(1);
59
60         lvl = init_level(testlvl);
61
62         xgl_init();
63
64         memset(&player, 0, sizeof player);
65         player.phi = 0x100;
66
67         cmap = (uint16_t*)CRAM_BG_ADDR;
68         *cmap++ = 0;
69         for(i=1; i<255; i++) {
70                 *cmap++ = rand();
71         }
72         *cmap = 0xffff;
73
74
75         select_input(BN_DPAD | BN_A | BN_B);
76
77         mask(INTR_VBLANK);
78         screen_vblank = vblank;
79         unmask(INTR_VBLANK);
80
81         nframes = 0;
82         for(;;) {
83                 backbuf = ++nframes & 1;
84                 fb = (unsigned char*)vram[backbuf];
85
86                 polyfill_framebuffer(fb, 240, 160);
87                 fillblock_16byte(fb, 0, 240 * 160 / 16);
88
89                 update();
90                 draw();
91
92                 vblperf_end();
93                 wait_vblank();
94                 present(backbuf);
95                 vblperf_begin();
96         }
97 }
98
99 static void update(void)
100 {
101         uint16_t bnstate;
102
103         bnstate = get_input();
104
105         player_input(&player, bnstate);
106
107         upd_vis(lvl, &player);
108 }
109
110 static void draw(void)
111 {
112         int i, x, y;
113         struct cell *cell;
114
115         xgl_load_identity();
116         xgl_translate(0, 0, 0x100000);
117         xgl_rotate_x(player.phi);
118         xgl_rotate_y(player.theta);
119         xgl_translate(player.x, 0, player.y);
120
121         for(i=0; i<lvl->numvis; i++) {
122                 cell = lvl->vis[i];
123
124                 x = (int32_t)(cell->x - player.cx) << 17;
125                 y = -(int32_t)(cell->y - player.cy) << 17;
126
127                 xgl_push_matrix();
128                 xgl_translate(x, 0, y);
129                 xgl_index(i + 1);
130                 xgl_draw(XGL_QUADS, tm_floor, sizeof tm_floor / sizeof *tm_floor);
131                 xgl_pop_matrix();
132         }
133 }
134
135 __attribute__((noinline, target("arm"), section(".iwram")))
136 static void vblank(void)
137 {
138         num_vbl++;
139 }