0e5c6f833e1c8587ea028bb12c898e13f1e90792
[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(0);
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, 10, 100);
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] = (((uint16_t)b << 7) & 0x7c00) | (((uint16_t)g << 2) & 0x3e0) | (((uint16_t)r >> 3) & 0x1f);
70         }
71
72         /* setup sky gradient palette */
73         for(i=0; i<64; i++) {
74                 int t = (i << 8) / 64;
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] = ((b << 7) & 0x7c00) | ((g << 2) & 0x3e0) | (r >> 3);
80         }
81
82         nframes = 0;
83         return 0;
84 }
85
86 static void gamescr_stop(void)
87 {
88 }
89
90 static void gamescr_frame(void)
91 {
92         unsigned char *fb;
93
94         backbuf = ++nframes & 1;
95         fb = (unsigned char*)vram[backbuf];
96
97         vox_framebuf(vox, 240, 160, fb, -1);
98
99         update();
100         draw();
101
102         vblperf_end();
103         wait_vblank();
104         present(backbuf);
105         vblperf_begin();
106 }
107
108 #define WALK_SPEED      0x40000
109 #define TURN_SPEED      0x100
110
111 static void update(void)
112 {
113         int32_t fwd[2], right[2];
114         uint16_t input;
115
116         if((input = read_input())) {
117
118                 if(input & BN_LT) angle += TURN_SPEED;
119                 if(input & BN_RT) angle -= TURN_SPEED;
120
121                 fwd[0] = -SIN(angle);
122                 fwd[1] = COS(angle);
123                 right[0] = fwd[1];
124                 right[1] = -fwd[0];
125
126                 if(input & BN_UP) {
127                         pos[0] += fwd[0];
128                         pos[1] += fwd[1];
129                 }
130                 if(input & BN_DOWN) {
131                         pos[0] -= fwd[0];
132                         pos[1] -= fwd[1];
133                 }
134                 if(input & BN_RIGHT) {
135                         pos[0] += right[0];
136                         pos[1] += right[1];
137                 }
138                 if(input & BN_LEFT) {
139                         pos[0] -= right[0];
140                         pos[1] -= right[1];
141                 }
142
143                 vox_view(vox, pos[0], pos[1], -30, angle);
144         }
145 }
146
147 static void draw(void)
148 {
149         vox_render(vox);
150         //vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
151         vox_sky_solid(vox, COLOR_ZENITH);
152 }
153
154 #ifdef BUILD_GBA
155 __attribute__((noinline, target("arm"), section(".iwram")))
156 #endif
157 static void gamescr_vblank(void)
158 {
159         num_vbl++;
160 }