fixed 8bit writes to vmem, unaligned data buffers, and started on
[gba_blender] / src / main.c
index 0eeaa5a..30d92c2 100644 (file)
@@ -15,30 +15,75 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
+#include <stdlib.h>
 #include <string.h>
 #include "gbaregs.h"
 #include "timer.h"
 #include "keyb.h"
 #include "intr.h"
 #include "gfx.h"
+#include "xgl.h"
+#include "polyfill.h"
+#include "debug.h"
+#include "meshdata.h"
+#include "sprites.h"
+#include "dma.h"
+
+#define MENU_HEIGHT            17
+#define TRACK_HEIGHT   18
+#define VP_HEIGHT              (160 - MENU_HEIGHT - TRACK_HEIGHT)
 
 static void handle_keys(void);
 
 extern struct { unsigned char r, g, b; } bgimg_cmap[];
 extern unsigned char bgimg_pixels[];
 
+static int32_t cam_theta = 0x10000, cam_phi = -0x8000;
+
+static int show_obj = 1, show_del;
+
+#define AXIS0  0x10000
+#define AXIS1  0x80000
+
+static struct xvertex gridaxes[] = {
+       {AXIS0, 0, 0,   0, 0, 0, 92},
+       {AXIS1, 0, 0,   0, 0, 0, 92},
+       {-AXIS0, 0, 0,  0, 0, 0, 92},
+       {-AXIS1, 0, 0,  0, 0, 0, 92},
+       {0, 0, AXIS0,   0, 0, 0, 93},
+       {0, 0, AXIS1,   0, 0, 0, 93},
+       {0, 0, -AXIS0,  0, 0, 0, 93},
+       {0, 0, -AXIS1,  0, 0, 0, 93},
+
+       {0, 0, 0,               0, 0, 0, 92},
+       {AXIS1, 0, 0,   0, 0, 0, 92},
+       {0, 0, 0,               0, 0, 0, 92},
+       {-AXIS1, 0, 0,  0, 0, 0, 92},
+       {0, 0, 0,               0, 0, 0, 93},
+       {0, 0, AXIS1,   0, 0, 0, 93},
+       {0, 0, -0,              0, 0, 0, 93},
+       {0, 0, -AXIS1,  0, 0, 0, 93},
+};
+
 int main(void)
 {
        int i;
+       unsigned int nframes = 0, backbuf;
        uint16_t *cptr;
        unsigned char r, g, b;
+       unsigned char *fbptr[2], *fb;
 
        intr_init();
        reset_msec_timer();
        set_intr();
 
        /* mode 4: 240x160 8bpp */
-       REG_DISPCNT = DISPCNT_BG2 | 4;
+       REG_DISPCNT = DISPCNT_BG2 | DISPCNT_OBJ | 4;
+
+       fbptr[0] = (unsigned char*)VRAM_LFB_FB0_ADDR;
+       fbptr[1] = (unsigned char*)VRAM_LFB_FB1_ADDR;
+
+       set_bg_color(0xff, 31, 31, 31);
 
        cptr = (uint16_t*)CRAM_BG_ADDR;
        for(i=0; i<128; i++) {
@@ -47,14 +92,68 @@ int main(void)
                b = bgimg_cmap[i].b >> 3;
                *cptr++ = r | (g << 5) | (b << 10);
        }
-       memcpy((void*)VRAM_LFB_FB0_ADDR, bgimg_pixels, 240 * 160);
-       memcpy((void*)VRAM_LFB_FB1_ADDR, bgimg_pixels, 240 * 160);
+       for(i=0; i<128; i++) {
+               r = i / 5 + 6;
+               *cptr++ = r | (r << 5) | (r << 10);
+       }
+       dma_copy16(3, fbptr[0], bgimg_pixels, 240 * 160 / 2);
+       dma_copy16(3, fbptr[1], bgimg_pixels, 240 * 160 / 2);
+
+       init_sprites();
+       set_sprite(0, 0, 512, 42, 42, 0, SPR_SZ64);
 
-       /*key_repeat(500, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);*/
+       xgl_init();
+       xgl_viewport(0, 0, 240, VP_HEIGHT);
+       xgl_enable(XGL_LIGHTING);
+
+       key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);
 
        for(;;) {
+               handle_keys();
+
+               backbuf = ++nframes & 1;
+
+               fb = fbptr[backbuf] + 240 * MENU_HEIGHT;
+               polyfill_framebuffer(fb, 240, VP_HEIGHT);
+               memset(fb, 14, 240 * VP_HEIGHT);
+
+               xgl_load_identity();
+               xgl_translate(0, 0, 8 << 16);
+               xgl_rotate_x(cam_phi);
+               xgl_rotate_y(cam_theta);
+
+               if(show_obj) {
+                       if(cam_theta < X_PI) {
+                               xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
+                       } else {
+                               xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
+                       }
+                       if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
+                               xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
+                       } else {
+                               xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
+                       }
+                       if(show_obj == 1) {
+                               xgl_draw(XGL_QUADS, cube, sizeof cube / sizeof *cube);
+                       } else {
+                               xgl_draw(XGL_TRIANGLES, suzanne, sizeof suzanne / sizeof *suzanne);
+                       }
+                       if(cam_theta < X_PI) {
+                               xgl_draw(XGL_LINES, gridaxes, 2);               /* +X */
+                       } else {
+                               xgl_draw(XGL_LINES, gridaxes + 2, 2);   /* -X */
+                       }
+                       if(cam_theta < X_HPI || cam_theta > (3 * X_HPI)) {
+                               xgl_draw(XGL_LINES, gridaxes + 6, 2);   /* -Z */
+                       } else {
+                               xgl_draw(XGL_LINES, gridaxes + 4, 2);   /* +Z */
+                       }
+               } else {
+                       xgl_draw(XGL_LINES, gridaxes + 8, 8);
+               }
+
                wait_vblank();
-               swap_buffers();
+               present(backbuf);
        }
 
        return 0;
@@ -65,11 +164,35 @@ static void handle_keys(void)
        update_keyb();
 
        if(KEYPRESS(KEY_UP)) {
+               cam_phi += 0x2000;
+               if(cam_phi > X_HPI) cam_phi = X_HPI;
        }
        if(KEYPRESS(KEY_DOWN)) {
+               cam_phi -= 0x2000;
+               if(cam_phi < -X_HPI) cam_phi = -X_HPI;
        }
        if(KEYPRESS(KEY_LEFT)) {
+               cam_theta += 0x2000;
+               if(cam_theta > X_2PI) cam_theta -= X_2PI;
        }
        if(KEYPRESS(KEY_RIGHT)) {
+               cam_theta -= 0x2000;
+               if(cam_theta < 0) cam_theta += X_2PI;
+       }
+       if(KEYPRESS(KEY_RT)) {
+               if(++show_obj > 2) show_obj = 0;
+       }
+       if(KEYPRESS(KEY_LT)) {
+               if(--show_obj < 0) show_obj = 2;
+       }
+
+       if(KEYPRESS(KEY_A)) {
+               show_del ^= 1;
+       }
+       if(KEYPRESS(KEY_B)) {
+               if(show_del) {
+                       show_obj = 0;
+                       show_del = 0;
+               }
        }
 }