tunnel shading
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 12 May 2021 09:44:57 +0000 (12:44 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 12 May 2021 09:44:57 +0000 (12:44 +0300)
Makefile
src/data.h
src/data.s
src/gamescr.c
tools/pngdump/main.c
tools/tungen.c

index 68b1f63..47632ea 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ name = gbajam21
 elf = $(name).elf
 bin = $(name).gba
 
-data = data/tuncross.raw data/tuncross.pal \
+data = data/tuncross.sraw data/tuncross.spal data/tuncross.shade \
           data/spr_game.raw data/spr_game.pal \
           data/tun.map
 
@@ -21,7 +21,6 @@ CC = $(TCPREFIX)gcc
 AS = $(TCPREFIX)as
 OBJCOPY = $(TCPREFIX)objcopy
 OBJDUMP = $(TCPREFIX)objdump
-EMU = vbam
 
 opt = -O3 -fomit-frame-pointer -mcpu=arm7tdmi -mtune=arm7tdmi -mthumb -mthumb-interwork
 dbg = -g
@@ -30,7 +29,6 @@ inc = -I. -Ilibs/maxmod
 CFLAGS = $(opt) $(dbg) -pedantic -Wall -MMD $(def) $(inc)
 ASFLAGS = -mthumb-interwork
 LDFLAGS = -mthumb -mthumb-interwork $(libs)
-EMUFLAGS = -T 100 -f 1 --agb-print
 
 -include cfg.mk
 
@@ -63,6 +61,9 @@ tools/mmutil/mmutil:
 #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 $^
 
+%.sraw: %.png tools/pngdump/pngdump
+       tools/pngdump/pngdump -o $@ -oc $(subst .sraw,.spal,$@) -os $(subst .sraw,.shade,$@) -s 8 $<
+
 %.raw: %.png tools/pngdump/pngdump
        tools/pngdump/pngdump -o $@ -n $<
 
index 92a43cb..0ba4709 100644 (file)
@@ -9,6 +9,7 @@
 
 extern unsigned char tuncross_pixels[];
 extern unsigned char tuncross_cmap[];
+extern unsigned char tuncross_shade[];
 
 extern uint32_t tunmap[];
 
index d99f695..7bfa602 100644 (file)
@@ -2,6 +2,7 @@
 
        .globl tuncross_pixels
        .globl tuncross_cmap
+       .globl tuncross_shade
        .globl tunmap
        .globl spr_game_pixels
        .globl spr_game_cmap
 
        .align 1
 tuncross_pixels:
-       .incbin "data/tuncross.raw"
+       .incbin "data/tuncross.sraw"
        .align 1
 tuncross_cmap:
-       .incbin "data/tuncross.pal"
+       .incbin "data/tuncross.spal"
+       .align 1
+tuncross_shade:
+       .incbin "data/tuncross.shade"
 
        .align 2
 tunmap: .incbin "data/tun.map"
index 23a8e21..5305c8b 100644 (file)
@@ -92,11 +92,13 @@ void gamescr(void)
 
 #define TUN_U(x)       ((x) & 0x3f)
 #define TUN_V(x)       (((x) >> 6) & 0x3ff)
+#define TEXEL(x, y, lvl) \
+       tuncross_shade[((int)tex[((y) << 5) + (x)] << 3) + (lvl)]
 
 __attribute__((noinline, target("arm"), section(".iwram")))
 static void draw_tunnel(void)
 {
-       int i, j, tx, ty, u, v, angle, depth, zoffs, uoffs, flip, tunturn;
+       int i, j, tx, ty, u, v, angle, depth, zoffs, uoffs, flip, tunturn, shade;
        static int tunsweep;
        uint16_t pptop, ppbot;
        uint16_t *top, *bot;
@@ -125,19 +127,21 @@ static void draw_tunnel(void)
 
                                angle = TUN_U(tun >> 16);
                                depth = TUN_V(tun >> 16);
+                               shade = depth >> 7;
                                tx = ~(angle - uoffs) & 0x1f;
                                ty = ((depth >> 1) + zoffs) & 0x1f;
-                               pptop = tex[(ty << 5) + tx];
+                               pptop = TEXEL(tx, ty, shade);
                                tx = (angle + uoffs) & 0x1f;
-                               ppbot = tex[(ty << 5) + tx];
+                               ppbot = TEXEL(tx, ty, shade);
 
                                angle = TUN_U(tun);
                                depth = TUN_V(tun);
+                               shade = depth >> 7;
                                tx = ~(angle - uoffs) & 0x1f;
                                ty = ((depth >> 1) + zoffs) & 0x1f;
-                               pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
+                               pptop |= (uint16_t)TEXEL(tx, ty, shade) << 8;
                                tx = (angle + uoffs) & 0x1f;
-                               ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
+                               ppbot |= (uint16_t)TEXEL(tx, ty, shade) << 8;
 
                                *top++ = pptop;
                                *bot++ = ppbot;
@@ -152,19 +156,21 @@ static void draw_tunnel(void)
 
                                angle = TUN_U(tun);
                                depth = TUN_V(tun);
+                               shade = depth >> 7;
                                tx = (angle - uoffs) & 0x1f;
                                ty = ((depth >> 1) + zoffs) & 0x1f;
-                               pptop = tex[(ty << 5) + tx];
+                               pptop = TEXEL(tx, ty, shade);
                                tx = ~(angle + uoffs) & 0x1f;
-                               ppbot = tex[(ty << 5) + tx];
+                               ppbot = TEXEL(tx, ty, shade);
 
                                angle = TUN_U(tun >> 16);
                                depth = TUN_V(tun >> 16);
+                               shade = depth >> 7;
                                tx = (angle - uoffs) & 0x1f;
                                ty = ((depth >> 1) + zoffs) & 0x1f;
-                               pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
+                               pptop |= (uint16_t)TEXEL(tx, ty, shade) << 8;
                                tx = ~(angle + uoffs) & 0x1f;
-                               ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
+                               ppbot |= (uint16_t)TEXEL(tx, ty, shade) << 8;
 
                                *top++ = pptop;
                                *bot++ = ppbot;
index edbbba5..71bcd7c 100644 (file)
@@ -12,6 +12,7 @@ enum {
        MODE_INFO
 };
 
+void dump_colormap(struct image *img, int text, FILE *fp);
 void print_usage(const char *argv0);
 
 int main(int argc, char **argv)
@@ -20,16 +21,17 @@ int main(int argc, char **argv)
        int text = 0;
        int renibble = 0;
        char *outfname = 0;
-       char *slut_fname = 0;
+       char *slut_fname = 0, *cmap_fname = 0;
        char *infiles[256];
        int num_infiles = 0;
        struct image img, tmpimg;
        FILE *out = stdout;
-       FILE *slut_out = 0;
+       FILE *aux_out;
        int *shade_lut = 0;
        int *lutptr;
        int shade_levels = 8;
        int maxcol = 0;
+       int lvl;
 
        for(i=1; i<argc; i++) {
                if(argv[i][0] == '-') {
@@ -81,14 +83,6 @@ int main(int argc, char **argv)
                                        outfname = argv[i];
                                        break;
 
-                               case 'S':
-                                       if(!argv[++i]) {
-                                               fprintf(stderr, "-S must be followed by a filename\n");
-                                               return 1;
-                                       }
-                                       slut_fname = argv[i];
-                                       break;
-
                                case 'h':
                                        print_usage(argv[0]);
                                        return 0;
@@ -99,9 +93,25 @@ int main(int argc, char **argv)
                                        return 1;
                                }
                        } else {
-                               fprintf(stderr, "invalid option: %s\n", argv[i]);
-                               print_usage(argv[0]);
-                               return 1;
+                               if(strcmp(argv[i], "-oc") == 0) {
+                                       if(!argv[++i]) {
+                                               fprintf(stderr, "-oc must be followed by a filename\n");
+                                               return 1;
+                                       }
+                                       cmap_fname = argv[i];
+
+                               } else if(strcmp(argv[i], "-os") == 0) {
+                                       if(!argv[++i]) {
+                                               fprintf(stderr, "-os must be followed by a filename\n");
+                                               return 1;
+                                       }
+                                       slut_fname = argv[i];
+
+                               } else {
+                                       fprintf(stderr, "invalid option: %s\n", argv[i]);
+                                       print_usage(argv[0]);
+                                       return 1;
+                               }
                        }
                } else {
                        infiles[num_infiles++] = argv[i];
@@ -142,7 +152,7 @@ int main(int argc, char **argv)
                        fprintf(stderr, "shading LUT generation is only supported for indexed color images\n");
                        return 1;
                }
-               if(!(slut_out = fopen(slut_fname, "wb"))) {
+               if(!(aux_out = fopen(slut_fname, "wb"))) {
                        fprintf(stderr, "failed to open shading LUT output file: %s: %s\n", slut_fname, strerror(errno));
                        return 1;
                }
@@ -159,14 +169,16 @@ int main(int argc, char **argv)
                lutptr = shade_lut;
                for(i=0; i<maxcol; i++) {
                        for(j=0; j<shade_levels; j++) {
+                               lvl = lutptr[shade_levels - j - 1];
                                if(text) {
-                                       fprintf(slut_out, "%d%c", *lutptr++, j < shade_levels - 1 ? ' ' : '\n');
+                                       fprintf(aux_out, "%d%c", lvl, j < shade_levels - 1 ? ' ' : '\n');
                                } else {
-                                       fputc(*lutptr++, slut_out);
+                                       fputc(lvl, aux_out);
                                }
                        }
+                       lutptr += shade_levels;
                }
-               fclose(slut_out);
+               fclose(aux_out);
 
        } else if(maxcol) {
                /* perform any color reductions if requested */
@@ -177,6 +189,19 @@ int main(int argc, char **argv)
                quantize_image(&img, maxcol);
        }
 
+       if(cmap_fname) {
+               if(img.bpp > 8) {
+                       fprintf(stderr, "colormap output works only for indexed color images\n");
+                       return 1;
+               }
+               if(!(aux_out = fopen(cmap_fname, "wb"))) {
+                       fprintf(stderr, "failed to open colormap output file: %s: %s\n", cmap_fname, strerror(errno));
+                       return 1;
+               }
+               dump_colormap(&img, text, aux_out);
+               fclose(aux_out);
+       }
+
        if(img.bpp == 4 && renibble) {
                unsigned char *ptr = img.pixels;
                for(i=0; i<img.width * img.height; i++) {
@@ -202,14 +227,7 @@ int main(int argc, char **argv)
                break;
 
        case MODE_CMAP:
-               if(text) {
-                       for(i=0; i<img.cmap_ncolors; i++) {
-                               printf("%d %d %d\n", img.cmap[i].r, img.cmap[i].g, img.cmap[i].b);
-                       }
-               } else {
-                       /*fwrite(img.cmap, sizeof img.cmap[0], img.cmap_ncolors, out);*/
-                       fwrite(img.cmap, sizeof img.cmap[0], 1 << img.bpp, out);
-               }
+               dump_colormap(&img, text, out);
                break;
 
        case MODE_INFO:
@@ -228,17 +246,31 @@ int main(int argc, char **argv)
        return 0;
 }
 
+void dump_colormap(struct image *img, int text, FILE *fp)
+{
+       int i;
+
+       if(text) {
+               for(i=0; i<img->cmap_ncolors; i++) {
+                       fprintf(fp, "%d %d %d\n", img->cmap[i].r, img->cmap[i].g, img->cmap[i].b);
+               }
+       } else {
+               fwrite(img->cmap, sizeof img->cmap[0], 1 << img->bpp, fp);
+       }
+}
+
 void print_usage(const char *argv0)
 {
        printf("Usage: %s [options] <input file>\n", argv0);
        printf("Options:\n");
        printf(" -o <output file>: specify output file (default: stdout)\n");
+       printf(" -oc <cmap file>: output colormap to separate file\n");
+       printf(" -os <lut file>: generate and output shading LUT\n");
        printf(" -p: dump pixels (default)\n");
        printf(" -P: output in PNG format\n");
        printf(" -c: dump colormap (palette) entries\n");
        printf(" -C <colors>: reduce image down to specified number of colors\n");
-       printf(" -S <lut file>: generate and output shading LUT\n");
-       printf(" -s <shade levels>: used in conjunction with -C or -S (default: 8)\n");
+       printf(" -s <shade levels>: used in conjunction with -os (default: 8)\n");
        printf(" -i: print image information\n");
        printf(" -t: output as text when possible\n");
        printf(" -n: swap the order of nibbles (for 4bpp)\n");
index d1ff400..f2485c3 100644 (file)
@@ -114,7 +114,7 @@ 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 = (j << 8) / UDIV;
-                                       ptr->y = ((VDIV - i) << 11) / VDIV;
+                                       ptr->y = ((VDIV - i - 1) << 10) / VDIV;
                                }
                        }
                        prev_r = r;
@@ -125,8 +125,8 @@ invalopt:   fprintf(stderr, "invalid argument: %s\n", argv[i]);
                        for(j=0; j<xsz; j++) {
                                int u = ptr->x;
                                int v = ptr->y;
-                               int r = (u << 3) & 0xff;
-                               int g = (v >> 3) & 0xff;
+                               int r = u & 0x3f;
+                               int g = (v >> 2) & 0xff;
 
                                /*if(v > 2.0) r = g = b = 0;*/