converting to 16bpp
[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(5, 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         nframes = 0;
65         return 0;
66 }
67
68 static void gamescr_stop(void)
69 {
70 }
71
72 static void gamescr_frame(void)
73 {
74         unsigned char *fb;
75
76         backbuf = ++nframes & 1;
77         fb = (unsigned char*)vram[backbuf];
78
79         vox_framebuf(vox, 160, 128, fb, -1);
80
81         update();
82         draw();
83
84         vblperf_end();
85         wait_vblank();
86         present(backbuf);
87
88         if(!(nframes & 15)) {
89                 emuprint("vbl: %d", vblperf_count);
90         }
91         vblperf_begin();
92 }
93
94 #define WALK_SPEED      0x40000
95 #define TURN_SPEED      0x100
96
97 static void update(void)
98 {
99         int32_t fwd[2], right[2];
100         uint16_t input;
101
102         if((input = read_input())) {
103
104                 if(input & BN_LT) angle += TURN_SPEED;
105                 if(input & BN_RT) angle -= TURN_SPEED;
106
107                 fwd[0] = -SIN(angle);
108                 fwd[1] = COS(angle);
109                 right[0] = fwd[1];
110                 right[1] = -fwd[0];
111
112                 if(input & BN_UP) {
113                         pos[0] += fwd[0];
114                         pos[1] += fwd[1];
115                 }
116                 if(input & BN_DOWN) {
117                         pos[0] -= fwd[0];
118                         pos[1] -= fwd[1];
119                 }
120                 if(input & BN_RIGHT) {
121                         pos[0] += right[0];
122                         pos[1] += right[1];
123                 }
124                 if(input & BN_LEFT) {
125                         pos[0] -= right[0];
126                         pos[1] -= right[1];
127                 }
128
129                 vox_view(vox, pos[0], pos[1], -30, angle);
130         }
131 }
132
133 static void draw(void)
134 {
135         vox_render(vox);
136         //vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
137         vox_sky_solid(vox, COLOR_ZENITH);
138 }
139
140 ARM_IWRAM
141 static void gamescr_vblank(void)
142 {
143         num_vbl++;
144 }