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