fixed the tunnel precalc to produce a continuous 10 bit depth value
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 25 Apr 2021 14:02:18 +0000 (17:02 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 25 Apr 2021 14:02:18 +0000 (17:02 +0300)
instead of repeating throughout the length of the tunnel, so it can be
used for fog too

.gdbinit [deleted file]
src/gamescr.c
tools/tungen.c

diff --git a/.gdbinit b/.gdbinit
deleted file mode 100644 (file)
index f4d7543..0000000
--- a/.gdbinit
+++ /dev/null
@@ -1 +0,0 @@
-target remote localhost:2345
index 2744d5d..6eee713 100644 (file)
@@ -13,7 +13,7 @@
 static void draw_tunnel(void);
 static void vblank(void);
 
-static int nframes, backbuf;
+static int nframes, num_vbl, backbuf;
 static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
 static unsigned char *tex;
 static uint16_t bnstate;
@@ -90,10 +90,13 @@ void gamescr(void)
        }
 }
 
+#define TUN_U(x)       ((x) & 0x3f)
+#define TUN_V(x)       (((x) >> 7) & 0x1ff)
+
 __attribute__((noinline, target("arm"), section(".iwram")))
 static void draw_tunnel(void)
 {
-       int i, j, tx, ty, angle, depth, zoffs, uoffs, flip, tunturn;
+       int i, j, tx, ty, u, v, angle, depth, zoffs, uoffs, flip, tunturn;
        static int tunsweep;
        uint16_t pptop, ppbot;
        uint16_t *top, *bot;
@@ -107,9 +110,8 @@ static void draw_tunnel(void)
        flip = tunsweep < 0;
        tunturn = abs(tunsweep) & 0x1f;
 
-       zoffs = nframes;
-
-       uoffs = flip ? -nframes : nframes;
+       zoffs = num_vbl;
+       uoffs = 0;//(flip ? -num_vbl : num_vbl) >> 2;
 
        top = vram[backbuf];
        bot = vram[backbuf] + 159 * 240 / 2;
@@ -120,22 +122,21 @@ static void draw_tunnel(void)
                for(i=0; i<80; i++) {
                        for(j=0; j<240/2; j++) {
                                tun = *--tunptr;
-                               tun = (tun >> 16) | (tun << 16);
 
-                               angle = tun & 0xff;
-                               depth = (tun >> 8) & 0xff;
-                               tx = ~((angle >> 1) - uoffs) & 0x1f;
-                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               angle = TUN_U(tun >> 16);
+                               depth = TUN_V(tun >> 16);
+                               tx = ~(angle - uoffs) & 0x1f;
+                               ty = (depth + zoffs) & 0x1f;
                                pptop = tex[(ty << 5) + tx];
-                               tx = ((angle >> 1) + uoffs) & 0x1f;
+                               tx = (angle + uoffs) & 0x1f;
                                ppbot = tex[(ty << 5) + tx];
 
-                               angle = (tun >> 16) & 0xff;
-                               depth = (tun >> 24) & 0xff;
-                               tx = ~((angle >> 1) - uoffs) & 0x1f;
-                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               angle = TUN_U(tun);
+                               depth = TUN_V(tun);
+                               tx = ~(angle - uoffs) & 0x1f;
+                               ty = (depth + zoffs) & 0x1f;
                                pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
-                               tx = ((angle >> 1) + uoffs) & 0x1f;
+                               tx = (angle + uoffs) & 0x1f;
                                ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
 
                                *top++ = pptop;
@@ -149,20 +150,20 @@ static void draw_tunnel(void)
                        for(j=0; j<240/2; j++) {
                                tun = *tunptr++;
 
-                               angle = tun & 0xff;
-                               depth = (tun >> 8) & 0xff;
-                               tx = ((angle >> 1) - uoffs) & 0x1f;
-                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               angle = TUN_U(tun);
+                               depth = TUN_V(tun);
+                               tx = (angle - uoffs) & 0x1f;
+                               ty = (depth + zoffs) & 0x1f;
                                pptop = tex[(ty << 5) + tx];
-                               tx = ~((angle >> 1) + uoffs) & 0x1f;
+                               tx = ~(angle + uoffs) & 0x1f;
                                ppbot = tex[(ty << 5) + tx];
 
-                               angle = (tun >> 16) & 0xff;
-                               depth = (tun >> 24) & 0xff;
-                               tx = ((angle >> 1) - uoffs) & 0x1f;
-                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               angle = TUN_U(tun >> 16);
+                               depth = TUN_V(tun >> 16);
+                               tx = (angle - uoffs) & 0x1f;
+                               ty = (depth + zoffs) & 0x1f;
                                pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
-                               tx = ~((angle >> 1) + uoffs) & 0x1f;
+                               tx = ~(angle + uoffs) & 0x1f;
                                ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
 
                                *top++ = pptop;
@@ -180,6 +181,8 @@ static void vblank(void)
        int16_t mat[4];
        static short gate_speed;
 
+       num_vbl++;
+
        bnstate = ~REG_KEYINPUT;
        if(bnstate & BN_DPAD) {
                if(gate_speed < 5) {
index f480758..d1ff400 100644 (file)
@@ -1,10 +1,11 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <stdint.h>
 #include <math.h>
 
 struct vec2 {
-       float x, y;
+       int x, y;
 };
 
 int main(int argc, char **argv)
@@ -89,6 +90,8 @@ invalopt:     fprintf(stderr, "invalid argument: %s\n", argv[i]);
        for(frm=0; frm<num_frames; frm++) {
                int coffs = num_frames > 1 ? frm * center / (num_frames - 1) : center;
 
+               memset(buf, 0xff, xsz * ysz * sizeof *buf);
+
 #define UDIV   2048
 #define VDIV   32768
                prev_r = 0.0f;
@@ -99,10 +102,10 @@ invalopt:  fprintf(stderr, "invalid argument: %s\n", argv[i]);
                        float z = v * coffs;
 
                        /* don't bother drawing rings < 1 pixel apart */
-                       if(fabs(r - prev_r) < 0.05) continue;
+                       if(r < 0 || fabs(r - prev_r) < 0.05) continue;
 
                        for(j=0; j<UDIV; j++) {
-                               float u = (float)j / (float)UDIV;
+                               float u = (float)j / (float)(UDIV - 1);
                                float theta = 2.0f * u * M_PI;
 
                                int x = (int)(cos(theta) * r - z) + xsz / 2;
@@ -110,8 +113,8 @@ invalopt:   fprintf(stderr, "invalid argument: %s\n", argv[i]);
 
                                if(x >= 0 && x < xsz && y >= 0 && y < ysz) {
                                        ptr = buf + y * xsz + x;
-                                       ptr->x = u;
-                                       ptr->y = v * 8;
+                                       ptr->x = (j << 8) / UDIV;
+                                       ptr->y = ((VDIV - i) << 11) / VDIV;
                                }
                        }
                        prev_r = r;
@@ -120,26 +123,22 @@ invalopt: fprintf(stderr, "invalid argument: %s\n", argv[i]);
                ptr = buf;
                for(i=0; i<out_nlines; i++) {
                        for(j=0; j<xsz; j++) {
-                               float u = ptr->x;
-                               float v = ptr->y;
-                               int r = (int)(u * 8.0 * 255.0f) & 0xff;
-                               int g = (int)(v * 8.0 * 255.0f) & 0xff;
-                               int b = (~(int)(v * 0.5 * 255.0f) & 0xff) + 105;
-                               if(b > 255) b = 255;
-                               if(b < 0) b = 0;
+                               int u = ptr->x;
+                               int v = ptr->y;
+                               int r = (u << 3) & 0xff;
+                               int g = (v >> 3) & 0xff;
 
                                /*if(v > 2.0) r = g = b = 0;*/
 
                                ptr++;
 
-                               uint16_t out = ((uint16_t)(u * 255.0f) & 0xff) |
-                                       (((uint16_t)(v * 255.0f) & 0xff) << 8);
+                               uint16_t out = ((uint16_t)u & 0x3f) | (((uint16_t)v & 0x3ff) << 6);
                                fwrite(&out, sizeof out, 1, stdout);
 
                                if(fp) {
                                        fputc(r, fp);
                                        fputc(g, fp);
-                                       fputc(b, fp);
+                                       fputc(0, fp);
                                }
                        }
                }