grid axes
[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
30 #define MENU_HEIGHT             17
31 #define TRACK_HEIGHT    18
32 #define VP_HEIGHT               (160 - MENU_HEIGHT - TRACK_HEIGHT)
33
34 static void handle_keys(void);
35
36 extern struct { unsigned char r, g, b; } bgimg_cmap[];
37 extern unsigned char bgimg_pixels[];
38
39 static int32_t cam_theta = 0x10000, cam_phi = -0x8000;
40
41 static int show_obj = 1, show_del;
42
43 #define AXIS0   0x10000
44 #define AXIS1   0x80000
45
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},
55
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},
64 };
65
66 int main(void)
67 {
68         int i;
69         unsigned int nframes = 0, backbuf;
70         uint16_t *cptr;
71         unsigned char r, g, b;
72         unsigned char *fbptr[2], *fb;
73
74         intr_init();
75         reset_msec_timer();
76         set_intr();
77
78         /* mode 4: 240x160 8bpp */
79         REG_DISPCNT = DISPCNT_BG2 | 4;
80
81         fbptr[0] = (unsigned char*)VRAM_LFB_FB0_ADDR;
82         fbptr[1] = (unsigned char*)VRAM_LFB_FB1_ADDR;
83
84         set_bg_color(0xff, 31, 31, 31);
85
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);
92         }
93         for(i=0; i<128; i++) {
94                 r = i / 5 + 6;
95                 *cptr++ = r | (r << 5) | (r << 10);
96         }
97         memcpy(fbptr[0], bgimg_pixels, 240 * 160);
98         memcpy(fbptr[1], bgimg_pixels, 240 * 160);
99
100         xgl_init();
101         xgl_viewport(0, 0, 240, VP_HEIGHT);
102         xgl_enable(XGL_LIGHTING);
103
104         key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);
105
106         for(;;) {
107                 handle_keys();
108
109                 backbuf = ++nframes & 1;
110
111                 fb = fbptr[backbuf] + 240 * MENU_HEIGHT;
112                 polyfill_framebuffer(fb, 240, VP_HEIGHT);
113                 memset(fb, 14, 240 * VP_HEIGHT);
114
115                 xgl_load_identity();
116                 xgl_translate(0, 0, 8 << 16);
117                 xgl_rotate_x(cam_phi);
118                 xgl_rotate_y(cam_theta);
119
120                 if(show_obj) {
121                         if(cam_theta < X_PI) {
122                                 xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
123                         } else {
124                                 xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
125                         }
126                         if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
127                                 xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
128                         } else {
129                                 xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
130                         }
131
132                         if(show_obj == 1) {
133                                 xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
134                         } else {
135                                 xgl_draw(XGL_TRIANGLES, suzanne, sizeof suzanne / sizeof *suzanne);
136                         }
137
138                         if(cam_theta < X_PI) {
139                                 xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
140                         } else {
141                                 xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
142                         }
143                         if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
144                                 xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
145                         } else {
146                                 xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
147                         }
148                 } else {
149                         xgl_draw(XGL_LINES, gridaxes + 8, 8);
150                 }
151
152                 wait_vblank();
153                 present(backbuf);
154         }
155
156         return 0;
157 }
158
159 static void handle_keys(void)
160 {
161         update_keyb();
162
163         if(KEYPRESS(KEY_UP)) {
164                 cam_phi += 0x2000;
165                 if(cam_phi > X_HPI) cam_phi = X_HPI;
166         }
167         if(KEYPRESS(KEY_DOWN)) {
168                 cam_phi -= 0x2000;
169                 if(cam_phi < -X_HPI) cam_phi = -X_HPI;
170         }
171         if(KEYPRESS(KEY_LEFT)) {
172                 cam_theta += 0x2000;
173                 if(cam_theta > X_2PI) cam_theta -= X_2PI;
174         }
175         if(KEYPRESS(KEY_RIGHT)) {
176                 cam_theta -= 0x2000;
177                 if(cam_theta < 0) cam_theta += X_2PI;
178         }
179         if(KEYPRESS(KEY_RT)) {
180                 if(++show_obj > 2) show_obj = 0;
181         }
182         if(KEYPRESS(KEY_LT)) {
183                 if(--show_obj < 0) show_obj = 2;
184         }
185
186         if(KEYPRESS(KEY_A)) {
187                 show_del ^= 1;
188         }
189         if(KEYPRESS(KEY_B)) {
190                 if(show_del) {
191                         show_obj = 0;
192                         show_del = 0;
193                 }
194         }
195 }