porting voxscape to gba
[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 "voxscape.h"
15 #include "data.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 int32_t pos[2], angle;
37 static struct voxscape *vox;
38
39 #define COLOR_HORIZON   192
40 #define COLOR_ZENITH    255
41
42
43
44 struct screen *init_game_screen(void)
45 {
46         return &gamescr;
47 }
48
49 static int gamescr_start(void)
50 {
51         int i;
52
53         gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
54
55         vblperf_setcolor(1);
56
57         pos[0] = pos[1] = 256 << 16;
58
59         if(!(vox = vox_create(512, 512, height_pixels, color_pixels))) {
60                 panic(get_pc(), "vox_create");
61         }
62         vox_proj(vox, 45, 1, 250);
63
64         /* setup color image palette */
65         for(i=0; i<192; i++) {
66                 int r = color_cmap[i * 3];
67                 int g = color_cmap[i * 3 + 1];
68                 int b = color_cmap[i * 3 + 2];
69                 gba_bgpal[i] = ((r << 8) & 0x7c00) | ((g << 2) & 0x3e0) | (b >> 3);
70         }
71
72         /* setup sky gradient palette */
73         for(i=0; i<64; i++) {
74                 int t = i << 8;
75                 int r = (0xcc00 + (0x55 - 0xcc) * t) >> 8;
76                 int g = (0x7700 + (0x88 - 0x77) * t) >> 8;
77                 int b = (0xff00 + (0xcc - 0xff) * t) >> 8;
78                 int cidx = COLOR_HORIZON + i;
79                 gba_bgpal[cidx] = ((r << 8) & 0x7c00) | ((g << 2) & 0x3e0) | (b >> 3);
80         }
81
82         /*select_input(BN_DPAD | BN_LT | BN_RT);*/
83
84         nframes = 0;
85         return 0;
86 }
87
88 static void gamescr_stop(void)
89 {
90 }
91
92 static void gamescr_frame(void)
93 {
94         unsigned char *fb;
95
96         backbuf = ++nframes & 1;
97         fb = (unsigned char*)vram[backbuf];
98
99         vox_framebuf(vox, 240, 160, fb, -1);
100
101         update();
102         draw();
103
104         vblperf_end();
105         wait_vblank();
106         present(backbuf);
107         vblperf_begin();
108 }
109
110 #define WALK_SPEED      0x40000
111 #define TURN_SPEED      0x100
112
113 static void update(void)
114 {
115         int32_t fwd[2], right[2];
116         uint16_t input;
117
118         input = read_input();
119
120         if(input & BN_LT) angle += TURN_SPEED;
121         if(input & BN_RT) angle -= TURN_SPEED;
122
123         fwd[0] = -SIN(angle);
124         fwd[1] = COS(angle);
125         right[0] = fwd[1];
126         right[1] = -fwd[0];
127
128         if(input & BN_UP) {
129                 pos[0] += fwd[0];
130                 pos[1] += fwd[1];
131         }
132         if(input & BN_DOWN) {
133                 pos[0] -= fwd[0];
134                 pos[1] -= fwd[1];
135         }
136         if(input & BN_RIGHT) {
137                 pos[0] += right[0];
138                 pos[1] += right[1];
139         }
140         if(input & BN_LEFT) {
141                 pos[0] -= right[0];
142                 pos[1] -= right[1];
143         }
144
145         vox_view(vox, pos[0], pos[1], -30, angle);
146 }
147
148 static void draw(void)
149 {
150 //      vox_begin(vox);
151         vox_render(vox);
152         vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
153         //vox_sky_solid(vox, COLOR_ZENITH);
154 }
155
156 #ifdef BUILD_GBA
157 __attribute__((noinline, target("arm"), section(".iwram")))
158 #endif
159 static void gamescr_vblank(void)
160 {
161         num_vbl++;
162 }