Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // For fputs/fprintf
- #include <stdlib.h> // For EXIT_FAILURE/EXIT_SUCCESS
- #include <sys/types.h> // For stat()
- #include <sys/stat.h> // For stat()
- #include <stdint.h> // For uint8_t
- #if defined(_WIN32)
- #include <direct.h> // For _mkdir
- #endif
- void print_usage() {
- fputs("Usage: <program> <rom> <output directory>", stderr);
- }
- int main(int argc, char** argv) {
- if (argc != 3) {
- print_usage();
- return EXIT_FAILURE;
- }
- FILE* rom = fopen(argv[1], "rb");
- if (rom == NULL) {
- fprintf(stderr, "Error: Couldn't open ROM file \"%s\"", argv[1]);
- print_usage();
- return EXIT_FAILURE;
- }
- fseek(rom, 0, SEEK_END);
- size_t rom_size = ftell(rom);
- fseek(rom, 0, SEEK_SET);
- uint8_t data[rom_size];
- fread(&data, sizeof(uint8_t), rom_size, rom);
- fclose(rom);
- // Not sure why I have to minus 64 here, but without it, the top row
- // of tiles is cut off in the output
- const int maps_begin = 0x1AA8E - 64;
- const int map_width = 64;
- const int map_height = 64;
- const int map_size = map_width * map_height;
- static const char* filenames[4] = {
- "continentia.bin",
- "forestria.bin",
- "archipelia.bin",
- "saharia.bin"
- };
- for (int i = 0; i < 4; ++i) {
- int start_offset = maps_begin + (i * map_size);
- uint8_t flipped_vertically[map_size];
- for (int j = 0; j < map_size; ++j) {
- // Break down index
- int x = j % map_width;
- int y = j / map_width;
- // Invert y
- y = map_height - y;
- // Recreate index
- int index = (y * map_width) + x;
- flipped_vertically[j] = data[start_offset + index];
- }
- char outfile_path[256];
- #if defined(_WIN32)
- _mkdir(argv[2]);
- #else
- mkdir(argv[2], 0700);
- #endif
- if (snprintf(outfile_path, 256, "%s/%s", argv[2], filenames[i]) > 256) {
- fputs("Error: Output path too large", stderr);
- return EXIT_FAILURE;
- }
- // Write flipped data to file
- FILE* map_out = fopen(outfile_path, "wb");
- fwrite(flipped_vertically, sizeof(uint8_t), map_size, map_out);
- fclose(map_out);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment