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         unsigned char *fb;
53
54         REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
55
56         vblperf_setcolor(0xff);
57
58         lvl = init_level(testlvl);
59
60         xgl_init();
61
62         memset(&player, 0, sizeof player);
63         player.y = 0x60000;
64
65         select_input(BN_DPAD | BN_A | BN_B);
66
67         mask(INTR_VBLANK);
68         screen_vblank = vblank;
69         unmask(INTR_VBLANK);
70
71         nframes = 0;
72         for(;;) {
73                 backbuf = ++nframes & 1;
74                 fb = (unsigned char*)vram[backbuf];
75
76                 polyfill_framebuffer(fb, 240, 160);
77                 fillblock_16byte(fb, 0, 240 * 160 / 16);
78
79                 update();
80                 draw();
81
82                 vblperf_end();
83                 wait_vblank();
84                 present(backbuf);
85                 vblperf_begin();
86         }
87 }
88
89 static void update(void)
90 {
91         uint16_t bnstate;
92
93         bnstate = get_input();
94
95         player_input(&player, bnstate);
96 }
97
98 static void draw(void)
99 {
100         xgl_load_identity();
101         xgl_rotate_x(player.phi);
102         xgl_rotate_y(player.theta);
103         xgl_translate(player.x, 0, player.y);
104         xgl_rotate_y(X_QPI);
105
106         xgl_draw(XGL_QUADS, tm_floor, sizeof tm_floor / sizeof *tm_floor);
107 }
108
109 __attribute__((noinline, target("arm"), section(".iwram")))
110 static void vblank(void)
111 {
112         num_vbl++;
113 }