panning
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 16 Mar 2021 14:30:45 +0000 (16:30 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 16 Mar 2021 14:30:45 +0000 (16:30 +0200)
Makefile
src/data.s
src/main.c
src/polyfill.c
src/sprites.c
src/xgl.c
src/xgl.h

index be6de8f..3484b4a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,8 @@ data = data/bg.raw data/bg.pal \
           data/sprites2.pal \
           data/sprites3.pal \
           data/sprites4.pal \
-          data/sprites5.pal
+          data/sprites5.pal \
+          data/sprites6.pal
 
 ARCH = arm-none-eabi-
 
@@ -24,8 +25,9 @@ EMU = vbam
 
 opt = -O3 -fomit-frame-pointer -mcpu=arm7tdmi -mtune=arm7tdmi -mthumb -mthumb-interwork
 #dbg = -g
+def = -DALT_LCLIP
 
-CFLAGS = $(opt) $(dbg) -pedantic -Wall -MMD
+CFLAGS = $(opt) $(dbg) -pedantic -Wall -MMD $(def)
 ASFLAGS = -mthumb-interwork
 LDFLAGS = -mthumb -mthumb-interwork -lm
 EMUFLAGS = -T 100 -f 1 --agb-print
@@ -48,7 +50,7 @@ src/data.o: src/data.s $(data)
 tools/pngdump/pngdump:
        $(MAKE) -C tools/pngdump
 
-data/sprites.raw: data/sprites1.png data/sprites2.png data/sprites3.png data/sprites4.png data/sprites5.png
+data/sprites.raw: data/sprites1.png data/sprites2.png data/sprites3.png data/sprites4.png data/sprites5.png data/sprites6.png
        tools/pngdump/pngdump -o $@ -n $^
 
 %.raw: %.png tools/pngdump/pngdump
index 0cb4fc3..db004e7 100644 (file)
@@ -24,3 +24,4 @@ sprites_cmap:
        .incbin "data/sprites3.pal"
        .incbin "data/sprites4.pal"
        .incbin "data/sprites5.pal"
+       .incbin "data/sprites6.pal"
index 9f42c02..b5648d0 100644 (file)
@@ -38,7 +38,7 @@ enum {
        SIDX_ICON_X,
        SIDX_ICON_Y,
        SIDX_ICON_Z,
-       SIDX_DIRTY,
+       SIDX_DIRTY
 };
 
 #define SIDX_ICONS_BASE                SIDX_ICON_ZOOM
@@ -54,7 +54,9 @@ enum {
        SNAM_ICON_X             = SNAM_START + 32 * 8 + 26,
        SNAM_ICON_Y             = SNAM_ICON_X + 2,
        SNAM_ICON_Z             = SNAM_ICON_Y + 2,
-       SNAM_DIRTY              = SNAM_ICON_ORBIT + 4
+       SNAM_DIRTY              = SNAM_ICON_ORBIT + 4,
+       SNAM_TIME0              = SNAM_START + 32 * 8,
+       SNAM_TIME1              = SNAM_TIME0 + 32 * 2
 };
 
 #define MENU_HEIGHT            17
@@ -69,9 +71,14 @@ extern struct { unsigned char r, g, b; } bgimg_cmap[];
 extern unsigned char bgimg_pixels[];
 
 static int32_t cam_theta = 0x10000, cam_phi = -0x8000;
+static int32_t cam_pan_x, cam_pan_y;
 
 static int show_obj = 1, show_del;
 static int32_t rot_matrix[16];
+static int pan_mode;
+
+static int snam_time[26];
+static int spos_time[104];
 
 #define AXIS0  0x10000
 #define AXIS1  0x80000
@@ -113,7 +120,7 @@ static uint16_t oam[4 * MAX_SIDX];
 int main(void)
 {
        int i;
-       int x, y;
+       int x, y, anm;
        unsigned int nframes = 0, backbuf;
        uint16_t *cptr;
        unsigned char r, g, b;
@@ -149,6 +156,15 @@ int main(void)
        REG_BLDCNT = BLDCNT_ALPHA | BLDCNT_B_BG2;
        REG_BLDALPHA = 0x040c;
 
+       for(i=0; i<13; i++) {
+               snam_time[i] = SNAM_TIME0 + (i << 1);
+               snam_time[i + 13] = SNAM_TIME1 + (i << 1);
+       }
+       for(i=0; i<104; i++) {
+               int t = (i << 16) / 103;
+               spos_time[i] = (t * 223) >> 16;
+       }
+
        set_sprite(oam, SIDX_ICONS_BASE, SNAM_ICON_ZOOM, 213, 57, 4, SPR_SZ32 | SPR_BLEND);
        set_sprite(oam, SIDX_ICONS_BASE + 1, SNAM_ICON_PAN, 213, 81, 4, SPR_SZ32 | SPR_BLEND);
        set_sprite(oam, SIDX_ICONS_BASE + 2, SNAM_ICON_ORBIT, 213, 103, 4, SPR_SZ32 | SPR_BLEND);
@@ -159,7 +175,7 @@ int main(void)
 
        upd_rotation();
 
-       key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP);
+       key_repeat(75, 75, KEY_LEFT | KEY_RIGHT | KEY_DOWN | KEY_UP | KEY_LT | KEY_RT);
 
        /* every vblank, copy the shadow OAM automatically */
        /*dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, DMACNT_VBLANK |
@@ -176,7 +192,7 @@ int main(void)
 
                xgl_viewport(0, 0, 240, VP_HEIGHT);
                xgl_load_identity();
-               xgl_translate(0, 0, 8 << 16);
+               xgl_translate(-cam_pan_x, -cam_pan_y, 8 << 16);
                xgl_mult_matrix(rot_matrix);
 
                if(show_obj) {
@@ -211,6 +227,7 @@ int main(void)
 
                /* small axes */
                xgl_viewport(176, 2, 64, 32);
+               xgl_xyzzy();
                xgl_draw(XGL_LINES, small_axes, 6);
 
                xgl_transform(small_axes + 5, &x, &y);
@@ -220,6 +237,9 @@ int main(void)
                xgl_transform(small_axes + 3, &x, &y);
                set_sprite(oam, SIDX_ICONS_BASE + 5, SNAM_ICON_Y, x - 8, y + 8, 2, SPR_SZ16);
 
+               anm = (timer_msec >> 5) % 104;
+               set_sprite(oam, SIDX_TIME, snam_time[anm >> 2], spos_time[anm], 144, 5, SPR_SZ16);
+
                wait_vblank();
                present(backbuf);
                dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
@@ -228,35 +248,69 @@ int main(void)
        return 0;
 }
 
