Advertisement
NovaYoshi

duplicate tile thing

Feb 5th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define TILE_SIZE 16
  5.  
  6. unsigned char *chr;
  7. FILE *output_file;
  8.  
  9. int tiles_equal(int tile1, int tile2) {
  10.   return !memcmp(&chr[tile1*TILE_SIZE], &chr[tile2*TILE_SIZE], TILE_SIZE);
  11. }
  12.  
  13. void write_tile(int tile) {
  14.   int i;
  15.   for(i=0; i<TILE_SIZE; i++)
  16.     fputc(chr[tile*TILE_SIZE+i], output_file);
  17. }
  18.  
  19. int main(int argc, char *argv[]) {
  20.   int i, j;
  21.   int cur_tile;
  22.   const char *input_name = NULL;
  23.   const char *output_name = NULL;
  24.   int bank_size = 64;
  25.   int written_tiles = 0;
  26.  
  27.   // parse parameters
  28.   for(i=1; i<(argc-1); i++) {
  29.     if(!strcmp(argv[i], "-i"))
  30.       input_name = argv[i+1];
  31.     if(!strcmp(argv[i], "-o"))
  32.       output_name = argv[i+1];
  33.     if(!strcmp(argv[i], "-banksize"))
  34.       bank_size = atoi(argv[i+1]);
  35.   }
  36.  
  37.   if(!input_name || !output_name) {
  38.     puts("Must specify input and output files");
  39.     return -1;
  40.   }
  41.  
  42.   // open the input CHR
  43.   FILE *input_file = fopen(input_name,"rb");
  44.   if(!input_file) {
  45.     fputs("Failed to open input file", stderr);
  46.     return -1;
  47.   }
  48.   fseek(input_file, 0, SEEK_END);
  49.   long file_size = ftell(input_file);
  50.   rewind(input_file);
  51.  
  52.   chr = (char*)malloc(sizeof(char)*file_size);
  53.   if(chr == NULL) {
  54.     fputs("Failed to allocate space for the input file", stderr);
  55.     return -1;
  56.   }
  57.   if(file_size != fread(chr, 1, file_size, input_file)) {
  58.     fputs("Failed to read the input file", stderr);
  59.     return -1;
  60.   }
  61.   fclose(input_file);
  62.  
  63.   // open output file
  64.   output_file = fopen(output_name, "wb");
  65.   for(cur_tile = 0; cur_tile < file_size/TILE_SIZE; cur_tile++) {
  66.     int already_written = 0;
  67.     for(i=cur_tile-1; i>=0; i--) {
  68.       if(tiles_equal(i, cur_tile)) {
  69.         already_written = 1;
  70.         break;
  71.       }
  72.     }
  73.     if(!already_written)
  74.       write_tile(cur_tile);
  75.   }
  76.   fclose(output_file);
  77.  
  78.   free(chr);
  79.   return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement