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