+#define PAN_SPEED      0x4000
+
 static void handle_keys(void)
 {
        update_keyb();
 
        if(KEYPRESS(KEY_UP)) {
-               cam_phi += 0x2000;
-               if(cam_phi > X_HPI) cam_phi = X_HPI;
-               upd_rotation();
+               if(pan_mode) {
+                       cam_pan_y += PAN_SPEED;
+                       if(cam_pan_y > 0x1c000) cam_pan_y = 0x1c000;
+               } else {
+                       cam_phi += 0x2000;
+                       if(cam_phi > X_HPI) cam_phi = X_HPI;
+                       upd_rotation();
+               }
        }
        if(KEYPRESS(KEY_DOWN)) {
-               cam_phi -= 0x2000;
-               if(cam_phi < -X_HPI) cam_phi = -X_HPI;
-               upd_rotation();
+               if(pan_mode) {
+                       cam_pan_y -= PAN_SPEED;
+                       if(cam_pan_y < -0x1c000) cam_pan_y = -0x1c000;
+               } else {
+                       cam_phi -= 0x2000;
+                       if(cam_phi < -X_HPI) cam_phi = -X_HPI;
+                       upd_rotation();
+               }
        }
        if(KEYPRESS(KEY_LEFT)) {
-               cam_theta += 0x2000;
-               if(cam_theta > X_2PI) cam_theta -= X_2PI;
-               upd_rotation();
+               if(pan_mode) {
+                       cam_pan_x -= PAN_SPEED;
+                       if(cam_pan_x < -0x50000) cam_pan_x = -0x50000;
+               } else {
+                       cam_theta += 0x2000;
+                       if(cam_theta > X_2PI) cam_theta -= X_2PI;
+                       upd_rotation();
+               }
        }
        if(KEYPRESS(KEY_RIGHT)) {
-               cam_theta -= 0x2000;
-               if(cam_theta < 0) cam_theta += X_2PI;
-               upd_rotation();
+               if(pan_mode) {
+                       cam_pan_x += PAN_SPEED;
+                       if(cam_pan_x > 0x50000) cam_pan_x = 0x50000;
+               } else {
+                       cam_theta -= 0x2000;
+                       if(cam_theta < 0) cam_theta += X_2PI;
+                       upd_rotation();
+               }
+       }
+
+       if(KEYPRESS(KEY_START)) {
+               pan_mode ^= 1;
        }
+       if(KEYPRESS(KEY_SELECT)) {
+               if(show_obj) {
+                       show_obj = ((show_obj - 1) ^ 1) + 1;
+               } else {
+                       show_obj = 1;
+               }
+       }
+
        if(KEYPRESS(KEY_RT)) {
-               if(++show_obj > 2) show_obj = 0;
+               cam_pan_x += PAN_SPEED;
        }
        if(KEYPRESS(KEY_LT)) {
-               if(--show_obj < 0) show_obj = 2;
+               cam_pan_x -= PAN_SPEED;
        }
 
        if(KEYPRESS(KEY_A)) {
index 856ea5f..18294f5 100644 (file)
@@ -208,6 +208,19 @@ int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax,
        return 1;
 }
 
+#ifdef ALT_LCLIP
+#define PUTPIXEL(ptr) \
+       do { \
+               if(x0 >= 0 && x0 < fbwidth && y0 >= 0 && y0 < fbheight) { \
+                       uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
+                       if((uint32_t)ptr & 1) { \
+                               *pptr = (*pptr & 0xff) | (color << 8); \
+                       } else { \
+                               *pptr = (*pptr & 0xff00) | color; \
+                       } \
+               } \
+       } while(0)
+#else  /* !ALT_LCLIP */
 #define PUTPIXEL(ptr) \
        do { \
                uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
@@ -217,10 +230,14 @@ int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax,
                        *pptr = (*pptr & 0xff00) | color; \
                } \
        } while(0)
