axis gizmo
[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_ICON_ZOOM,
36         SIDX_ICON_PAN,
37         SIDX_ICON_ORBIT,
38         SIDX_ICON_X,
39         SIDX_ICON_Y,
40         SIDX_ICON_Z,
41         SIDX_DIRTY,
42 };
43
44 #define SIDX_ICONS_BASE         SIDX_ICON_ZOOM
45
46 #define SNAM_START      512
47 enum {
48         SNAM_DEL0               = SNAM_START,
49         SNAM_DEL1               = SNAM_DEL0 + 8,
50         SNAM_DEL2               = SNAM_DEL1 + 8,
51         SNAM_ICON_ZOOM  = SNAM_START + 24,
52         SNAM_ICON_PAN   = SNAM_ICON_ZOOM + 4,
53         SNAM_ICON_ORBIT = SNAM_START + 32 * 4 + 24,     /* for tiles down, 24 across */
54         SNAM_ICON_X             = SNAM_START + 32 * 8 + 26,
55         SNAM_ICON_Y             = SNAM_ICON_X + 2,
56         SNAM_ICON_Z             = SNAM_ICON_Y + 2,
57         SNAM_DIRTY              = SNAM_ICON_ORBIT + 4
58 };
59
60 #define MENU_HEIGHT             17
61 #define TRACK_HEIGHT    18
62 #define VP_HEIGHT               (160 - MENU_HEIGHT - TRACK_HEIGHT)
63
64 static void handle_keys(void);
65 static void upd_rotation(void);
66 static void show_msgbox(int en);
67
68 extern struct { unsigned char r, g, b; } bgimg_cmap[];
69 extern unsigned char bgimg_pixels[];
70
71 static int32_t cam_theta = 0x10000, cam_phi = -0x8000;
72
73 static int show_obj = 1, show_del;
74 static int32_t rot_matrix[16];
75
76 #define AXIS0   0x10000
77 #define AXIS1   0x80000
78 #define AXIS3   0x2c000
79
80 static struct xvertex gridaxes[] = {
81         {AXIS0, 0, 0,   0, 0, 0, 92},
82         {AXIS1, 0, 0,   0, 0, 0, 92},
83         {-AXIS0, 0, 0,  0, 0, 0, 92},
84         {-AXIS1, 0, 0,  0, 0, 0, 92},
85         {0, 0, AXIS0,   0, 0, 0, 93},
86         {0, 0, AXIS1,   0, 0, 0, 93},
87         {0, 0, -AXIS0,  0, 0, 0, 93},
88         {0, 0, -AXIS1,  0, 0, 0, 93},
89
90         {0, 0, 0,               0, 0, 0, 92},
91         {AXIS1, 0, 0,   0, 0, 0, 92},
92         {0, 0, 0,               0, 0, 0, 92},
93         {-AXIS1, 0, 0,  0, 0, 0, 92},
94         {0, 0, 0,               0, 0, 0, 93},
95         {0, 0, AXIS1,   0, 0, 0, 93},
96         {0, 0, -0,              0, 0, 0, 93},
97         {0, 0, -AXIS1,  0, 0, 0, 93},
98 };
99
100 static struct xvertex small_axes[] = {
101         {0, 0, 0,               0, 0, 0, 92},
102         {AXIS3, 0, 0,   0, 0, 0, 92},
103         {0, 0, 0,               0, 0, 0, 93},
104         {0, 0, AXIS3,   0, 0, 0, 93},
105         {0, 0, 0,               0, 0, 0, 94},
106         {0, AXIS3, 0,   0, 0, 0, 94}
107 };
108
109 #define MAX_SIDX        16
110 static uint16_t oam[4 * MAX_SIDX];
111
112
113 int main(void)
114 {
115         int i;
116         int x, y;
117         unsigned int nframes = 0, backbuf;
118         uint16_t *cptr;
119         unsigned char r, g, b;
120         unsigned char *fbptr[2], *fb;
121
122         intr_init();
123         reset_msec_timer();
124         set_intr();
125
126         /* mode 4: 240x160 8bpp */
127         REG_DISPCNT = DISPCNT_BG2 | DISPCNT_OBJ | 4;
128
129         fbptr[0] = (unsigned char*)VRAM_LFB_FB0_ADDR;
130         fbptr[1] = (unsigned char*)VRAM_LFB_FB1_ADDR;
131
132         set_bg_color(0xff, 31, 31, 31);
133
134         cptr = (uint16_t*)CRAM_BG_ADDR;
135         for(i=0; i<128; i++) {
136                 r = bgimg_cmap[i].r >> 3;
137                 g = bgimg_cmap[i].g >> 3;
138                 b = bgimg_cmap[i].b >> 3;
139                 *cptr++ = r | (g << 5) | (b << 10);
140         }
141         for(i=0; i<128; i++) {
142                 r = i / 5 + 6;
143                 *cptr++ = r | (r << 5) | (r << 10);
144         }
145         dma_copy16(3, fbptr[0], bgimg_pixels, 240 * 160 / 2, 0);
146         dma_copy16(3, fbptr[1], bgimg_pixels, 240 * 160 / 2, 0);
147
148         init_sprites();
149         REG_BLDCNT = BLDCNT_ALPHA | BLDCNT_B_BG2;
150         REG_BLDALPHA = 0x040c;
151
152         set_sprite(oam, SIDX_ICONS_BASE, SNAM_ICON_ZOOM, 213, 57, 4, SPR_SZ32 | SPR_BLEND);
153         set_sprite(oam, SIDX_ICONS_BASE + 1, SNAM_ICON_PAN, 213, 81, 4, SPR_SZ32 | SPR_BLEND);
154         set_sprite(oam, SIDX_ICONS_BASE + 2, SNAM_ICON_ORBIT, 213, 103, 4, SPR_SZ32 | SPR_BLEND);
155         set_sprite(oam, SIDX_DIRTY, SNAM_DIRTY, 192, 9, 4, SPR_SZ16 | SPR_HRECT);
156
157         xgl_init();
158         xgl_enable(XGL_LIGHTING);
159
160         upd_rotation();
161
162         key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);
163
164         /* every vblank, copy the shadow OAM automatically */
165         /*dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, DMACNT_VBLANK |
166                         DMACNT_REPEAT | DMACNT_INC_RELOAD);*/
167
168         for(;;) {
169                 handle_keys();
170
171                 backbuf = ++nframes & 1;
172
173                 fb = fbptr[backbuf] + 240 * MENU_HEIGHT;
174                 polyfill_framebuffer(fb, 240, VP_HEIGHT);
175                 dma_fill16(3, fb, 0x0e0e, 240 * VP_HEIGHT / 2);
176
177                 xgl_viewport(0, 0, 240, VP_HEIGHT);
178                 xgl_load_identity();
179                 xgl_translate(0, 0, 8 << 16);
180                 xgl_mult_matrix(rot_matrix);
181
182                 if(show_obj) {
183                         if(cam_theta < X_PI) {
184                                 xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
185                         } else {
186                                 xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
187                         }
188                         if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
189                                 xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
190                         } else {
191                                 xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
192                         }
193                         if(show_obj == 1) {
194                                 xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
195                         } else {
196                                 xgl_draw(XGL_TRIANGLES, suzanne, sizeof suzanne / sizeof *suzanne);
197                         }
198                         if(cam_theta < X_PI) {
199                                 xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
200                         } else {
201                                 xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
202                         }
203                         if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
204                                 xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
205                         } else {
206                                 xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
207                         }
208                 } else {
209                         xgl_draw(XGL_LINES, gridaxes + 8, 8);
210                 }
211
212                 /* small axes */
213                 xgl_viewport(176, 2, 64, 32);
214                 xgl_draw(XGL_LINES, small_axes, 6);
215
216                 xgl_transform(small_axes + 5, &x, &y);
217                 set_sprite(oam, SIDX_ICONS_BASE + 3, SNAM_ICON_Z, x - 8, y + 8, 3, SPR_SZ16);
218                 xgl_transform(small_axes + 1, &x, &y);
219                 set_sprite(oam, SIDX_ICONS_BASE + 4, SNAM_ICON_X, x - 8, y + 8, 1, SPR_SZ16);
220                 xgl_transform(small_axes + 3, &x, &y);
221                 set_sprite(oam, SIDX_ICONS_BASE + 5, SNAM_ICON_Y, x - 8, y + 8, 2, SPR_SZ16);
222
223                 wait_vblank();
224                 present(backbuf);
225                 dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
226         }
227
228         return 0;
229 }
230
231 static void handle_keys(void)
232 {
233         update_keyb();
234
235         if(KEYPRESS(KEY_UP)) {
236                 cam_phi += 0x2000;
237                 if(cam_phi > X_HPI) cam_phi = X_HPI;
238                 upd_rotation();
239         }
240         if(KEYPRESS(KEY_DOWN)) {
241                 cam_phi -= 0x2000;
242                 if(cam_phi < -X_HPI) cam_phi = -X_HPI;
243                 upd_rotation();
244         }
245         if(KEYPRESS(KEY_LEFT)) {
246                 cam_theta += 0x2000;
247                 if(cam_theta > X_2PI) cam_theta -= X_2PI;
248                 upd_rotation();
249         }
250         if(KEYPRESS(KEY_RIGHT)) {
251                 cam_theta -= 0x2000;
252                 if(cam_theta < 0) cam_theta += X_2PI;
253                 upd_rotation();
254         }
255         if(KEYPRESS(KEY_RT)) {
256                 if(++show_obj > 2) show_obj = 0;
257         }
258         if(KEYPRESS(KEY_LT)) {
259                 if(--show_obj < 0) show_obj = 2;
260         }
261
262         if(KEYPRESS(KEY_A)) {
263                 if(!show_del) {
264                         if(show_obj) show_del = 1;
265                 } else {
266                         show_del = 0;
267                 }
268                 show_msgbox(show_del);
269         }
270         if(KEYPRESS(KEY_B)) {
271                 if(show_del) {
272                         show_obj = 0;
273                         show_del = 0;
274                         show_msgbox(0);
275                 }
276         }
277 }
278
279 static void upd_rotation(void)
280 {
281         xgl_load_identity();
282         xgl_rotate_x(cam_phi);
283         xgl_rotate_y(cam_theta);
284         xgl_get_matrix(rot_matrix);
285 }
286
287 static void show_msgbox(int en)
288 {
289         int i;
290
291         if(en) {
292                 for(i=0; i<3; i++) {
293                         set_sprite(oam, SIDX_DEL0 + i, SNAM_DEL0 + i * 8, 42 + i * 64, 50, 0,
294                                         SPR_SZ64 | SPR_BLEND);
295                 }
296         } else {
297                 for(i=0; i<3; i++) {
298                         set_sprite(oam, SIDX_DEL0 + i, 0, 0, 0, 0, 0);
299                 }
300         }
301 }