Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define TILE_SIZE 16
- unsigned char *chr;
- FILE *output_file;
- int tiles_equal(int tile1, int tile2) {
- return !memcmp(&chr[tile1*TILE_SIZE], &chr[tile2*TILE_SIZE], TILE_SIZE);
- }
- void write_tile(int tile) {
- int i;
- for(i=0; i<TILE_SIZE; i++)
- fputc(chr[tile*TILE_SIZE+i], output_file);
- }
- int main(int argc, char *argv[]) {
- int i, j;
- int cur_tile;
- const char *input_name = NULL;
- const char *output_name = NULL;
- int bank_size = 64;
- int written_tiles = 0;
- // parse parameters
- for(i=1; i<(argc-1); i++) {
- if(!strcmp(argv[i], "-i"))
- input_name = argv[i+1];
- if(!strcmp(argv[i], "-o"))
- output_name = argv[i+1];
- if(!strcmp(argv[i], "-banksize"))
- bank_size = atoi(argv[i+1]);
- }
- if(!input_name || !output_name) {
- puts("Must specify input and output files");
- return -1;
- }
- // open the input CHR
- FILE *input_file = fopen(input_name,"rb");
- if(!input_file) {
- fputs("Failed to open input file", stderr);
- return -1;
- }
- fseek(input_file, 0, SEEK_END);
- long file_size = ftell(input_file);
- rewind(input_file);
- chr = (char*)malloc(sizeof(char)*file_size);
- if(chr == NULL) {
- fputs("Failed to allocate space for the input file", stderr);
- return -1;
- }
- if(file_size != fread(chr, 1, file_size, input_file)) {
- fputs("Failed to read the input file", stderr);
- return -1;
- }
- fclose(input_file);
- // open output file
- output_file = fopen(output_name, "wb");
- for(cur_tile = 0; cur_tile < file_size/TILE_SIZE; cur_tile++) {
- int already_written = 0;
- for(i=cur_tile-1; i>=0; i--) {
- if(tiles_equal(i, cur_tile)) {
- already_written = 1;
- break;
- }
- }
- if(!already_written)
- write_tile(cur_tile);
- }
- fclose(output_file);
- free(chr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement