2 blender for the Gameboy Advance
3 Copyright (C) 2021 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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/>.
33 SIDX_DEL0, SIDX_DEL1, SIDX_DEL2,
44 #define SIDX_ICONS_BASE SIDX_ICON_ZOOM
46 #define SNAM_START 512
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
60 #define MENU_HEIGHT 17
61 #define TRACK_HEIGHT 18
62 #define VP_HEIGHT (160 - MENU_HEIGHT - TRACK_HEIGHT)
64 static void handle_keys(void);
65 static void upd_rotation(void);
66 static void show_msgbox(int en);
68 extern struct { unsigned char r, g, b; } bgimg_cmap[];
69 extern unsigned char bgimg_pixels[];
71 static int32_t cam_theta = 0x10000, cam_phi = -0x8000;
73 static int show_obj = 1, show_del;
74 static int32_t rot_matrix[16];
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},
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},
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}
110 static uint16_t oam[4 * MAX_SIDX];
117 unsigned int nframes = 0, backbuf;
119 unsigned char r, g, b;
120 unsigned char *fbptr[2], *fb;
126 /* mode 4: 240x160 8bpp */
127 REG_DISPCNT = DISPCNT_BG2 | DISPCNT_OBJ | 4;
129 fbptr[0] = (unsigned char*)VRAM_LFB_FB0_ADDR;
130 fbptr[1] = (unsigned char*)VRAM_LFB_FB1_ADDR;
132 set_bg_color(0xff, 31, 31, 31);
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);
141 for(i=0; i<128; i++) {
143 *cptr++ = r | (r << 5) | (r << 10);
145 dma_copy16(3, fbptr[0], bgimg_pixels, 240 * 160 / 2, 0);
146 dma_copy16(3, fbptr[1], bgimg_pixels, 240 * 160 / 2, 0);
149 REG_BLDCNT = BLDCNT_ALPHA | BLDCNT_B_BG2;
150 REG_BLDALPHA = 0x040c;
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);
158 xgl_enable(XGL_LIGHTING);
162 key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);
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);*/
171 backbuf = ++nframes & 1;
173 fb = fbptr[backbuf] + 240 * MENU_HEIGHT;
174 polyfill_framebuffer(fb, 240, VP_HEIGHT);
175 dma_fill16(3, fb, 0x0e0e, 240 * VP_HEIGHT / 2);
177 xgl_viewport(0, 0, 240, VP_HEIGHT);
179 xgl_translate(0, 0, 8 << 16);
180 xgl_mult_matrix(rot_matrix);
183 if(cam_theta < X_PI) {
184 xgl_draw(XGL_LINES, gridaxes + 2, 2); /* -X */
186 xgl_draw(XGL_LINES, gridaxes, 2); /* +X */
188 if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
189 xgl_draw(XGL_LINES, gridaxes + 4, 2); /* +Z */
191 xgl_draw(XGL_LINES, gridaxes + 6, 2); /* -Z */
194 xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
196 xgl_draw(XGL_TRIANGLES, suzanne, sizeof suzanne / sizeof *suzanne);
198 if(cam_theta < X_PI) {
199 xgl_draw(XGL_LINES, gridaxes, 2); /* +X */
201 xgl_draw(XGL_LINES, gridaxes + 2, 2); /* -X */
203 if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
204 xgl_draw(XGL_LINES, gridaxes + 6, 2); /* -Z */
206 xgl_draw(XGL_LINES, gridaxes + 4, 2); /* +Z */
209 xgl_draw(XGL_LINES, gridaxes + 8, 8);
213 xgl_viewport(176, 2, 64, 32);
214 xgl_draw(XGL_LINES, small_axes, 6);
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);
225 dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
231 static void handle_keys(void)
235 if(KEYPRESS(KEY_UP)) {
237 if(cam_phi > X_HPI) cam_phi = X_HPI;
240 if(KEYPRESS(KEY_DOWN)) {
242 if(cam_phi < -X_HPI) cam_phi = -X_HPI;
245 if(KEYPRESS(KEY_LEFT)) {
247 if(cam_theta > X_2PI) cam_theta -= X_2PI;
250 if(KEYPRESS(KEY_RIGHT)) {
252 if(cam_theta < 0) cam_theta += X_2PI;
255 if(KEYPRESS(KEY_RT)) {
256 if(++show_obj > 2) show_obj = 0;
258 if(KEYPRESS(KEY_LT)) {
259 if(--show_obj < 0) show_obj = 2;
262 if(KEYPRESS(KEY_A)) {
264 if(show_obj) show_del = 1;
268 show_msgbox(show_del);
270 if(KEYPRESS(KEY_B)) {
279 static void upd_rotation(void)
282 xgl_rotate_x(cam_phi);
283 xgl_rotate_y(cam_theta);
284 xgl_get_matrix(rot_matrix);
287 static void show_msgbox(int en)
293 set_sprite(oam, SIDX_DEL0 + i, SNAM_DEL0 + i * 8, 42 + i * 64, 50, 0,
294 SPR_SZ64 | SPR_BLEND);
298 set_sprite(oam, SIDX_DEL0 + i, 0, 0, 0, 0, 0);