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/>.
30 #define MENU_HEIGHT 17
31 #define TRACK_HEIGHT 18
32 #define VP_HEIGHT (160 - MENU_HEIGHT - TRACK_HEIGHT)
34 static void handle_keys(void);
36 extern struct { unsigned char r, g, b; } bgimg_cmap[];
37 extern unsigned char bgimg_pixels[];
39 static int32_t cam_theta = 0x10000, cam_phi = -0x8000;
41 static int show_obj = 1, show_del;
46 static struct xvertex gridaxes[] = {
47 {AXIS0, 0, 0, 0, 0, 0, 92},
48 {AXIS1, 0, 0, 0, 0, 0, 92},
49 {-AXIS0, 0, 0, 0, 0, 0, 92},
50 {-AXIS1, 0, 0, 0, 0, 0, 92},
51 {0, 0, AXIS0, 0, 0, 0, 93},
52 {0, 0, AXIS1, 0, 0, 0, 93},
53 {0, 0, -AXIS0, 0, 0, 0, 93},
54 {0, 0, -AXIS1, 0, 0, 0, 93},
56 {0, 0, 0, 0, 0, 0, 92},
57 {AXIS1, 0, 0, 0, 0, 0, 92},
58 {0, 0, 0, 0, 0, 0, 92},
59 {-AXIS1, 0, 0, 0, 0, 0, 92},
60 {0, 0, 0, 0, 0, 0, 93},
61 {0, 0, AXIS1, 0, 0, 0, 93},
62 {0, 0, -0, 0, 0, 0, 93},
63 {0, 0, -AXIS1, 0, 0, 0, 93},
69 unsigned int nframes = 0, backbuf;
71 unsigned char r, g, b;
72 unsigned char *fbptr[2], *fb;
78 /* mode 4: 240x160 8bpp */
79 REG_DISPCNT = DISPCNT_BG2 | 4;
81 fbptr[0] = (unsigned char*)VRAM_LFB_FB0_ADDR;
82 fbptr[1] = (unsigned char*)VRAM_LFB_FB1_ADDR;
84 set_bg_color(0xff, 31, 31, 31);
86 cptr = (uint16_t*)CRAM_BG_ADDR;
87 for(i=0; i<128; i++) {
88 r = bgimg_cmap[i].r >> 3;
89 g = bgimg_cmap[i].g >> 3;
90 b = bgimg_cmap[i].b >> 3;
91 *cptr++ = r | (g << 5) | (b << 10);
93 for(i=0; i<128; i++) {
95 *cptr++ = r | (r << 5) | (r << 10);
97 memcpy(fbptr[0], bgimg_pixels, 240 * 160);
98 memcpy(fbptr[1], bgimg_pixels, 240 * 160);
101 xgl_viewport(0, 0, 240, VP_HEIGHT);
102 xgl_enable(XGL_LIGHTING);
104 key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);
109 backbuf = ++nframes & 1;
111 fb = fbptr[backbuf] + 240 * MENU_HEIGHT;
112 polyfill_framebuffer(fb, 240, VP_HEIGHT);
113 memset(fb, 14, 240 * VP_HEIGHT);
116 xgl_translate(0, 0, 8 << 16);
117 xgl_rotate_x(cam_phi);
118 xgl_rotate_y(cam_theta);
121 if(cam_theta < X_PI) {
122 xgl_draw(XGL_LINES, gridaxes + 2, 2); /* -X */
124 xgl_draw(XGL_LINES, gridaxes, 2); /* +X */
126 if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
127 xgl_draw(XGL_LINES, gridaxes + 4, 2); /* +Z */
129 xgl_draw(XGL_LINES, gridaxes + 6, 2); /* -Z */
133 xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
135 xgl_draw(XGL_TRIANGLES, suzanne, sizeof suzanne / sizeof *suzanne);
138 if(cam_theta < X_PI) {
139 xgl_draw(XGL_LINES, gridaxes, 2); /* +X */
141 xgl_draw(XGL_LINES, gridaxes + 2, 2); /* -X */
143 if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
144 xgl_draw(XGL_LINES, gridaxes + 6, 2); /* -Z */
146 xgl_draw(XGL_LINES, gridaxes + 4, 2); /* +Z */
149 xgl_draw(XGL_LINES, gridaxes + 8, 8);
159 static void handle_keys(void)
163 if(KEYPRESS(KEY_UP)) {
165 if(cam_phi > X_HPI) cam_phi = X_HPI;
167 if(KEYPRESS(KEY_DOWN)) {
169 if(cam_phi < -X_HPI) cam_phi = -X_HPI;
171 if(KEYPRESS(KEY_LEFT)) {
173 if(cam_theta > X_2PI) cam_theta -= X_2PI;
175 if(KEYPRESS(KEY_RIGHT)) {
177 if(cam_theta < 0) cam_theta += X_2PI;
179 if(KEYPRESS(KEY_RT)) {
180 if(++show_obj > 2) show_obj = 0;
182 if(KEYPRESS(KEY_LT)) {
183 if(--show_obj < 0) show_obj = 2;
186 if(KEYPRESS(KEY_A)) {
189 if(KEYPRESS(KEY_B)) {