#include #include #include #include #include #include /* Created by Dhurdahl for the map system in AO 14.4 dhurdahl@ao-europe.com Version 1.1 MapRec size bug fixed. Version 1.1.1 recomp... Version 1.1.2 Text fixes... Version 1.3 Naming of map Version 2.0 Support for SL Version 2.1 Jpeg/PNG selection Version 2.5 5 map layers gcc -static -o cspmapcreator cspmapcreator.c -lgd -lpng -lm -lz */ /* CSP fixes/changes... - if a dimension is already a multiple of the texture_size, no pixels are added. - no jpeg support - no command line control */ /* "map3dnw.png" */ char *mapfile[] = {"map1.png", "map2.png", "map3.png", "map3rk1.png", "map3rk2.png", "map4.png"}; int mapfilecount = 6; int mapstructure[] = {1, 1, 3, 1}; int maplayercount = 4; int mapversioncount = 3; /* number of "dimensions" to map */ int indextomultiversion = 2; /* array index to start of multi-version map */ int maprect[4][4] = {{7, 0, 360, 457}, /* map rectangles */ {55, 0, 944, 1144}, {3, 0, 3561, 4582}, {3, 0, 3561, 4582}}; char *mapname = "CSPmap"; char *mapversion[] = {"", "-rk1", "-rk2", "-dnw"}; int texture_size = 128; int main (int argc, char **argv) { FILE *inputmapfile; gdImagePtr *im; /* array of image pointers */ int i, j, k, xt, yt, x, y, size, s; char *tmp; FILE *txt[4]; /* size should be mapversioncount */ FILE *sbin, *out; char foo; gdImagePtr *im_out; /* array for multi-version layers */ int *im_filepos; int filecount; printf("\nCSPmap compiler 1.0\n"); printf("This will compile "); for (i=0; i < (mapfilecount-1); i++) { printf("%s, ", mapfile[i]); } printf("and %s into a working map file.\n\n", mapfile[mapfilecount-1]); tmp = (char *)malloc(255); im = (gdImagePtr *)malloc(sizeof(gdImagePtr)*mapfilecount); im_out = (gdImagePtr *)malloc(sizeof(gdImagePtr)*mapversioncount); im_filepos = (int *)malloc(sizeof(int)*mapversioncount); size = 0; /* filepos counter */ if ( mkdir( mapname, S_IRWXU ) ) { printf("eeek map already exists.\n"); exit(1); } /* load all maps */ for (i=0; i < mapfilecount; i++) { printf("Loading %s.....\n", mapfile[i]); inputmapfile = fopen(mapfile[i], "rb"); if (! inputmapfile ) { printf("%s couldn't be openend\n", mapfile[i]); exit(1); } im[i] = gdImageCreateFromPng( inputmapfile ); if (! im[i] ) { printf("%s openend, but not read.\n", mapfile[i]); exit(1); } fclose( inputmapfile ); } /* open txt info files */ for (j=0; j < mapversioncount; j++) { sprintf(tmp, "%s/%s%s.txt", mapname, mapname, mapversion[j]); txt[j] = fopen(tmp, "wb"); if (! txt[j] ) { printf("%s could not be openend.\n", tmp); exit(1); } fprintf(txt[j], "Name \"%s%s\"\r\n", mapname, mapversion[j]); fprintf(txt[j], "Type Rubika\r\n" ); } /* open bin file */ sprintf(tmp, "%s/%s.bin", mapname, mapname); sbin = fopen(tmp, "wb"); filecount = 0; /* file count */ for (i=0; i < maplayercount; i++) {/* layer count */ printf("Splitting "); /* print all file names for this layer */ for (j=0; j < (mapstructure[i]-1); j++) printf("%s, ", mapfile[filecount+j]); printf("%s...\n", mapfile[filecount+j]); if (im[filecount]->sx % texture_size == 0) xt = im[filecount]->sx / texture_size; /* already a multiple of 128 */ else /* need to add some pixels here */ xt = (im[filecount]->sx - (im[filecount]->sx % texture_size)) / texture_size + 1; if (im[filecount]->sy % texture_size == 0) yt = im[filecount]->sy / texture_size; else yt = (im[filecount]->sy - (im[filecount]->sy % texture_size)) / texture_size + 1; for (j=0; j < mapversioncount; j++) { fprintf(txt[j], "\r\nFile %s/%s.bin\r\nTextureSize %d\r\n\r\n", mapname, mapname, texture_size); fprintf(txt[j], "Size %d %d\r\nTiles %d %d\r\n", xt * texture_size, yt * texture_size, xt, yt); fprintf(txt[j], "MapRect %d %d %d %d\r\n", maprect[i][0], maprect[i][1], maprect[i][2], maprect[i][3]); } x = y = 0; while ( x++ < xt ) { while ( y++ < yt ) { for (j=0; j < mapstructure[i]; j++) { im_out[j] = (gdImagePtr )gdImageCreateTrueColor (texture_size, texture_size); gdImageCopy(im_out[j], im[filecount+j], 0, 0, (x-1)*texture_size, (y-1)*texture_size, texture_size, texture_size); gdImageSaveAlpha(im_out[j], 0); for (k=0; k < j; k++) { if ( !(gdImageCompare(im_out[k], im_out[j]) & GD_CMP_IMAGE) ) { /* images are the same */ /* printf("found identical squares..."); */ break; } } /* end for */ if (k < j) { /* found identical image already written */ /* printf(" recording filepos %d...\n", im_filepos[k]); */ im_filepos[j] = im_filepos[k]; continue; } else { /* printf("no previous identical square, so adding at filepos %d...\n", size); */ im_filepos[j] = size; /* record filepos */ remove("maptmp"); /* deletes temp file if there (?) */ out = fopen( "maptmp", "wb"); if ( !out ) { printf("Could not open tmp file for writing; exiting...\n"); exit(1); } gdImagePng( im_out[j], out ); fclose (out); /* copy to bin file */ s = 0; out = fopen("maptmp", "rb"); while( !feof(out) ) { foo = fgetc( out ); fputc( foo, sbin); s++; } fclose(out); size += s; } /* end else */ } /* end for() */ for (j=0; j < mapstructure[i]; j++) gdImageDestroy (im_out[j]); if (mapstructure[i] == mapversioncount) for (j=0; j < mapversioncount; j++) fprintf(txt[j], "FilePos %d\r\n", im_filepos[j]); else for (j=0; j < mapversioncount; j++) fprintf(txt[j], "FilePos %d\r\n", im_filepos[0]); } y = 0; } for (j=0; j < mapstructure[i]; j++) { gdImageDestroy(im[filecount]); filecount++; } remove("maptmp"); } fclose(sbin); for (j=0; j < mapversioncount; j++) fclose(txt[j]); return 0; }