snes: make pcx_to_compressed_tilemap_8bpp.c generic

It should now work for 4bpp tiles too
This commit is contained in:
Vince Weaver 2013-01-22 15:30:19 -05:00
parent 7e6e1203a2
commit d798e031eb
3 changed files with 58 additions and 90 deletions

View File

@ -11,7 +11,7 @@ tb1_snes.sfc: tb1_snes.o snes-hirom.cfg
tb1_snes.o: tb1_snes.s \
snes_init.s opening.s title_screen.s level_1.s \
svmwgraph.s \
tb1_title.tiles tb1_opening.tiles \
tb1_title.tiles tb1_opening.tiles level1_background.tiles \
level1_pal0.sprites \
tbfont.inc
$(AS) -t none -o tb1_snes.o -l tb1_snes.lst -c tb1_snes.s
@ -20,11 +20,14 @@ tb1_snes.o: tb1_snes.s \
level1_pal0.sprites: graphics/level1_pal0.pcx tools/pcx_to_tiles_4bpp
./tools/pcx_to_tiles_4bpp level1_pal0 < graphics/level1_pal0.pcx > level1_pal0.sprites
level1_background.tiles: graphics/level1_background.pcx ./tools/pcx_to_compressed_tilemap
./tools/pcx_to_compressed_tilemap level1_background 4 < graphics/level1_background.pcx > level1_background.tiles
tb1_opening.tiles: graphics/vmw_logo.pcx ./tools/pcx_to_tiles_8bpp
./tools/pcx_to_tiles_8bpp vmw_logo < graphics/vmw_logo.pcx > tb1_opening.tiles
tb1_title.tiles: graphics/tb1_title.pcx ./tools/pcx_to_compressed_tilemap_8bpp
./tools/pcx_to_compressed_tilemap_8bpp title_screen < graphics/tb1_title.pcx > tb1_title.tiles
tb1_title.tiles: graphics/tb1_title.pcx ./tools/pcx_to_compressed_tilemap
./tools/pcx_to_compressed_tilemap title_screen 8 < graphics/tb1_title.pcx > tb1_title.tiles
checksum.inc: ./tools/snes_checksum tb1_snes.sfc
./tools/snes_checksum < tb1_snes.sfc > checksum.inc

View File

@ -5,7 +5,7 @@ LFLAGS =
all: bin2byte color_convert convert_font convert_font_bin \
dump_font make_pal \
pcx_to_tiles_4bpp pcx_to_tiles_8bpp \
pcx_to_compressed_tilemap_8bpp \
pcx_to_compressed_tilemap \
ansi_to_tile string_to_bytes convert_font_4bpp \
snes_checksum
@ -80,11 +80,11 @@ pcx_to_tiles_8bpp.o: pcx_to_tiles_8bpp.c
$(CC) $(CFLAGS) -c pcx_to_tiles_8bpp.c
pcx_to_compressed_tilemp_8bpp: pcx_to_compressed_tilemap_8bpp.o
$(CC) $(LFLAGS) -o pcx_to_compressed_tilemap_8bpp pcx_to_compressed_tilemap_8bpp.o
pcx_to_compressed_tilemap: pcx_to_compressed_tilemap.o
$(CC) $(LFLAGS) -o pcx_to_compressed_tilemap pcx_to_compressed_tilemap.o
pcx_to_compressed_tilemap_8bpp.o: pcx_to_compressed_tilemap_8bpp.c
$(CC) $(CFLAGS) -c pcx_to_compressed_tilemap_8bpp.c
pcx_to_compressed_tilemap.o: pcx_to_compressed_tilemap.c
$(CC) $(CFLAGS) -c pcx_to_compressed_tilemap.c
@ -106,7 +106,7 @@ clean:
rm -f *.o *~ bin2byte color_convert convert_font \
convert_font_bin dump_font make_pal \
pcx_to_tiles_4bpp pcx_to_tiles_8bpp \
pcx_to_compressed_tilemap_8bpp \
pcx_to_compressed_tilemap \
ansi_to_tile string_to_bytes \
convert_font_4bpp snes_checksum

