conv_sprite tool
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Thu, 26 Jul 2018 16:35:16 +0000 (19:35 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Thu, 26 Jul 2018 16:35:16 +0000 (19:35 +0300)
tools/conv_sprite [new file with mode: 0755]
tools/conv_sprite.c [new file with mode: 0644]

diff --git a/tools/conv_sprite b/tools/conv_sprite
new file mode 100755 (executable)
index 0000000..c05deb7
--- /dev/null
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+if [ -z "$1" ]; then
+       echo "pass the sprite header file as argument"
+       exit 1
+fi
+
+cc -pedantic -Wall -DHDRFILE=\"$1\" -o /tmp/conv_sprite.bin conv_sprite.c
+if [ $? != 0 ]; then
+       exit 1
+fi
+
+/tmp/conv_sprite.bin
diff --git a/tools/conv_sprite.c b/tools/conv_sprite.c
new file mode 100644 (file)
index 0000000..ff50a6a
--- /dev/null
@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#include HDRFILE
+
+#define MAX_SPRITE_WIDTH       16
+
+static void conv_sprite(int slice, unsigned char *img, int width, int height, int pitch);
+static void conv_img_data(int sub, uint16_t *out, unsigned char *img, int width,
+               int height, int pitch);
+
+int main(int argc, char **argv)
+{
+       int i, nslices;
+
+       nslices = (width + MAX_SPRITE_WIDTH - 1) / MAX_SPRITE_WIDTH;
+       fprintf(stderr, "source image: %dx%d. splitting into %d slices\n",
+                       width, height, nslices);
+
+       printf("\t.data\n\n");
+
+       for(i=0; i<nslices; i++) {
+               int xstart = i * MAX_SPRITE_WIDTH;
+               int spr_w = width - xstart;
+               if(spr_w > MAX_SPRITE_WIDTH) spr_w = MAX_SPRITE_WIDTH;
+
+               conv_sprite(i, header_data + xstart, spr_w, height, width);
+       }
+
+       printf("\n\t.global sprpal\n");
+       printf("sprpal:\n");
+       for(i=0; i<16; i++) {
+               int r = header_data_cmap[i][0] >> 4;
+               int g = header_data_cmap[i][1] >> 4;
+               int b = header_data_cmap[i][2] >> 4;
+               unsigned int col = ((r & 0xf) << 8) | ((g & 0xf) << 4) | (b & 0xf);
+               printf("\t.short 0x%03x\n", col);
+       }
+}
+
+static void conv_sprite(int slice, unsigned char *img, int width, int height, int pitch)
+{
+       int i, j;
+       uint16_t *sprdata;
+
+       for(i=0; i<2; i++) {
+               printf("\n\t.global spr%d%c\n", slice, i == 0 ? 'a' : 'b');
+               printf("spr%d%c:\n", slice, i == 0 ? 'a' : 'b');
+               printf("\t.short 0\n");         /* position x/y */
+               printf("\t.short 0\n");         /* vstop */
+
+               sprdata = malloc(height * 2 * sizeof *sprdata);
+               conv_img_data(i, sprdata, img, width, height, pitch);
+
+               for(j=0; j<height; j++) {
+                       printf("\t.short 0x%x\n", (unsigned int)sprdata[j]);
+               }
+
+               printf("\t.long 0\n");  /* end of sprite CW */
+               free(sprdata);
+       }
+}
+
+static void conv_img_data(int sub, uint16_t *out, unsigned char *img, int width,
+               int height, int pitch)
+{
+       int i, j, inshift;
+       uint16_t bit0, bit1;
+
+       /* sprite 0: low order bits, sprite 1: high order bits */
+       inshift = sub == 0 ? 0 : 2;
+
+       for(i=0; i<height; i++) {
+               bit0 = bit1 = 0;
+               for(j=0; j<width; j++) {
+                       bit0 = (bit0 << 1) | ((img[j] >> inshift) & 1);
+                       bit1 = (bit1 << 1) | ((img[j] >> (inshift + 1)) & 1);
+               }
+               if(width < MAX_SPRITE_WIDTH) {
+                       bit0 <<= MAX_SPRITE_WIDTH - width;
+                       bit1 <<= MAX_SPRITE_WIDTH - width;
+               }
+               *out++ = bit0;
+               *out++ = bit1;
+               img += pitch;
+       }
+}
+