+#endif
 
 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
 {
        int i, dx, dy, x_inc, y_inc, error;
+#ifdef ALT_LCLIP
+       int y0inc;
+#endif
        unsigned char *fbptr = fb;
 
        fbptr += y0 * fbwidth + x0;
@@ -236,8 +253,14 @@ void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
        }
        if(dy >= 0) {
                y_inc = fbwidth;
+#ifdef ALT_LCLIP
+               y0inc = 1;
+#endif
        } else {
                y_inc = -fbwidth;
+#ifdef ALT_LCLIP
+               y0inc = -1;
+#endif
                dy = -dy;
        }
 
@@ -248,9 +271,15 @@ void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
                        if(error >= 0) {
                                error -= dx * 2;
                                fbptr += y_inc;
+#ifdef ALT_LCLIP
+                               y0 += y0inc;
+#endif
                        }
                        error += dy * 2;
                        fbptr += x_inc;
+#ifdef ALT_LCLIP
+                       x0 += x_inc;
+#endif
                }
        } else {
                error = dx * 2 - dy;
@@ -259,9 +288,15 @@ void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
                        if(error >= 0) {
                                error -= dy * 2;
                                fbptr += x_inc;
+#ifdef ALT_LCLIP
+                               x0 += x_inc;
+#endif
                        }
                        error += dx * 2;
                        fbptr += y_inc;
+#ifdef ALT_LCLIP
+                       y0 += y0inc;
+#endif
                }
        }
 }
index 8cd5b2c..c00929b 100644 (file)
@@ -32,7 +32,7 @@ void init_sprites(void)
        /* copy from cartridge to OBJ RAM */
        dst = (uint16_t*)VRAM_LFB_OBJ_ADDR;
        src = (uint16_t*)sprites_pixels;
-       for(i=0; i<320; i++) {  /* 320 tiles */
+       for(i=0; i<384; i++) {  /* 384 tiles */
                for(j=0; j<8; j++) {
                        *dst++ = src[j * 64];
                        *dst++ = src[j * 64 + 1];
@@ -48,7 +48,7 @@ void init_sprites(void)
 
        /* setup OBJ colormaps */
        cptr = (uint16_t*)CRAM_OBJ_ADDR;
-       for(i=0; i<80; i++) {   /* 5 colormaps of 16 colors each */
+       for(i=0; i<96; i++) {   /* 6 colormaps of 16 colors each */
                unsigned char r = sprites_cmap[i].r >> 3;
                unsigned char g = sprites_cmap[i].g >> 3;
                unsigned char b = sprites_cmap[i].b >> 3;
index 2632473..deccdea 100644 (file)
--- a/src/xgl.c
+++ b/src/xgl.c
@@ -281,7 +281,14 @@ static void draw_ptlines(int prim, const struct xvertex *varr, int vcount)
                vcount -= prim;
 
                /* line clipping */
+#ifndef ALT_LCLIP
                clip_line((int*)&xv[0].x, (int*)&xv[0].y, (int*)&xv[1].x, (int*)&xv[1].y, vp[0], vp[1], vp[2] - 1, vp[3] - 1);
+#endif
                draw_line(xv[0].x, xv[0].y, xv[1].x, xv[1].y, varr[-2].cidx);
        }
 }
+
+void xgl_xyzzy(void)
+{
+       mat[mtop][12] = mat[mtop][13] = 0;
+}
index 6076073..eba42b5 100644 (file)
--- a/src/xgl.h
+++ b/src/xgl.h
@@ -63,4 +63,6 @@ void xgl_scale(int32_t x, int32_t y, int32_t z);
 void xgl_draw(int prim, const struct xvertex *varr, int vcount);
 void xgl_transform(const struct xvertex *vin, int *x, int *y);
 
+void xgl_xyzzy(void);
+
 #endif /* XGL_H_ */