polygon filler, rudimentary GL
[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
29 #define MENU_HEIGHT             17
30 #define TRACK_HEIGHT    18
31 #define VP_HEIGHT               (160 - MENU_HEIGHT - TRACK_HEIGHT)
32
33 static void handle_keys(void);
34
35 extern struct { unsigned char r, g, b; } bgimg_cmap[];
36 extern unsigned char bgimg_pixels[];
37
38 struct xvertex varr[] = {
39         {0, -0xd000},
40         {-0x8000, 0x7000},
41         {0x8000, 0x7000}
42 };
43
44 int main(void)
45 {
46         int i;
47         unsigned int nframes = 0, backbuf;
48         unsigned long tm0, tm;
49         uint16_t *cptr;
50         unsigned char r, g, b;
51         unsigned char *fbptr[2], *fb;
52         struct pvertex benchv[3] = {
53                 {120 << 8, 8 << 8},
54                 {75 << 8, 110 << 8},
55                 {164 << 8, 80 << 8}
56         };
57
58         intr_init();
59         reset_msec_timer();
60         set_intr();
61
62         /* mode 4: 240x160 8bpp */
63         REG_DISPCNT = DISPCNT_BG2 | 4;
64
65         fbptr[0] = (unsigned char*)VRAM_LFB_FB0_ADDR;
66         fbptr[1] = (unsigned char*)VRAM_LFB_FB1_ADDR;
67
68         set_bg_color(0xff, 31, 31, 31);
69
70         cptr = (uint16_t*)CRAM_BG_ADDR;
71         for(i=0; i<128; i++) {
72                 r = bgimg_cmap[i].r >> 3;
73                 g = bgimg_cmap[i].g >> 3;
74                 b = bgimg_cmap[i].b >> 3;
75                 *cptr++ = r | (g << 5) | (b << 10);
76         }
77         for(i=0; i<128; i++) {
78                 r = (rand() & 0xf) + 8;
79                 g = (rand() & 0xf) + 8;
80                 b = (rand() & 0xf) + 8;
81                 *cptr++ = r | (g << 5) | (b << 10);
82         }
83         memcpy(fbptr[0], bgimg_pixels, 240 * 160);
84         memcpy(fbptr[1], bgimg_pixels, 240 * 160);
85
86         xgl_init();
87         xgl_viewport(0, MENU_HEIGHT, 240, VP_HEIGHT);
88
89         /* benchmark */
90         polyfill_framebuffer(fbptr[0] + 240 * MENU_HEIGHT, 240, VP_HEIGHT);
91         tm0 = timer_msec;
92         for(i=0; i<2048; i++) {
93                 polyfill_flat(benchv, 3, 128 + (i & 0x7f));
94         }
95         tm = timer_msec - tm0;
96         emuprint("benchmark: %lu ms\n", tm);
97
98         /*key_repeat(500, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);*/
99
100         for(;;) {
101                 backbuf = ++nframes & 1;
102
103                 fb = fbptr[backbuf] + 240 * MENU_HEIGHT;
104                 polyfill_framebuffer(fb, 240, VP_HEIGHT);
105                 memset(fb, 14, 240 * VP_HEIGHT);
106
107                 xgl_load_identity();
108                 xgl_rotate_z(nframes << 8);
109                 xgl_draw(XGL_TRIANGLES, varr, 3);
110
111                 wait_vblank();
112                 present(backbuf);
113         }
114
115         return 0;
116 }
117
118 static void handle_keys(void)
119 {
120         update_keyb();
121
122         if(KEYPRESS(KEY_UP)) {
123         }
124         if(KEYPRESS(KEY_DOWN)) {
125         }
126         if(KEYPRESS(KEY_LEFT)) {
127         }
128         if(KEYPRESS(KEY_RIGHT)) {
129         }
130 }