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