af2f6d32eeb2ce11fa085c6e38dd6144597b174d
[gba_blender] / src / main.c
1 /*
2 blender for the Gameboy Advance
3 Copyright (C) 2021  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdlib.h>
19 #include <string.h>
20 #include "gbaregs.h"
21 #include "timer.h"
22 #include "keyb.h"
23 #include "intr.h"
24 #include "gfx.h"
25 #include "xgl.h"
26 #include "polyfill.h"
27 #include "debug.h"
28 #include "meshdata.h"
29 #include "sprites.h"
30 #include "dma.h"
31
32 enum {
33         SIDX_DEL0, SIDX_DEL1, SIDX_DEL2,
34         SIDX_TIME,
35         SIDX_ICONS_BASE
36 };
37
38 enum {
39         SNAM_DEL0       = 512,
40         SNAM_DEL1       = SNAM_DEL0 + 8,
41         SNAM_DEL2       = SNAM_DEL1 + 8
42 };
43
44 #define MENU_HEIGHT             17
45 #define TRACK_HEIGHT    18
46 #define VP_HEIGHT               (160 - MENU_HEIGHT - TRACK_HEIGHT)
47
48 static void handle_keys(void);
49 static void show_msgbox(int en);
50
51 extern struct { unsigned char r, g, b; } bgimg_cmap[];
52 extern unsigned char bgimg_pixels[];
53
54 static int32_t cam_theta = 0x10000, cam_phi = -0x8000;
55
56 static int show_obj = 1, show_del;
57
58 #define AXIS0   0x10000
59 #define AXIS1   0x80000
60
61 static struct xvertex gridaxes[] = {
62         {AXIS0, 0, 0,   0, 0, 0, 92},
63         {AXIS1, 0, 0,   0, 0, 0, 92},
64         {-AXIS0, 0, 0,  0, 0, 0, 92},
65         {-AXIS1, 0, 0,  0, 0, 0, 92},
66         {0, 0, AXIS0,   0, 0, 0, 93},
67         {0, 0, AXIS1,   0, 0, 0, 93},
68         {0, 0, -AXIS0,  0, 0, 0, 93},
69         {0, 0, -AXIS1,  0, 0, 0, 93},
70
71         {0, 0, 0,               0, 0, 0, 92},
72         {AXIS1, 0, 0,   0, 0, 0, 92},
73         {0, 0, 0,               0, 0, 0, 92},
74         {-AXIS1, 0, 0,  0, 0, 0, 92},
75         {0, 0, 0,               0, 0, 0, 93},
76         {0, 0, AXIS1,   0, 0, 0, 93},
77         {0, 0, -0,              0, 0, 0, 93},
78         {0, 0, -AXIS1,  0, 0, 0, 93},
79 };
80
81 #define MAX_SIDX        8
82 static uint16_t oam[4 * MAX_SIDX];
83
84
85 int main(void)
86 {
87         int i;
88         unsigned int nframes = 0, backbuf;
89         uint16_t *cptr;
90         unsigned char r, g, b;
91         unsigned char *fbptr[2], *fb;
92
93         intr_init();
94         reset_msec_timer();
95         set_intr();
96
97         /* mode 4: 240x160 8bpp */
98         REG_DISPCNT = DISPCNT_BG2 | DISPCNT_OBJ | 4;
99
100         fbptr[0] = (unsigned char*)VRAM_LFB_FB0_ADDR;
101         fbptr[1] = (unsigned char*)VRAM_LFB_FB1_ADDR;
102
103         set_bg_color(0xff, 31, 31, 31);
104
105         cptr = (uint16_t*)CRAM_BG_ADDR;
106         for(i=0; i<128; i++) {
107                 r = bgimg_cmap[i].r >> 3;
108                 g = bgimg_cmap[i].g >> 3;
109                 b = bgimg_cmap[i].b >> 3;
110                 *cptr++ = r | (g << 5) | (b << 10);
111         }
112         for(i=0; i<128; i++) {
113                 r = i / 5 + 6;
114                 *cptr++ = r | (r << 5) | (r << 10);
115         }
116         dma_copy16(3, fbptr[0], bgimg_pixels, 240 * 160 / 2, 0);
117         dma_copy16(3, fbptr[1], bgimg_pixels, 240 * 160 / 2, 0);
118
119         init_sprites();
120
121         xgl_init();
122         xgl_viewport(0, 0, 240, VP_HEIGHT);
123         xgl_enable(XGL_LIGHTING);
124
125         key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);
126
127         /* every vblank, copy the shadow OAM automatically */
128         /*dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, DMACNT_VBLANK |
129                         DMACNT_REPEAT | DMACNT_INC_RELOAD);*/
130
131         for(;;) {
132                 handle_keys();
133
134                 backbuf = ++nframes & 1;
135
136                 fb = fbptr[backbuf] + 240 * MENU_HEIGHT;
137                 polyfill_framebuffer(fb, 240, VP_HEIGHT);
138                 //memset(fb, 14, 240 * VP_HEIGHT);
139                 dma_fill16(3, fb, 0x0e0e, 240 * VP_HEIGHT / 2);
140
141                 xgl_load_identity();
142                 xgl_translate(0, 0, 8 << 16);
143                 xgl_rotate_x(cam_phi);
144                 xgl_rotate_y(cam_theta);
145
146                 if(show_obj) {
147                         if(cam_theta < X_PI) {
148                                 xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
149                         } else {
150                                 xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
151                         }
152                         if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
153                                 xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
154                         } else {
155                                 xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
156                         }
157                         if(show_obj == 1) {
158                                 xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
159                         } else {
160                                 xgl_draw(XGL_TRIANGLES, suzanne, sizeof suzanne / sizeof *suzanne);
161                         }
162                         if(cam_theta < X_PI) {
163                                 xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
164                         } else {
165                                 xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
166                         }
167                         if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
168                                 xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
169                         } else {
170                                 xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
171                         }
172                 } else {
173                         xgl_draw(XGL_LINES, gridaxes + 8, 8);
174                 }
175
176                 wait_vblank();
177                 present(backbuf);
178                 dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
179         }
180
181         return 0;
182 }
183
184 static void handle_keys(void)
185 {
186         update_keyb();
187
188         if(KEYPRESS(KEY_UP)) {
189                 cam_phi += 0x2000;
190                 if(cam_phi > X_HPI) cam_phi = X_HPI;
191         }
192         if(KEYPRESS(KEY_DOWN)) {
193                 cam_phi -= 0x2000;
194                 if(cam_phi < -X_HPI) cam_phi = -X_HPI;
195         }
196         if(KEYPRESS(KEY_LEFT)) {
197                 cam_theta += 0x2000;
198                 if(cam_theta > X_2PI) cam_theta -= X_2PI;
199         }
200         if(KEYPRESS(KEY_RIGHT)) {
201                 cam_theta -= 0x2000;
202                 if(cam_theta < 0) cam_theta += X_2PI;
203         }
204         if(KEYPRESS(KEY_RT)) {
205                 if(++show_obj > 2) show_obj = 0;
206         }
207         if(KEYPRESS(KEY_LT)) {
208                 if(--show_obj < 0) show_obj = 2;
209         }
210
211         if(KEYPRESS(KEY_A)) {
212                 if(!show_del) {
213                         if(show_obj) show_del = 1;
214                 } else {
215                         show_del = 0;
216                 }
217                 show_msgbox(show_del);
218         }
219         if(KEYPRESS(KEY_B)) {
220                 if(show_del) {
221                         show_obj = 0;
222                         show_del = 0;
223                         show_msgbox(0);
224                 }
225         }
226 }
227
228 static void show_msgbox(int en)
229 {
230         int i;
231
232         if(en) {
233                 for(i=0; i<3; i++) {
234                         set_sprite(oam, SIDX_DEL0 + i, SNAM_DEL0 + i * 8, 42 + i * 64, 42, 0, SPR_SZ64);
235                 }
236         } else {
237                 for(i=0; i<3; i++) {
238                         set_sprite(oam, SIDX_DEL0 + i, 0, 0, 0, 0, 0);
239                 }
240         }
241 }