View File

@ -1,12 +1,15 @@
/* Converts a 24-bit PCX file to 256-color SNES background tiles */
/* Converts a 24-bit PCX file to SNES background tiles */
/* It is "compressed", that is it detects identical tiles */
/* instead of using just a plain linear tilemap */
#include <stdio.h> /* For FILE I/O */
#include <string.h> /* For strncmp */
#include <fcntl.h> /* for open() */
#include <unistd.h> /* for lseek() */
#include <stdio.h> /* For FILE I/O */
#include <string.h> /* For strncmp */
#include <fcntl.h> /* for open() */
#include <unistd.h> /* for lseek() */
#include <sys/stat.h> /* for file modes */
#include <stdlib.h> /* exit() */
#include <stdlib.h> /* exit() */
/* Horizontally flip the bits in a byte */
static unsigned int hflip_byte(unsigned int byte) {
int new_byte=0,i;
@ -36,14 +39,17 @@ static char symbol_name[BUFSIZ]="temp";
#define MAX_TILE_X 32
#define MAX_TILE_Y 32
#define MAX_PLANES_DIV2 4
#define Y_SIZE 8
static unsigned short tilemap[MAX_TILE_X*MAX_TILE_Y]; /* 2k */
static unsigned short tiledata[MAX_TILE_X*MAX_TILE_Y][8][4];
static unsigned short tiledata_hflip[MAX_TILE_X*MAX_TILE_Y][8][4];
static unsigned short temp_tile[8][4];
static unsigned short tiledata[MAX_TILE_X*MAX_TILE_Y][Y_SIZE][MAX_PLANES_DIV2];
static unsigned short
tiledata_hflip[MAX_TILE_X*MAX_TILE_Y][Y_SIZE][MAX_PLANES_DIV2];
static unsigned short temp_tile[Y_SIZE][MAX_PLANES_DIV2];
static int total_tiles=0;
static int compressed_tiles=0;
static int max_planes=8;
/* File already open */
@ -146,83 +152,38 @@ static int vmwLoadPCX(int pcx_fd) {
#define Y_CHUNKSIZE 8
unsigned int plane0,plane1,plane2,plane3;
unsigned int plane4,plane5,plane6,plane7,offset;
unsigned int plane0,plane1,offset;
int ychunk,xchunk;
printf("%s_tile_data:\n",symbol_name);
int ychunk,xchunk;
for(ychunk=0;ychunk<ysize/Y_CHUNKSIZE;ychunk++) {
for(xchunk=0;xchunk<xsize/X_CHUNKSIZE;xchunk++) {
// printf("\t; Tile %d %d, Plane 0 Plane 1\n",xchunk,ychunk);
for(y=0;y<Y_CHUNKSIZE;y++){
plane0=0;plane1=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane0<<=1;
plane1<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane0|=(output[offset])&1;
plane1|=(((output[offset])&2)>>1);
}
temp_tile[y][0]=(plane1<<8)|plane0;
// printf("\t.word $%02x%02x\n",plane1,plane0);
}
// printf("\t; Plane 2 Plane 3\n");
for(y=0;y<Y_CHUNKSIZE;y++){
plane2=0;plane3=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane2<<=1;
plane3<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane2|=(((output[offset])&4)>>2);
plane3|=(((output[offset])&8)>>3);
}
temp_tile[y][1]=(plane3<<8)|plane2;
// printf("\t.word $%02x%02x\n",plane3,plane2);
}
// printf("\t; Plane 4 Plane 5\n");
for(y=0;y<Y_CHUNKSIZE;y++){
plane4=0;plane5=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane4<<=1;
plane5<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane4|=(((output[offset])&16)>>4);
plane5|=(((output[offset])&32)>>5);
}
temp_tile[y][2]=(plane5<<8)|plane4;
// printf("\t.word $%02x%02x\n",plane5,plane4);
}
// printf("\t; Plane 6 Plane 7\n");
for(y=0;y<Y_CHUNKSIZE;y++){
plane6=0;plane7=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane6<<=1;
plane7<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane6|=(((output[offset])&64)>>6);
plane7|=(((output[offset])&128)>>7);
}
temp_tile[y][3]=(plane7<<8)|plane6;
// printf("\t.word $%02x%02x\n",plane7,plane6);
}
for(i=0;i<max_planes/2;i++) {
for(y=0;y<Y_CHUNKSIZE;y++){
plane0=0;plane1=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane0<<=1;
plane1<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane=i*2;
plane0|=(((output[offset])&(1<<(plane))) >> plane);
plane1|=(((output[offset])&(1<<(plane+1)) )>>(plane+1));
}
temp_tile[y][i]=(plane1<<8)|plane0;
}
}
int i,plane,v=0,h=0,o=0,pal=0,found_tile=0,found=0,match;
int v=0,h=0,o=0,pal=0,found_tile=0,found=0,match;
/* see if the new tile matches an existing one */
for(i=0;i<compressed_tiles;i++) {
/* see if matches direct */
match=1;
for(plane=0;plane<4;plane++) {
for(plane=0;plane<max_planes/2;plane++) {
for(y=0;y<8;y++) {
if (temp_tile[y][plane]!=tiledata[i][y][plane]) match=0;
}
@ -236,7 +197,7 @@ static int vmwLoadPCX(int pcx_fd) {
/* see if matches with vflip */
match=1;
for(plane=0;plane<4;plane++) {
for(plane=0;plane<max_planes/2;plane++) {
for(y=0;y<8;y++) {
if (temp_tile[y][plane]!=tiledata[i][7-y][plane]) match=0;
}
@ -250,7 +211,7 @@ static int vmwLoadPCX(int pcx_fd) {
/* see if matches with hflip */
match=1;
for(plane=0;plane<4;plane++) {
for(plane=0;plane<max_planes/2;plane++) {
for(y=0;y<8;y++) {
if (temp_tile[y][plane]!=tiledata_hflip[i][y][plane]) match=0;
}
@ -263,7 +224,7 @@ static int vmwLoadPCX(int pcx_fd) {
}
/* see if matches with hflip and vflip */
match=1;
for(plane=0;plane<4;plane++) {
for(plane=0;plane<max_planes/2;plane++) {
for(y=0;y<8;y++) {
if (temp_tile[y][plane]!=tiledata_hflip[i][7-y][plane]) match=0;
}
@ -280,7 +241,7 @@ static int vmwLoadPCX(int pcx_fd) {
if (!found) {
/* print tile data */
for(x=0;x<4;x++) {
for(x=0;x<max_planes/2;x++) {
printf("\t; Tile %d %d, Plane %d Plane %d\n",xchunk,ychunk,
x*2,(x*2)+1);
for(y=0;y<Y_CHUNKSIZE;y++) {
@ -290,13 +251,13 @@ static int vmwLoadPCX(int pcx_fd) {
found_tile=compressed_tiles;
/* put data in lookup table */
for(plane=0;plane<4;plane++) {
for(plane=0;plane<max_planes/2;plane++) {
for(y=0;y<8;y++) {
tiledata[compressed_tiles][y][plane]=temp_tile[y][plane];
}
}
/* put hflip in lookup table */
for(plane=0;plane<4;plane++) {
for(plane=0;plane<max_planes/2;plane++) {
for(y=0;y<8;y++) {
tiledata_hflip[compressed_tiles][y][plane]=
(hflip_byte( (temp_tile[y][plane]>>8)&0xff)<<8) |
@ -351,7 +312,7 @@ static int vmwLoadPCX(int pcx_fd) {
}
else {
int r,g,b;
for(i=0;i<256;i++) {
for(i=0;i<(1<<max_planes);i++) {
read(pcx_fd,&temp_byte,1);
r=temp_byte;
read(pcx_fd,&temp_byte,1);
@ -374,6 +335,10 @@ int main(int argc, char **argv) {
strncpy(symbol_name,argv[1],BUFSIZ);
}
if (argc>2) {
max_planes=atoi(argv[2]);
}
/* read from stdin */
result=vmwLoadPCX(fileno(stdin));