lower res, still slow
[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] = VOX_SZ << 15;
58
59         if(!(vox = vox_create(VOX_SZ, VOX_SZ, height_pixels, color_pixels))) {
60                 panic(get_pc(), "vox_create");
61         }
62         vox_proj(vox, 45, 2, VOX_SZ / 5);
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
106         if(!(nframes & 15)) {
107                 emuprint("vbl: %d", vblperf_count);
108         }
109         vblperf_begin();
110 }
111
112 #define WALK_SPEED      0x40000
113 #define TURN_SPEED      0x100
114
115 static void update(void)
116 {
117         int32_t fwd[2], right[2];
118         uint16_t input;
119
120         if((input = read_input())) {
121
122                 if(input & BN_LT) angle += TURN_SPEED;
123                 if(input & BN_RT) angle -= TURN_SPEED;
124
125                 fwd[0] = -SIN(angle);
126                 fwd[1] = COS(angle);
127                 right[0] = fwd[1];
128                 right[1] = -fwd[0];
129
130                 if(input & BN_UP) {
131                         pos[0] += fwd[0];
132                         pos[1] += fwd[1];
133                 }
134                 if(input & BN_DOWN) {
135                         pos[0] -= fwd[0];
136                         pos[1] -= fwd[1];
137                 }
138                 if(input & BN_RIGHT) {
139                         pos[0] += right[0];
140                         pos[1] += right[1];
141                 }
142                 if(input & BN_LEFT) {
143                         pos[0] -= right[0];
144                         pos[1] -= right[1];
145                 }
146
147                 vox_view(vox, pos[0], pos[1], -30, angle);
148         }
149 }
150
151 static void draw(void)
152 {
153         vox_render(vox);
154         //vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
155         vox_sky_solid(vox, COLOR_ZENITH);
156 }
157
158 ARM_IWRAM
159 static void gamescr_vblank(void)
160 {
161         num_vbl++;
162 }