3b15a4a50f0f508658088c1ffceec8af8c25baae
[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 "sprite.h"
10 #include "debug.h"
11 #include "level.h"
12 #include "xgl.h"
13 #include "polyfill.h"
14
15 static void update(void);
16 static void draw(void);
17 static void vblank(void);
18
19 static int nframes, num_vbl, backbuf;
20 static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
21
22 static const char *testlvl =
23         "########\n"
24         "###   s#\n"
25         "### ####\n"
26         "###    #\n"
27         "##     #\n"
28         "##     #\n"
29         "##     #\n"
30         "## ### #\n"
31         "## ### #\n"
32         "##     #\n"
33         "#### ###\n"
34         "########\n";
35
36 static struct xvertex cube[] __attribute__((section(".rodata"))) = {
37         /* front */
38         {-0x10000, -0x10000, -0x10000,  0, 0, -0x10000, 255},
39         {0x10000, -0x10000, -0x10000,   0, 0, -0x10000, 255},
40         {0x10000, 0x10000, -0x10000,    0, 0, -0x10000, 255},
41         {-0x10000, 0x10000, -0x10000,   0, 0, -0x10000, 255},
42         /* right */
43         {0x10000, -0x10000, -0x10000,   0x10000, 0, 0,  128},
44         {0x10000, -0x10000, 0x10000,    0x10000, 0, 0,  128},
45         {0x10000, 0x10000, 0x10000,             0x10000, 0, 0,  128},
46         {0x10000, 0x10000, -0x10000,    0x10000, 0, 0,  128},
47         /* back */
48         {0x10000, -0x10000, 0x10000,    0, 0, 0x10000,  200},
49         {-0x10000, -0x10000, 0x10000,   0, 0, 0x10000,  200},
50         {-0x10000, 0x10000, 0x10000,    0, 0, 0x10000,  200},
51         {0x10000, 0x10000, 0x10000,             0, 0, 0x10000,  200},
52         /* left */
53         {-0x10000, -0x10000, 0x10000,   -0x10000, 0, 0, 192},
54         {-0x10000, -0x10000, -0x10000,  -0x10000, 0, 0, 192},
55         {-0x10000, 0x10000, -0x10000,   -0x10000, 0, 0, 192},
56         {-0x10000, 0x10000, 0x10000,    -0x10000, 0, 0, 192},
57         /* top */
58         {-0x10000, 0x10000, -0x10000,   0, 0x10000, 0,  150},
59         {0x10000, 0x10000, -0x10000,    0, 0x10000, 0,  150},
60         {0x10000, 0x10000, 0x10000,             0, 0x10000, 0,  150},
61         {-0x10000, 0x10000, 0x10000,    0, 0x10000, 0,  150},
62         /* bottom */
63         {0x10000, -0x10000, -0x10000,   0, -0x10000, 0, 210},
64         {-0x10000, -0x10000, -0x10000,  0, -0x10000, 0, 210},
65         {-0x10000, -0x10000, 0x10000,   0, -0x10000, 0, 210},
66         {0x10000, -0x10000, 0x10000,    0, -0x10000, 0, 210}
67 };
68
69
70 static struct level *lvl;
71
72 static int32_t cam_theta, cam_phi;
73
74 void gamescr(void)
75 {
76         unsigned char *fb;
77
78         REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
79
80         vblperf_setcolor(0xff);
81
82         lvl = init_level(testlvl);
83
84         xgl_init();
85
86         select_input(BN_DPAD);
87
88         mask(INTR_VBLANK);
89         screen_vblank = vblank;
90         unmask(INTR_VBLANK);
91
92         nframes = 0;
93         for(;;) {
94                 backbuf = ++nframes & 1;
95                 fb = (unsigned char*)vram[backbuf];
96
97                 polyfill_framebuffer(fb, 240, 160);
98                 fillblock_16byte(fb, 0, 240 * 160 / 16);
99
100                 update();
101                 draw();
102
103                 vblperf_end();
104                 wait_vblank();
105                 present(backbuf);
106                 vblperf_begin();
107         }
108 }
109
110 static void update(void)
111 {
112         uint16_t bnstate;
113
114         bnstate = get_input();
115
116         if(bnstate & KEY_UP) {
117                 cam_phi += 0x2000;
118                 if(cam_phi > X_HPI) cam_phi = X_HPI;
119         }
120         if(bnstate & KEY_DOWN) {
121                 cam_phi -= 0x2000;
122                 if(cam_phi < -X_HPI) cam_phi = -X_HPI;
123         }
124         if(bnstate & KEY_LEFT) {
125                 cam_theta += 0x2000;
126                 if(cam_theta > X_2PI) cam_theta -= X_2PI;
127         }
128         if(bnstate & KEY_RIGHT) {
129                 cam_theta -= 0x2000;
130                 if(cam_theta < X_2PI) cam_theta += X_2PI;
131         }
132 }
133
134 static void draw(void)
135 {
136         xgl_load_identity();
137         //xgl_translate(0, -0x50000, 0);
138         xgl_translate(0, 0, 0x80000);
139         xgl_rotate_x(cam_phi);
140         xgl_rotate_y(cam_theta);
141
142         xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
143 }
144
145 __attribute__((noinline, target("arm"), section(".iwram")))
146 static void vblank(void)
147 {
148         num_vbl++;
149 }