voxelscape port
[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
15 static int gamescr_start(void);
16 static void gamescr_stop(void);
17 static void gamescr_frame(void);
18 static void gamescr_vblank(void);
19
20 static void update(void);
21 static void draw(void);
22
23 static struct screen gamescr = {
24         "game",
25         gamescr_start,
26         gamescr_stop,
27         gamescr_frame,
28         gamescr_vblank
29 };
30
31 static int nframes, num_vbl, backbuf;
32 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
33
34 static int32_t pos[2], angle;
35 static struct voxscape *vox;
36
37 #define COLOR_HORIZON   0xcc77ff
38 #define COLOR_ZENITH    0x5588cc
39
40
41
42 struct screen *init_game_screen(void)
43 {
44         return &gamescr;
45 }
46
47 static int gamescr_start(void)
48 {
49         gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
50
51         vblperf_setcolor(1);
52
53         if(!(vox = vox_create(512, 512))) {
54                 panic(get_pc(), "vox_create");
55         }
56         vox_proj(vox, 45, 1, 250);
57
58         /*select_input(BN_DPAD | BN_LT | BN_RT);*/
59
60         nframes = 0;
61         return 0;
62 }
63
64 static void gamescr_stop(void)
65 {
66 }
67
68 static void gamescr_frame(void)
69 {
70         unsigned char *fb;
71
72         backbuf = ++nframes & 1;
73         fb = (unsigned char*)vram[backbuf];
74
75         vox_framebuf(vox, 240, 160, fb, -1);
76
77         update();
78         draw();
79
80         vblperf_end();
81         wait_vblank();
82         present(backbuf);
83         vblperf_begin();
84 }
85
86 #define WALK_SPEED      0x40000
87 #define TURN_SPEED      0x100
88
89 static void update(void)
90 {
91         int32_t fwd[2], right[2];
92         uint16_t input;
93
94         input = ~REG_KEYINPUT;
95
96         if(input & BN_LT) angle += TURN_SPEED;
97         if(input & BN_RT) angle -= TURN_SPEED;
98
99         fwd[0] = -SIN(angle);
100         fwd[1] = COS(angle);
101         right[0] = fwd[1];
102         right[1] = -fwd[0];
103
104         if(input & BN_UP) {
105                 pos[0] += fwd[0];
106                 pos[1] += fwd[1];
107         }
108         if(input & BN_DOWN) {
109                 pos[0] -= fwd[0];
110                 pos[1] -= fwd[1];
111         }
112         if(input & BN_RIGHT) {
113                 pos[0] += right[0];
114                 pos[1] += right[1];
115         }
116         if(input & BN_LEFT) {
117                 pos[0] -= right[0];
118                 pos[1] -= right[1];
119         }
120
121         vox_view(vox, pos[0], pos[1], -30, angle);
122 }
123
124 static void draw(void)
125 {
126         vox_render(vox);
127         vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
128 }
129
130 #ifdef BUILD_GBA
131 __attribute__((noinline, target("arm"), section(".iwram")))
132 #endif
133 static void gamescr_vblank(void)
134 {
135         num_vbl++;
136 }