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