semi-transparent delete window
[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         REG_BLDCNT = BLDCNT_ALPHA | BLDCNT_B_BG2;
121         REG_BLDALPHA = 0x040c;
122
123         xgl_init();
124         xgl_viewport(0, 0, 240, VP_HEIGHT);
125         xgl_enable(XGL_LIGHTING);
126
127         key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);
128
129         /* every vblank, copy the shadow OAM automatically */
130         /*dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, DMACNT_VBLANK |
131                         DMACNT_REPEAT | DMACNT_INC_RELOAD);*/
132
133         for(;;) {
134                 handle_keys();
135
136                 backbuf = ++nframes & 1;
137
138                 fb = fbptr[backbuf] + 240 * MENU_HEIGHT;
139                 polyfill_framebuffer(fb, 240, VP_HEIGHT);
140                 //memset(fb, 14, 240 * VP_HEIGHT);
141                 dma_fill16(3, fb, 0x0e0e, 240 * VP_HEIGHT / 2);
142
143                 xgl_load_identity();
144                 xgl_translate(0, 0, 8 << 16);
145                 xgl_rotate_x(cam_phi);
146                 xgl_rotate_y(cam_theta);
147
148                 if(show_obj) {
149                         if(cam_theta < X_PI) {
150                                 xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
151                         } else {
152                                 xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
153                         }
154                         if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
155                                 xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
156                         } else {
157                                 xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
158                         }
159                         if(show_obj == 1) {
160                                 xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
161                         } else {
162                                 xgl_draw(XGL_TRIANGLES, suzanne, sizeof suzanne / sizeof *suzanne);
163                         }
164                         if(cam_theta < X_PI) {
165                                 xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
166                         } else {
167                                 xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
168                         }
169                         if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
170                                 xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
171                         } else {
172                                 xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
173                         }
174                 } else {
175                         xgl_draw(XGL_LINES, gridaxes + 8, 8);
176                 }
177
178                 wait_vblank();
179                 present(backbuf);
180                 dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
181         }
182
183         return 0;
184 }
185
186 static void handle_keys(void)
187 {
188         update_keyb();
189
190         if(KEYPRESS(KEY_UP)) {
191                 cam_phi += 0x2000;
192                 if(cam_phi > X_HPI) cam_phi = X_HPI;
193         }
194         if(KEYPRESS(KEY_DOWN)) {
195                 cam_phi -= 0x2000;
196                 if(cam_phi < -X_HPI) cam_phi = -X_HPI;
197         }
198         if(KEYPRESS(KEY_LEFT)) {
199                 cam_theta += 0x2000;
200                 if(cam_theta > X_2PI) cam_theta -= X_2PI;
201         }
202         if(KEYPRESS(KEY_RIGHT)) {
203                 cam_theta -= 0x2000;
204                 if(cam_theta < 0) cam_theta += X_2PI;
205         }
206         if(KEYPRESS(KEY_RT)) {
207                 if(++show_obj > 2) show_obj = 0;
208         }
209         if(KEYPRESS(KEY_LT)) {
210                 if(--show_obj < 0) show_obj = 2;
211         }
212
213         if(KEYPRESS(KEY_A)) {
214                 if(!show_del) {
215                         if(show_obj) show_del = 1;
216                 } else {
217                         show_del = 0;
218                 }
219                 show_msgbox(show_del);
220         }
221         if(KEYPRESS(KEY_B)) {
222                 if(show_del) {
223                         show_obj = 0;
224                         show_del = 0;
225                         show_msgbox(0);
226                 }
227         }
228 }
229
230 static void show_msgbox(int en)
231 {
232         int i;
233
234         if(en) {
235                 for(i=0; i<3; i++) {
236                         set_sprite(oam, SIDX_DEL0 + i, SNAM_DEL0 + i * 8, 42 + i * 64, 50, 0,
237                                         SPR_SZ64 | SPR_BLEND);
238                 }
239         } else {
240                 for(i=0; i<3; i++) {
241                         set_sprite(oam, SIDX_DEL0 + i, 0, 0, 0, 0, 0);
242                 }
243         }
244 }