git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit 6eb0fe6726debf6e53a9a5cd36cf33b970c0a850
parent 2cb321c9218caf122f4cd85a33949c22e0b9e404
Author: bbbbbr <bbbbbr@users.noreply.github.com>
Date:   Fri, 30 Sep 2022 20:59:19 -0700

Merge pull request #421 from bbbbbr/png2asset/header_for_map_bin

png2asset: enable header output for map bin mode
Diffstat:
Mgbdk-support/png2asset/png2asset.cpp676++++++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 356 insertions(+), 320 deletions(-)

diff --git a/gbdk-support/png2asset/png2asset.cpp b/gbdk-support/png2asset/png2asset.cpp @@ -23,21 +23,47 @@ enum { SPR_16x16_MSX }; -bool export_as_map = false; -bool use_map_attributes = false; -bool use_2x2_map_attributes = false; -bool pack_map_attributes = false; -bool convert_rgb_to_nes = false; -size_t map_attributes_width = 0; -size_t map_attributes_height = 0; -size_t map_attributes_packed_width = 0; -size_t map_attributes_packed_height = 0; -size_t colors_per_pal; // Number of colors per palette (ex: CGB has 4 colors per palette x 8 palettes total) -int tile_w; -int tile_h; -int sprite_mode; -int bpp = 2; -unsigned int tile_origin = 0; // Default to no tile index offset +bool export_map_binary(); +bool export_h_file(void); +bool export_c_file(void); + +// TODO: Moved these vars to global scope (from giant main()) as start of breaking main into functions +// They should get encapsulated + string output_filename_h; + string output_filename_bin; + string output_filename_attributes_bin; + string output_filename_tiles_bin; + string data_name; + //default values for some params + int sprite_w = 0; + int sprite_h = 0; + int pivot_x = 0xFFFFFF; + int pivot_y = 0xFFFFFF; + int pivot_w = 0xFFFFFF; + int pivot_h = 0xFFFFFF; + string output_filename; + int bank = 0; + bool keep_palette_order = false; + bool output_binary = false; + bool output_transposed = false; + size_t max_palettes = 8; + + bool export_as_map = false; + bool use_map_attributes = false; + bool use_2x2_map_attributes = false; + bool pack_map_attributes = false; + bool convert_rgb_to_nes = false; + size_t map_attributes_width = 0; + size_t map_attributes_height = 0; + size_t map_attributes_packed_width = 0; + size_t map_attributes_packed_height = 0; + size_t colors_per_pal; // Number of colors per palette (ex: CGB has 4 colors per palette x 8 palettes total) + int tile_w = 8; + int tile_h = 16; + int sprite_mode = SPR_8x16; + + int bpp = 2; + unsigned int tile_origin = 0; // Default to no tile index offset extern unsigned char rgb_to_nes[64]; @@ -401,7 +427,7 @@ void GetMap() struct CmpIntColor { bool operator() (unsigned int const& c1, unsigned int const& c2) const { - unsigned char* c1_ptr = (unsigned char*)&c1; + unsigned char* c1_ptr = (unsigned char*)&c1; unsigned char* c2_ptr = (unsigned char*)&c2; //Compare alpha first, transparent color is considered smaller @@ -761,24 +787,8 @@ int main(int argc, char* argv[]) } //default params - int sprite_w = 0; - int sprite_h = 0; - int pivot_x = 0xFFFFFF; - int pivot_y = 0xFFFFFF; - int pivot_w = 0xFFFFFF; - int pivot_h = 0xFFFFFF; - - tile_w = 8; - tile_h = 16; - sprite_mode = SPR_8x16; - - string output_filename = argv[1]; + output_filename = argv[1]; output_filename = output_filename.substr(0, output_filename.size() - 4) + ".c"; - int bank = 0; - bool keep_palette_order = false; - bool output_binary = false; - bool output_transposed = false; - size_t max_palettes = 8; //Parse argv for(int i = 2; i < argc; ++i) @@ -939,11 +949,11 @@ int main(int argc, char* argv[]) slash_pos = (int)output_filename.find_last_of('\\'); int dot_pos = (int)output_filename.find_first_of('.', slash_pos == -1 ? 0 : slash_pos); - string output_filename_h = output_filename.substr(0, dot_pos) + ".h"; - string output_filename_bin = output_filename.substr(0, dot_pos) + "_map.bin"; - string output_filename_attributes_bin = output_filename.substr(0, dot_pos) + "_map_attributes.bin"; - string output_filename_tiles_bin = output_filename.substr(0, dot_pos) + "_tiles.bin"; - string data_name = output_filename.substr(slash_pos + 1, dot_pos - 1 - slash_pos); + output_filename_h = output_filename.substr(0, dot_pos) + ".h"; + output_filename_bin = output_filename.substr(0, dot_pos) + "_map.bin"; + output_filename_attributes_bin = output_filename.substr(0, dot_pos) + "_map_attributes.bin"; + output_filename_tiles_bin = output_filename.substr(0, dot_pos) + "_tiles.bin"; + data_name = output_filename.substr(slash_pos + 1, dot_pos - 1 - slash_pos); replace(data_name.begin(), data_name.end(), '-', '_'); // This was moved from outside the upcoming else statement when not using keep_palette_order @@ -1127,367 +1137,395 @@ int main(int argc, char* argv[]) map_attributes_packed_height = map_attributes_height; } - //Output .h FILE + // === EXPORT === + + // Header file export + if (export_h_file() == false) return 1; // Exit with Fail + + if ((export_as_map) && (output_binary)) { + // Handle special case of binary map export + export_map_binary(); + } else { + // Normal source file export + if (export_c_file() == false) return 1; // Exit with Fail + } +} + + +bool export_h_file(void) { + FILE* file; - if(!output_binary||!export_as_map){ - file=fopen(output_filename_h.c_str(), "w"); - if (!file) { - printf("Error writing file"); - return 1; - } - fprintf(file, "//AUTOGENERATED FILE FROM png2asset\n"); - fprintf(file, "#ifndef METASPRITE_%s_H\n", data_name.c_str()); - fprintf(file, "#define METASPRITE_%s_H\n", data_name.c_str()); - fprintf(file, "\n"); - fprintf(file, "#include <stdint.h>\n"); - fprintf(file, "#include <gbdk/platform.h>\n"); - fprintf(file, "#include <gbdk/metasprites.h>\n"); - fprintf(file, "\n"); - if(use_structs) + file=fopen(output_filename_h.c_str(), "w"); + if (!file) { + printf("Error writing file: %s", output_filename_h.c_str() ); + return false; + } + + fprintf(file, "//AUTOGENERATED FILE FROM png2asset\n"); + fprintf(file, "#ifndef METASPRITE_%s_H\n", data_name.c_str()); + fprintf(file, "#define METASPRITE_%s_H\n", data_name.c_str()); + fprintf(file, "\n"); + fprintf(file, "#include <stdint.h>\n"); + fprintf(file, "#include <gbdk/platform.h>\n"); + fprintf(file, "#include <gbdk/metasprites.h>\n"); + fprintf(file, "\n"); + if(use_structs) + { + if(export_as_map) { - if(export_as_map) - { - fprintf(file, "#include \"TilesInfo.h\"\n"); - fprintf(file, "#include \"MapInfo.h\"\n"); - fprintf(file, "\n"); - fprintf(file, "extern const struct TilesInfo %s_tiles_info;\n", data_name.c_str()); - fprintf(file, "extern const struct MapInfo %s;\n", data_name.c_str()); - } - else - { - fprintf(file, "#include \"MetaSpriteInfo.h\"\n"); - fprintf(file, "\n"); - fprintf(file, "extern const struct MetaSpriteInfo %s;\n", data_name.c_str()); - } + fprintf(file, "#include \"TilesInfo.h\"\n"); + fprintf(file, "#include \"MapInfo.h\"\n"); + fprintf(file, "\n"); + fprintf(file, "extern const struct TilesInfo %s_tiles_info;\n", data_name.c_str()); + fprintf(file, "extern const struct MapInfo %s;\n", data_name.c_str()); } else { - fprintf(file, "#define %s_TILE_ORIGIN %d\n", data_name.c_str(), tile_origin); - fprintf(file, "#define %s_TILE_W %d\n", data_name.c_str(), tile_w); - fprintf(file, "#define %s_TILE_H %d\n", data_name.c_str(), tile_h); - fprintf(file, "#define %s_WIDTH %d\n", data_name.c_str(), sprite_w); - fprintf(file, "#define %s_HEIGHT %d\n", data_name.c_str(), sprite_h); - // The TILE_COUNT calc here is referring to number of 8x8 tiles, - // so the >> 3 for each sizes axis is to get a multiplier for larger hardware sprites such as 8x16 and 16x16 - fprintf(file, "#define %s_TILE_COUNT %d\n", data_name.c_str(), ((unsigned int)tiles.size() - source_tileset_size) * (tile_h >> 3) * (tile_w >> 3)); - fprintf(file, "#define %s_PALETTE_COUNT %d\n", data_name.c_str(), (unsigned int)(image.total_color_count / colors_per_pal)); - fprintf(file, "#define %s_COLORS_PER_PALETTE %d\n", data_name.c_str(), (unsigned int)colors_per_pal); - fprintf(file, "#define %s_TOTAL_COLORS %d\n", data_name.c_str(), (unsigned int)image.total_color_count); - - if (includedMapOrMetaspriteData) { - - if(export_as_map) - { - fprintf(file, "#define %s_MAP_ATTRIBUTES ", data_name.c_str()); - if(use_map_attributes && map_attributes.size()) - fprintf(file, "%s_map_attributes\n", data_name.c_str()); - else - fprintf(file, "0\n"); - - if (use_map_attributes) - { - fprintf(file, "#define %s_MAP_ATTRIBUTES_WIDTH %d\n", data_name.c_str(), (int)map_attributes_width); - fprintf(file, "#define %s_MAP_ATTRIBUTES_HEIGHT %d\n", data_name.c_str(), (int)map_attributes_height); - fprintf(file, "#define %s_MAP_ATTRIBUTES_PACKED_WIDTH %d\n", data_name.c_str(), (int)map_attributes_packed_width); - fprintf(file, "#define %s_MAP_ATTRIBUTES_PACKED_HEIGHT %d\n", data_name.c_str(), (int)map_attributes_packed_height); - } + fprintf(file, "#include \"MetaSpriteInfo.h\"\n"); + fprintf(file, "\n"); + fprintf(file, "extern const struct MetaSpriteInfo %s;\n", data_name.c_str()); + } + } + else + { + fprintf(file, "#define %s_TILE_ORIGIN %d\n", data_name.c_str(), tile_origin); + fprintf(file, "#define %s_TILE_W %d\n", data_name.c_str(), tile_w); + fprintf(file, "#define %s_TILE_H %d\n", data_name.c_str(), tile_h); + fprintf(file, "#define %s_WIDTH %d\n", data_name.c_str(), sprite_w); + fprintf(file, "#define %s_HEIGHT %d\n", data_name.c_str(), sprite_h); + // The TILE_COUNT calc here is referring to number of 8x8 tiles, + // so the >> 3 for each sizes axis is to get a multiplier for larger hardware sprites such as 8x16 and 16x16 + fprintf(file, "#define %s_TILE_COUNT %d\n", data_name.c_str(), ((unsigned int)tiles.size() - source_tileset_size) * (tile_h >> 3) * (tile_w >> 3)); + fprintf(file, "#define %s_PALETTE_COUNT %d\n", data_name.c_str(), (unsigned int)(image.total_color_count / colors_per_pal)); + fprintf(file, "#define %s_COLORS_PER_PALETTE %d\n", data_name.c_str(), (unsigned int)colors_per_pal); + fprintf(file, "#define %s_TOTAL_COLORS %d\n", data_name.c_str(), (unsigned int)image.total_color_count); + + if (includedMapOrMetaspriteData) { - if(use_structs) - { - fprintf(file, "#define %s_TILE_PALS ", data_name.c_str()); - if(use_map_attributes) - fprintf(file, "0\n"); - else - fprintf(file, "%s_tile_pals\n", data_name.c_str()); - } - } + if(export_as_map) + { + fprintf(file, "#define %s_MAP_ATTRIBUTES ", data_name.c_str()); + if(use_map_attributes && map_attributes.size()) + fprintf(file, "%s_map_attributes\n", data_name.c_str()); else + fprintf(file, "0\n"); + + if (use_map_attributes) { - fprintf(file, "#define %s_PIVOT_X %d\n", data_name.c_str(), pivot_x); - fprintf(file, "#define %s_PIVOT_Y %d\n", data_name.c_str(), pivot_y); - fprintf(file, "#define %s_PIVOT_W %d\n", data_name.c_str(), pivot_w); - fprintf(file, "#define %s_PIVOT_H %d\n", data_name.c_str(), pivot_h); + fprintf(file, "#define %s_MAP_ATTRIBUTES_WIDTH %d\n", data_name.c_str(), (int)map_attributes_width); + fprintf(file, "#define %s_MAP_ATTRIBUTES_HEIGHT %d\n", data_name.c_str(), (int)map_attributes_height); + fprintf(file, "#define %s_MAP_ATTRIBUTES_PACKED_WIDTH %d\n", data_name.c_str(), (int)map_attributes_packed_width); + fprintf(file, "#define %s_MAP_ATTRIBUTES_PACKED_HEIGHT %d\n", data_name.c_str(), (int)map_attributes_packed_height); } - } - fprintf(file, "\n"); - fprintf(file, "BANKREF_EXTERN(%s)\n", data_name.c_str()); - fprintf(file, "\n"); - // If we are not using a source tileset, or if we have extra palettes defined - if (image.total_color_count - source_total_color_count > 0 || !use_source_tileset) { - fprintf(file, "extern const palette_color_t %s_palettes[%d];\n", data_name.c_str(), (unsigned int)image.total_color_count - source_total_color_count); + if(use_structs) + { + fprintf(file, "#define %s_TILE_PALS ", data_name.c_str()); + if(use_map_attributes) + fprintf(file, "0\n"); + else + fprintf(file, "%s_tile_pals\n", data_name.c_str()); + } } - if (includeTileData) { - fprintf(file, "extern const uint8_t %s_tiles[%d];\n", data_name.c_str(), (unsigned int)((tiles.size() - source_tileset_size) * (tile_w * tile_h * bpp / 8))); + else + { + fprintf(file, "#define %s_PIVOT_X %d\n", data_name.c_str(), pivot_x); + fprintf(file, "#define %s_PIVOT_Y %d\n", data_name.c_str(), pivot_y); + fprintf(file, "#define %s_PIVOT_W %d\n", data_name.c_str(), pivot_w); + fprintf(file, "#define %s_PIVOT_H %d\n", data_name.c_str(), pivot_h); } + } + fprintf(file, "\n"); + fprintf(file, "BANKREF_EXTERN(%s)\n", data_name.c_str()); + fprintf(file, "\n"); - fprintf(file, "\n"); - if (includedMapOrMetaspriteData) { - if(export_as_map) - { - fprintf(file, "extern const unsigned char %s_map[%d];\n", data_name.c_str(), (unsigned int)map.size()); + // If we are not using a source tileset, or if we have extra palettes defined + if (image.total_color_count - source_total_color_count > 0 || !use_source_tileset) { + fprintf(file, "extern const palette_color_t %s_palettes[%d];\n", data_name.c_str(), (unsigned int)image.total_color_count - source_total_color_count); + } + if (includeTileData) { + fprintf(file, "extern const uint8_t %s_tiles[%d];\n", data_name.c_str(), (unsigned int)((tiles.size() - source_tileset_size) * (tile_w * tile_h * bpp / 8))); + } - if(use_map_attributes) { - if(map_attributes.size()) { - fprintf(file, "extern const unsigned char %s_map_attributes[%d];\n", data_name.c_str(), (unsigned int)map_attributes.size()); - } + fprintf(file, "\n"); + if (includedMapOrMetaspriteData) { + if(export_as_map) + { + fprintf(file, "extern const unsigned char %s_map[%d];\n", data_name.c_str(), (unsigned int)map.size()); + + if(use_map_attributes) { + if(map_attributes.size()) { + fprintf(file, "extern const unsigned char %s_map_attributes[%d];\n", data_name.c_str(), (unsigned int)map_attributes.size()); } - else if ((includeTileData) && (use_structs)) - fprintf(file, "extern const unsigned char* %s_tile_pals[%d];\n", data_name.c_str(), (unsigned int)tiles.size()); } - else - { - fprintf(file, "extern const metasprite_t* const %s_metasprites[%d];\n", data_name.c_str(), (unsigned int)sprites.size()); + else if ((includeTileData) && (use_structs)) { + fprintf(file, "extern const unsigned char* %s_tile_pals[%d];\n", data_name.c_str(), (unsigned int)tiles.size()); } } + else + { + fprintf(file, "extern const metasprite_t* const %s_metasprites[%d];\n", data_name.c_str(), (unsigned int)sprites.size()); + } } - fprintf(file, "\n"); - fprintf(file, "#endif"); + } + fprintf(file, "\n"); + fprintf(file, "#endif\n"); - fclose(file); + fclose(file); + // END: Output .h FILE - //Output .c FILE - file = fopen(output_filename.c_str(), "w"); - if(!file) { - printf("Error writing file"); - return 1; - } + return true; // success +} - if (bank) fprintf(file, "#pragma bank %d\n\n", bank); - fprintf(file, "//AUTOGENERATED FILE FROM png2asset\n\n"); - fprintf(file, "#include <stdint.h>\n"); - fprintf(file, "#include <gbdk/platform.h>\n"); - fprintf(file, "#include <gbdk/metasprites.h>\n"); - fprintf(file, "\n"); +bool export_c_file(void) { - fprintf(file, "BANKREF(%s)\n\n", data_name.c_str()); + FILE* file; - // Are we not using a source tileset, or do we have extra colors - if (image.total_color_count - source_total_color_count > 0||!use_source_tileset) { + file = fopen(output_filename.c_str(), "w"); + if(!file) { + printf("Error writing file: %s", output_filename.c_str() ); + return false; + } - // Subtract however many palettes we had in the source tileset - fprintf(file, "const palette_color_t %s_palettes[%d] = {\n", data_name.c_str(), (unsigned int)image.total_color_count - source_total_color_count); + if (bank) fprintf(file, "#pragma bank %d\n\n", bank); - // Offset by however many palettes we had in the source tileset - for (size_t i = source_total_color_count / colors_per_pal; i < image.total_color_count / colors_per_pal; ++i) - { - if(i != 0) - fprintf(file, ",\n"); - fprintf(file, "\t"); + fprintf(file, "//AUTOGENERATED FILE FROM png2asset\n\n"); - unsigned char* pal_ptr = &image.palette[i * (colors_per_pal * RGBA32_SZ)]; - for(int c = 0; c < (int)colors_per_pal; ++ c, pal_ptr += RGBA32_SZ) - { - size_t rgb222 = (((pal_ptr[2] >> 6) & 0x3) << 4) | - (((pal_ptr[1] >> 6) & 0x3) << 2) | - (((pal_ptr[0] >> 6) & 0x3) << 0); - if (convert_rgb_to_nes) - fprintf(file, "0x%0X", rgb_to_nes[rgb222]); - else - fprintf(file, "RGB8(%3d,%3d,%3d)", pal_ptr[0], pal_ptr[1], pal_ptr[2]); - if(c != (int)colors_per_pal - 1) - fprintf(file, ", "); - // Line break every 4 color entries, to keep line width down - if (((c + 1) % 4) == 0) - fprintf(file, "\n\t"); - } - } - fprintf(file, "\n};\n"); - } + fprintf(file, "#include <stdint.h>\n"); + fprintf(file, "#include <gbdk/platform.h>\n"); + fprintf(file, "#include <gbdk/metasprites.h>\n"); + fprintf(file, "\n"); - if (includeTileData) { - fprintf(file, "\n"); - fprintf(file, "const uint8_t %s_tiles[%d] = {\n", data_name.c_str(), (unsigned int)((tiles.size()-source_tileset_size) * tile_w * tile_h * bpp / 8)); - fprintf(file, "\t"); - for (vector< Tile >::iterator it = tiles.begin()+ source_tileset_size; it != tiles.end(); ++it) + fprintf(file, "BANKREF(%s)\n\n", data_name.c_str()); + + // Are we not using a source tileset, or do we have extra colors + if (image.total_color_count - source_total_color_count > 0||!use_source_tileset) { + + // Subtract however many palettes we had in the source tileset + fprintf(file, "const palette_color_t %s_palettes[%d] = {\n", data_name.c_str(), (unsigned int)image.total_color_count - source_total_color_count); + + // Offset by however many palettes we had in the source tileset + for (size_t i = source_total_color_count / colors_per_pal; i < image.total_color_count / colors_per_pal; ++i) { + if(i != 0) + fprintf(file, ",\n"); + fprintf(file, "\t"); - int line_break = 1; // Start with 1 to prevent line break on first iteration - vector< unsigned char > packed_data = (*it).GetPackedData(pack_mode); - for(vector< unsigned char >::iterator it2 = packed_data.begin(); it2 != packed_data.end(); ++it2) + unsigned char* pal_ptr = &image.palette[i * (colors_per_pal * RGBA32_SZ)]; + for(int c = 0; c < (int)colors_per_pal; ++ c, pal_ptr += RGBA32_SZ) { - fprintf(file, "0x%02x", (*it2)); - if((it + 1) != tiles.end() || (it2 + 1) != packed_data.end()) - fprintf(file, ","); - // Add a line break after each 8x8 tile - if (((line_break++) % (8 / bpp)) == 0) - fprintf(file, "\n\t"); + size_t rgb222 = (((pal_ptr[2] >> 6) & 0x3) << 4) | + (((pal_ptr[1] >> 6) & 0x3) << 2) | + (((pal_ptr[0] >> 6) & 0x3) << 0); + if (convert_rgb_to_nes) + fprintf(file, "0x%0X", rgb_to_nes[rgb222]); + else + fprintf(file, "RGB8(%3d,%3d,%3d)", pal_ptr[0], pal_ptr[1], pal_ptr[2]); + if(c != (int)colors_per_pal - 1) + fprintf(file, ", "); + // Line break every 4 color entries, to keep line width down + if (((c + 1) % 4) == 0) + fprintf(file, "\n\t"); } + } + fprintf(file, "\n};\n"); + } - if ((!export_as_map) && (it != tiles.end())) - fprintf(file, "\n"); + if (includeTileData) { + fprintf(file, "\n"); + fprintf(file, "const uint8_t %s_tiles[%d] = {\n", data_name.c_str(), (unsigned int)((tiles.size()-source_tileset_size) * tile_w * tile_h * bpp / 8)); + fprintf(file, "\t"); + for (vector< Tile >::iterator it = tiles.begin()+ source_tileset_size; it != tiles.end(); ++it) + { + + int line_break = 1; // Start with 1 to prevent line break on first iteration + vector< unsigned char > packed_data = (*it).GetPackedData(pack_mode); + for(vector< unsigned char >::iterator it2 = packed_data.begin(); it2 != packed_data.end(); ++it2) + { + fprintf(file, "0x%02x", (*it2)); + if((it + 1) != tiles.end() || (it2 + 1) != packed_data.end()) + fprintf(file, ","); + // Add a line break after each 8x8 tile + if (((line_break++) % (8 / bpp)) == 0) + fprintf(file, "\n\t"); } - fprintf(file, "};\n\n"); + + if ((!export_as_map) && (it != tiles.end())) + fprintf(file, "\n"); } + fprintf(file, "};\n\n"); + } - if(includedMapOrMetaspriteData) { + if(includedMapOrMetaspriteData) { - if(!export_as_map) + if(!export_as_map) + { + for(vector< MetaSprite >::iterator it = sprites.begin(); it != sprites.end(); ++ it) { - for(vector< MetaSprite >::iterator it = sprites.begin(); it != sprites.end(); ++ it) + fprintf(file, "const metasprite_t %s_metasprite%d[] = {\n", data_name.c_str(), (int)(it - sprites.begin())); + fprintf(file, "\t"); + for(MetaSprite::iterator it2 = (*it).begin(); it2 != (*it).end(); ++ it2) { - fprintf(file, "const metasprite_t %s_metasprite%d[] = {\n", data_name.c_str(), (int)(it - sprites.begin())); - fprintf(file, "\t"); - for(MetaSprite::iterator it2 = (*it).begin(); it2 != (*it).end(); ++ it2) - { - fprintf(file, "METASPR_ITEM(%d, %d, %d, %d), ", (*it2).offset_y, (*it2).offset_x, (*it2).offset_idx, (*it2).props); - } - fprintf(file, "METASPR_TERM\n"); - fprintf(file, "};\n\n"); + fprintf(file, "METASPR_ITEM(%d, %d, %d, %d), ", (*it2).offset_y, (*it2).offset_x, (*it2).offset_idx, (*it2).props); } + fprintf(file, "METASPR_TERM\n"); + fprintf(file, "};\n\n"); + } - fprintf(file, "const metasprite_t* const %s_metasprites[%d] = {\n\t", data_name.c_str(), (unsigned int)sprites.size()); - for(vector< MetaSprite >::iterator it = sprites.begin(); it != sprites.end(); ++ it) - { - fprintf(file, "%s_metasprite%d", data_name.c_str(), (int)(it - sprites.begin())); - if(it + 1 != sprites.end()) - fprintf(file, ", "); - } - fprintf(file, "\n};\n"); + fprintf(file, "const metasprite_t* const %s_metasprites[%d] = {\n\t", data_name.c_str(), (unsigned int)sprites.size()); + for(vector< MetaSprite >::iterator it = sprites.begin(); it != sprites.end(); ++ it) + { + fprintf(file, "%s_metasprite%d", data_name.c_str(), (int)(it - sprites.begin())); + if(it + 1 != sprites.end()) + fprintf(file, ", "); + } + fprintf(file, "\n};\n"); + if(use_structs) + { + fprintf(file, "\n"); + fprintf(file, "#include \"MetaSpriteInfo.h\"\n"); + fprintf(file, "const struct MetaSpriteInfo %s = {\n", data_name.c_str()); + fprintf(file, "\t%d, //width\n", pivot_w); + fprintf(file, "\t%d, //height\n", pivot_h); + fprintf(file, "\t%d, //num tiles\n", (unsigned int)tiles.size() * (tile_h >> 3)); + fprintf(file, "\t%s_tiles, //tiles\n", data_name.c_str()); + fprintf(file, "\t%d, //num palettes\n", (unsigned int)(image.total_color_count / colors_per_pal)); + fprintf(file, "\t%s_palettes, //CGB palette\n", data_name.c_str()); + fprintf(file, "\t%d, //num sprites\n", (unsigned int)sprites.size()); + fprintf(file, "\t%s_metasprites, //metasprites\n", data_name.c_str()); + fprintf(file, "};\n"); + } + } + else + { + if (includeTileData) { if(use_structs) { + //Export tiles pals (if any) + if(!use_map_attributes) + { + fprintf(file, "\n"); + fprintf(file, "const uint8_t %s_tile_pals[%d] = {\n\t", data_name.c_str(), (unsigned int)tiles.size()- source_tileset_size); + for(vector< Tile >::iterator it = tiles.begin()+ source_tileset_size; it != tiles.end(); ++ it) + { + if(it != tiles.begin()) + fprintf(file, ", "); + fprintf(file, "%d", it->pal); + } + fprintf(file, "\n};\n"); + } + //Export Tiles Info fprintf(file, "\n"); - fprintf(file, "#include \"MetaSpriteInfo.h\"\n"); - fprintf(file, "const struct MetaSpriteInfo %s = {\n", data_name.c_str()); - fprintf(file, "\t%d, //width\n", pivot_w); - fprintf(file, "\t%d, //height\n", pivot_h); + fprintf(file, "#include \"TilesInfo.h\"\n"); + fprintf(file, "BANKREF(%s_tiles_info)\n", data_name.c_str()); + fprintf(file, "const struct TilesInfo %s_tiles_info = {\n", data_name.c_str()); fprintf(file, "\t%d, //num tiles\n", (unsigned int)tiles.size() * (tile_h >> 3)); fprintf(file, "\t%s_tiles, //tiles\n", data_name.c_str()); fprintf(file, "\t%d, //num palettes\n", (unsigned int)(image.total_color_count / colors_per_pal)); - fprintf(file, "\t%s_palettes, //CGB palette\n", data_name.c_str()); - fprintf(file, "\t%d, //num sprites\n", (unsigned int)sprites.size()); - fprintf(file, "\t%s_metasprites, //metasprites\n", data_name.c_str()); + fprintf(file, "\t%s_palettes, //palettes\n", data_name.c_str()); + if(!use_map_attributes) + fprintf(file, "\t%s_tile_pals, //tile palettes\n", data_name.c_str()); + else + fprintf(file, "\t0 //tile palettes\n"); fprintf(file, "};\n"); } } - else - { - if (includeTileData) { - if(use_structs) + + //Export map + fprintf(file, "\n"); + fprintf(file, "const unsigned char %s_map[%d] = {\n", data_name.c_str(), (unsigned int)map.size()); + size_t line_size = map.size() / (image.h / 8); + if (output_transposed) { + + for(size_t i = 0; i < line_size; ++i) + { + fprintf(file, "\t"); + for (size_t j = 0; j < image.h / 8; ++j) { - //Export tiles pals (if any) - if(!use_map_attributes) - { - fprintf(file, "\n"); - fprintf(file, "const uint8_t %s_tile_pals[%d] = {\n\t", data_name.c_str(), (unsigned int)tiles.size()- source_tileset_size); - for(vector< Tile >::iterator it = tiles.begin()+ source_tileset_size; it != tiles.end(); ++ it) - { - if(it != tiles.begin()) - fprintf(file, ", "); - fprintf(file, "%d", it->pal); - } - fprintf(file, "\n};\n"); - } - //Export Tiles Info - fprintf(file, "\n"); - fprintf(file, "#include \"TilesInfo.h\"\n"); - fprintf(file, "BANKREF(%s_tiles_info)\n", data_name.c_str()); - fprintf(file, "const struct TilesInfo %s_tiles_info = {\n", data_name.c_str()); - fprintf(file, "\t%d, //num tiles\n", (unsigned int)tiles.size() * (tile_h >> 3)); - fprintf(file, "\t%s_tiles, //tiles\n", data_name.c_str()); - fprintf(file, "\t%d, //num palettes\n", (unsigned int)(image.total_color_count / colors_per_pal)); - fprintf(file, "\t%s_palettes, //palettes\n", data_name.c_str()); - if(!use_map_attributes) - fprintf(file, "\t%s_tile_pals, //tile palettes\n", data_name.c_str()); - else - fprintf(file, "\t0 //tile palettes\n"); - fprintf(file, "};\n"); + fprintf(file, "0x%02x,", map[j * line_size + i]); + } + fprintf(file, "\n"); + } + } + else { + + for (size_t j = 0; j < image.h / 8; ++j) + { + fprintf(file, "\t"); + for (size_t i = 0; i < line_size; ++i) + { + fprintf(file, "0x%02x,", map[j * line_size + i]); } + fprintf(file, "\n"); } + } + fprintf(file, "};\n"); + - //Export map + //Export map attributes (if any) + if(use_map_attributes && map_attributes.size()) + { fprintf(file, "\n"); - fprintf(file, "const unsigned char %s_map[%d] = {\n", data_name.c_str(), (unsigned int)map.size()); - size_t line_size = map.size() / (image.h / 8); + fprintf(file, "const unsigned char %s_map_attributes[%d] = {\n", data_name.c_str(), (unsigned int)map_attributes.size()); if (output_transposed) { - - for(size_t i = 0; i < line_size; ++i) + for (size_t i = 0; i < map_attributes_packed_width; ++i) { fprintf(file, "\t"); - for (size_t j = 0; j < image.h / 8; ++j) + for (size_t j = 0; j < map_attributes_packed_height; ++j) { - fprintf(file, "0x%02x,", map[j * line_size + i]); + fprintf(file, "0x%02x,", map_attributes[j * map_attributes_packed_width + i]); } fprintf(file, "\n"); } } else { - - for (size_t j = 0; j < image.h / 8; ++j) + for (size_t j = 0; j < map_attributes_packed_height; ++j) { fprintf(file, "\t"); - for (size_t i = 0; i < line_size; ++i) + for (size_t i = 0; i < map_attributes_packed_width; ++i) { - fprintf(file, "0x%02x,", map[j * line_size + i]); + fprintf(file, "0x%02x,", map_attributes[j * map_attributes_packed_width + i]); } fprintf(file, "\n"); } } - fprintf(file, "};\n"); - - - //Export map attributes (if any) - if(use_map_attributes && map_attributes.size()) - { - fprintf(file, "\n"); - fprintf(file, "const unsigned char %s_map_attributes[%d] = {\n", data_name.c_str(), (unsigned int)map_attributes.size()); - if (output_transposed) { - for (size_t i = 0; i < map_attributes_packed_width; ++i) - { - fprintf(file, "\t"); - for (size_t j = 0; j < map_attributes_packed_height; ++j) - { - fprintf(file, "0x%02x,", map_attributes[j * map_attributes_packed_width + i]); - } - fprintf(file, "\n"); - } - } - else { - for (size_t j = 0; j < map_attributes_packed_height; ++j) - { - fprintf(file, "\t"); - for (size_t i = 0; i < map_attributes_packed_width; ++i) - { - fprintf(file, "0x%02x,", map_attributes[j * map_attributes_packed_width + i]); - } - fprintf(file, "\n"); - } - } - fprintf(file, "};\n"); - } + fprintf(file, "};\n"); + } - if(use_structs) - { - //Export Map Info - fprintf(file, "\n"); - fprintf(file, "#include \"MapInfo.h\"\n"); - fprintf(file, "BANKREF_EXTERN(%s_tiles_info)\n", data_name.c_str()); - fprintf(file, "const struct MapInfo %s = {\n", data_name.c_str()); - fprintf(file, "\t%s_map, //map\n", data_name.c_str()); - fprintf(file, "\t%d, //with\n", image.w >> 3); - fprintf(file, "\t%d, //height\n", image.h >> 3); - if (use_map_attributes && map_attributes.size()) - fprintf(file, "\t%s_map_attributes, //map attributes\n", data_name.c_str()); - else - fprintf(file, "\t%s, //map attributes\n", "0"); - fprintf(file, "\tBANK(%s_tiles_info), //tiles bank\n", data_name.c_str()); - fprintf(file, "\t&%s_tiles_info, //tiles info\n", data_name.c_str()); - fprintf(file, "};\n"); - } + if(use_structs) + { + //Export Map Info + fprintf(file, "\n"); + fprintf(file, "#include \"MapInfo.h\"\n"); + fprintf(file, "BANKREF_EXTERN(%s_tiles_info)\n", data_name.c_str()); + fprintf(file, "const struct MapInfo %s = {\n", data_name.c_str()); + fprintf(file, "\t%s_map, //map\n", data_name.c_str()); + fprintf(file, "\t%d, //with\n", image.w >> 3); + fprintf(file, "\t%d, //height\n", image.h >> 3); + if (use_map_attributes && map_attributes.size()) + fprintf(file, "\t%s_map_attributes, //map attributes\n", data_name.c_str()); + else + fprintf(file, "\t%s, //map attributes\n", "0"); + fprintf(file, "\tBANK(%s_tiles_info), //tiles bank\n", data_name.c_str()); + fprintf(file, "\t&%s_tiles_info, //tiles info\n", data_name.c_str()); + fprintf(file, "};\n"); } } - - fclose(file); } - // If we are exporting as a map, and binary output is desired - else if (export_as_map) { + fclose(file); + + return true; // success +} + + +bool export_map_binary() { std::ofstream mapBinaryFile, mapAttributesBinaryfile,tilesBinaryFile; mapBinaryFile.open(output_filename_bin, std::ios_base::binary); @@ -1545,7 +1583,5 @@ int main(int argc, char* argv[]) tilesBinaryFile.close(); if (use_map_attributes)mapAttributesBinaryfile.close(); - } + return true; // success } - -

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.