libMicrofx/tools/mapconv/src/main.c

102 lines
2.5 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <help.h>
void grabint(char *data, int size, int *start, char stop, int *out) {
int i;
*out=0;
for(i=0;*start<size-1;i++){
if(data[*start] == stop) break;
(*out) += data[*start] - 48;
if(data[(*start)+1] != stop) (*out) *= 10;
(*start)++;
}
(*start)++;
}
int main(int argc, char **argv) {
FILE *fp;
FILE *fp_map;
size_t size;
char tile[256];
int i, n, oldn, a, len;
int w, h;
if(argc < 4){
puts(help);
return 1;
}
char *data = NULL;
fp = fopen(argv[2], "rb");
if(fp == NULL){
printf("[mapconv] Can't open \"%s\"\n", argv[2]);
return 2;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
rewind(fp);
data = malloc(sizeof(unsigned char) * size);
if(data == NULL){
puts("[mapconv] More memory needed !");
return 3;
}
if(!fread(data, 1, sizeof(unsigned char) * size, fp)){
fclose(fp);
return;
}
fclose(fp);
/* Get the width and the height */
n=0;
grabint(data, size, &n, '\n', &w);
grabint(data, size, &n, '\n', &h);
oldn = n;
len = 0;
for(i=0;i<256 && n<size-1;i++){
if(data[n] == '\n') break;
grabint(data, size, &n, ' ', (int *)&tile[i]);
i++;
tile[i] = data[n];
n+=2;
len++;
}
free(data);
/* Make the map */
fp = fopen(argv[1], "rb");
if(fp == NULL){
printf("[mapconv] Can't open \"%s\"\n", argv[1]);
return 2;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
rewind(fp);
data = malloc(sizeof(unsigned char) * size);
if(data == NULL){
puts("[mapconv] More memory needed !");
return 3;
}
if(!fread(data, 1, sizeof(unsigned char) * size, fp)){
fclose(fp);
return;
}
fclose(fp);
fp_map = fopen(argv[3], "w");
fputs("#ifndef MAP_H\n#define MAP_H\n\n#include \"TILESET.h\"\n"
"#include <microfx/ext/gametools.h>\n\n"
"const unsigned char _map[] = {", fp_map);
for(i=0;i<size;i++){
if(data[i] != '\n'){
for(a=0;a<len;a++){
if(tile[a*2+1] == data[i]) fprintf(fp_map, "%d, ", tile[a*2]);
}
}
}
fseek(fp_map, -2L, SEEK_CUR);
fputs("};", fp_map);
fprintf(fp_map, "\n\nMMap map = {(unsigned char *)_map, (unsigned char **)"
"TILESET_HERE, %d, %d, TILEWIDTH_HERE, TILEHEIGHT_HERE, 0, 0};\n\n#endif\n",
w, h);
fclose(fp_map);
free(data);
return 0;
}