gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 638d966f26e77870c15bb20a31a344c772fd11aa parent 5eba9417bec4dcdeff303932abe9e60a1c16f1e4 Author: bbbbbr <bbbbbr@users.noreply.github.com> Date: Sat, 16 Dec 2023 02:05:54 -0800 Merge pull request #601 from bbbbbr/png2asset/re_add_source_tileset png2asset: fix -source_tileset Diffstat:
| M | gbdk-support/png2asset/Makefile | 2 | +- |
| M | gbdk-support/png2asset/export.cpp | 54 | +++++++++++++++++++++++++++++------------------------- |
| M | gbdk-support/png2asset/image_data.cpp | 70 | +++++++++++++++++++++++++++++++++++++++++++++------------------------- |
| M | gbdk-support/png2asset/image_utils.cpp | 2 | +- |
| M | gbdk-support/png2asset/image_utils.h | 2 | +- |
| M | gbdk-support/png2asset/main.cpp | 27 | ++++++++++++++++++++------- |
| M | gbdk-support/png2asset/map_attributes.cpp | 4 | ++-- |
| M | gbdk-support/png2asset/map_attributes.h | 2 | +- |
| M | gbdk-support/png2asset/maps.cpp | 59 | +++++++++++++++++++++++++++++++++-------------------------- |
| M | gbdk-support/png2asset/metasprites.cpp | 30 | ++++++++++++++++++------------ |
| M | gbdk-support/png2asset/png2asset.h | 1 | - |
| M | gbdk-support/png2asset/png_image.h | 9 | ++++++++- |
| M | gbdk-support/png2asset/process_arguments.cpp | 47 | ++++++++++++++++++++++++++++------------------- |
| M | gbdk-support/png2asset/process_arguments.h | 2 | ++ |
14 files changed, 189 insertions(+), 122 deletions(-)
diff --git a/gbdk-support/png2asset/Makefile b/gbdk-support/png2asset/Makefile @@ -5,7 +5,7 @@ # LFLAGS = -s -static CXX = $(TOOLSPREFIX)g++ -CXXFLAGS = -Os -Wall -g +CXXFLAGS = -Os -Wall -g # -Wextra -pedantic LFLAGS = -g ifeq ($(OS),Windows_NT) diff --git a/gbdk-support/png2asset/export.cpp b/gbdk-support/png2asset/export.cpp @@ -122,8 +122,10 @@ bool export_h_file( PNG2AssetData* assetData) { fprintf(file, "\n"); // If we are not using a source tileset, or if we have extra palettes defined - if(assetData->args->include_palettes && (assetData->image.total_color_count - assetData->args->source_total_color_count > 0 || assetData->args->source_tilesets.size()==0)) { - fprintf(file, "extern const palette_color_t %s_palettes[%d];\n", assetData->args->data_name.c_str(), (unsigned int)assetData->image.total_color_count - (unsigned int)assetData->args->source_total_color_count); + if (((assetData->image.total_color_count - assetData->args->source_total_color_count) > 0) || (assetData->args->has_source_tilesets == false)) { + if (assetData->args->include_palettes) { + fprintf(file, "extern const palette_color_t %s_palettes[%d];\n", assetData->args->data_name.c_str(), (unsigned int)assetData->image.total_color_count - (unsigned int)assetData->args->source_total_color_count); + } } if(assetData->args->includeTileData) { fprintf(file, "extern const uint8_t %s_tiles[%d];\n", assetData->args->data_name.c_str(), (unsigned int)((assetData->tiles.size() - assetData->args->source_tileset_size) * (assetData->image.tile_w * assetData->image.tile_h * assetData->args->bpp / 8))); @@ -189,37 +191,39 @@ bool export_c_file( PNG2AssetData* assetData) { fprintf(file, "BANKREF(%s)\n\n", assetData->args->data_name.c_str()); // Are we not using a source tileset, or do we have extra colors - if(assetData->args->include_palettes && (assetData->image.total_color_count - assetData->args->source_total_color_count > 0 || !assetData->args->source_tilesets.size() == 0)) { + if (((assetData->image.total_color_count - assetData->args->source_total_color_count) > 0) || (assetData->args->has_source_tilesets == false)) { + if (assetData->args->include_palettes) { // Subtract however many palettes we had in the source tileset fprintf(file, "const palette_color_t %s_palettes[%d] = {\n", assetData->args->data_name.c_str(), (unsigned int)assetData->image.total_color_count - (unsigned int)assetData->args->source_total_color_count); - // Offset by however many palettes we had in the source tileset - for(size_t i = assetData->args->source_total_color_count / assetData->image.colors_per_pal; i < assetData->image.total_color_count / assetData->image.colors_per_pal; ++i) - { - if(i != 0) - fprintf(file, ",\n"); - fprintf(file, "\t"); - - unsigned char* pal_ptr = &assetData->image.palette[i * (assetData->image.colors_per_pal * RGBA32_SZ)]; - for(int c = 0; c < (int)assetData->image.colors_per_pal; ++c, pal_ptr += RGBA32_SZ) + // Offset by however many palettes we had in the source tileset + for(size_t i = assetData->args->source_total_color_count / assetData->image.colors_per_pal; i < assetData->image.total_color_count / assetData->image.colors_per_pal; ++i) { - size_t rgb222 = (((pal_ptr[2] >> 6) & 0x3) << 4) | - (((pal_ptr[1] >> 6) & 0x3) << 2) | - (((pal_ptr[0] >> 6) & 0x3) << 0); - if(assetData->args->convert_rgb_to_nes) { - fprintf(file, "0x%0X", rgb_to_nes[rgb222]); + if(i != 0) + fprintf(file, ",\n"); + fprintf(file, "\t"); + + unsigned char* pal_ptr = &assetData->image.palette[i * (assetData->image.colors_per_pal * RGBA32_SZ)]; + for(int c = 0; c < (int)assetData->image.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(assetData->args->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)assetData->image.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"); } - else - fprintf(file, "RGB8(%3d,%3d,%3d)", pal_ptr[0], pal_ptr[1], pal_ptr[2]); - if(c != (int)assetData->image.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, "\n};\n"); } if(assetData->args->includeTileData) { diff --git a/gbdk-support/png2asset/image_data.cpp b/gbdk-support/png2asset/image_data.cpp @@ -28,14 +28,13 @@ using namespace std; int decodePNG(vector<unsigned char>& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, size_t in_size, bool convert_to_rgba32 = true); void loadFile(vector<unsigned char>& buffer, const std::string& filename); -int ReadImageData_KeepPaletteOrder( PNG2AssetData* assetData, string input_filename) { +int ReadImageData_KeepPaletteOrder( PNG2AssetData* assetData, string input_filename) { //load and decode png vector<unsigned char> buffer; lodepng::load_file(buffer, input_filename); lodepng::State state; - //Calling with keep_palette_order means //-The image should be png indexed (1-8 bits per pixel) //-For CGB: Each 4 colors define a gbc palette, the first color is the transparent one @@ -52,9 +51,13 @@ int ReadImageData_KeepPaletteOrder( PNG2AssetData* assetData, string input_fil // Also see below about requirement to use palette from source image state.decoder.color_convert = false; + // Clearing is needed to ensure loading the png works + // (in cases where a previous png was loaded for source tilesets) + assetData->image.data.clear(); + unsigned error = lodepng::decode(assetData->image.data, assetData->image.w, assetData->image.h, state, buffer); // Unpack the image if needed. Also checks and errors on incompatible palette type if needed - if(!image_indexed_ensure_8bpp(assetData->image.data, assetData->image.w, assetData->image.h, (int)state.info_png.color.bitdepth, (int)state.info_png.color.colortype)) + if(!image_indexed_ensure_8bpp(assetData->image.data, (int)state.info_png.color.bitdepth, (int)state.info_png.color.colortype)) return 1; else if(error) { printf("decoder error %s\n", lodepng_error_text(error)); @@ -69,6 +72,12 @@ int ReadImageData_KeepPaletteOrder( PNG2AssetData* assetData, string input_fil assetData->image.palette = new unsigned char[assetData->image.total_color_count * RGBA32_SZ]; memcpy(assetData->image.palette, state.info_png.color.palette, assetData->image.total_color_count * RGBA32_SZ); + // Save a copy of the palette data if it's the source palette (free first if already allocated) + if (assetData->args->processing_mode == MODE_SOURCE_TILESET) { + if (assetData->image.source_tileset_palette) free(assetData->image.source_tileset_palette); + assetData->image.source_tileset_palette = new unsigned char[assetData->image.total_color_count * RGBA32_SZ]; + memcpy(assetData->image.source_tileset_palette, state.info_png.color.palette, assetData->image.total_color_count * RGBA32_SZ); + } if(assetData->args->repair_indexed_pal) @@ -83,21 +92,24 @@ int ReadImageData_KeepPaletteOrder( PNG2AssetData* assetData, string input_fil // return 1; // } - if(assetData->args->source_tilesets.size()>0) { + // Only check this for the main image, not for source tilesets + if ((assetData->args->processing_mode == MODE_MAIN_IMAGE) && (assetData->args->has_source_tilesets)) { // Make sure these two values match when keeping palette order - if(assetData->image.total_color_count != assetData->source_tileset_image.total_color_count) { + if(assetData->image.total_color_count != assetData->args->source_total_color_count) { - printf("error: The number of color palette's for your source tileset (%d) and target image (%d) do not match.", (unsigned int)assetData->source_tileset_image.total_color_count, (unsigned int)assetData->image.total_color_count); + printf("error: The number of color palettes for your source tileset (%d) and target image (%d) do not match.", + (unsigned int)assetData->args->source_total_color_count, (unsigned int)assetData->image.total_color_count); return 1; } - size_t size = max(assetData->image.total_color_count, assetData->source_tileset_image.total_color_count); + size_t size = max(assetData->image.total_color_count, assetData->args->source_total_color_count); + // size_t size = max(assetData->image.total_color_count, assetData->source_tileset_image.total_color_count); // Make sure these two values match when keeping palette order - if(memcmp(assetData->image.palette, assetData->source_tileset_image.palette, size) != 0) { + if (memcmp(assetData->image.palette, assetData->image.source_tileset_palette, size) != 0) { - printf("error: The palette's for your source tileset and target image do not match."); + printf("error: The palettes for your source tileset and target image do not match."); return 1; } } @@ -106,11 +118,9 @@ int ReadImageData_KeepPaletteOrder( PNG2AssetData* assetData, string input_fil int ReadImageData_Default(PNG2AssetData* assetData, string input_filename) { - - //load and decode png vector<unsigned char> buffer; - lodepng::load_file(buffer, assetData->args->input_filename); + lodepng::load_file(buffer, input_filename); lodepng::State state; PNGImage image32; @@ -134,7 +144,9 @@ int ReadImageData_Default(PNG2AssetData* assetData, string input_filename) { int* palettes_per_tile = BuildPalettesAndAttributes(image32, assetData->palettes, assetData->args->use_2x2_map_attributes); - //Create the indexed image + // Create the indexed image + // Clearing is needed to ensure loading the png works + // (in cases where a previous png was loaded for source tilesets) assetData->image.data.clear(); assetData->image.w = image32.w; assetData->image.h = image32.h; @@ -146,9 +158,12 @@ int ReadImageData_Default(PNG2AssetData* assetData, string input_filename) { // Pre-fill palette to all black. Prevents garbage in palette color slots that are unused (ex: only 3 colors when colors-per-pal is 4) memset(assetData->image.palette, 0, palette_count * assetData->image.colors_per_pal * RGBA32_SZ); - // If we are using a sourcetileset and have more palettes than it defines - if(assetData->args->source_tilesets.size() > 0 && (assetData->image.total_color_count > assetData->args->source_total_color_count)) { - printf("Found %d extra palette(s) for target tilemap.\n", (unsigned int)((assetData->image.total_color_count - assetData->args->source_total_color_count) / assetData->image.colors_per_pal)); + // If using a source tileset and have more palettes than it defines + // (only check for main image data, not source tilesets) + if ((assetData->args->processing_mode == MODE_MAIN_IMAGE) && (assetData->args->has_source_tilesets)) { + if (assetData->image.total_color_count > assetData->args->source_total_color_count) { + printf("Found %d extra palette(s) for target tilemap.\n", (unsigned int)((assetData->image.total_color_count - assetData->args->source_total_color_count) / assetData->image.colors_per_pal)); + } } for(size_t p = 0; p < palette_count; ++p) { @@ -182,11 +197,14 @@ int ReadImageData_Default(PNG2AssetData* assetData, string input_filename) { int ReadImageData( PNG2AssetData* assetData, string input_filename) { assetData->image.colors_per_pal = static_cast<size_t>(1) << assetData->args->bpp; + // assetData->source_tileset_image.colors_per_pal = static_cast<size_t>(1) << assetData->args->bpp; if(assetData->args->export_as_map) { assetData->image.tile_w = 8; //Force tiles_w to 8 on maps assetData->image.tile_h = 8; //Force tiles_h to 8 on maps + // assetData->source_tileset_image.tile_w = 8; //Force tiles_w to 8 on maps + // assetData->source_tileset_image.tile_h = 8; //Force tiles_h to 8 on maps assetData->args->sprite_mode = SPR_NONE; } @@ -197,25 +215,27 @@ int ReadImageData( PNG2AssetData* assetData, string input_filename) { if(assetData->args->keep_palette_order) { // Save the error code - errorCode= ReadImageData_KeepPaletteOrder(assetData, input_filename); + errorCode= ReadImageData_KeepPaletteOrder(assetData, input_filename); } else { // Save the error code - errorCode= ReadImageData_Default(assetData, input_filename); + errorCode= ReadImageData_Default(assetData, input_filename); } if(errorCode != 0)return errorCode; - if(assetData->args->spriteSize.width == 0) assetData->args->spriteSize.width = (int)assetData->image.w; - if(assetData->args->spriteSize.height == 0) assetData->args->spriteSize.height = (int)assetData->image.h; - if(assetData->args->pivot.x == 0xFFFFFF) assetData->args->pivot.x = (unsigned int)assetData->args->spriteSize.width / 2; - if(assetData->args->pivot.y == 0xFFFFFF) assetData->args->pivot.y = (unsigned int)assetData->args->spriteSize.height / 2; - if(assetData->args->pivot.width == 0xFFFFFF) assetData->args->pivot.width = assetData->args->spriteSize.width; - if(assetData->args->pivot.height == 0xFFFFFF) assetData->args->pivot.height = assetData->args->spriteSize.height; - + // Only set this data when processing the main image + if (assetData->args->processing_mode == MODE_MAIN_IMAGE) { + if(assetData->args->spriteSize.width == 0) assetData->args->spriteSize.width = (int)assetData->image.w; + if(assetData->args->spriteSize.height == 0) assetData->args->spriteSize.height = (int)assetData->image.h; + if(assetData->args->pivot.x == 0xFFFFFF) assetData->args->pivot.x = (unsigned int)assetData->args->spriteSize.width / 2; + if(assetData->args->pivot.y == 0xFFFFFF) assetData->args->pivot.y = (unsigned int)assetData->args->spriteSize.height / 2; + if(assetData->args->pivot.width == 0xFFFFFF) assetData->args->pivot.width = assetData->args->spriteSize.width; + if(assetData->args->pivot.height == 0xFFFFFF) assetData->args->pivot.height = assetData->args->spriteSize.height; + } return 0; } diff --git a/gbdk-support/png2asset/image_utils.cpp b/gbdk-support/png2asset/image_utils.cpp @@ -41,7 +41,7 @@ static void image_bitunpack(const vector<uint8_t> & src_image_data, vector<uint8 // Converts indexed image with bit depths 1- 8 bpp to indexed 8 bits per pixel // Replaces incoming image buffer with unpacked image buffer if successful -bool image_indexed_ensure_8bpp(vector<uint8_t> & src_image_data, int width, int height, int bitdepth, int colortype) { +bool image_indexed_ensure_8bpp(vector<uint8_t> & src_image_data, int bitdepth, int colortype) { vector<uint8_t> unpacked_image_data; diff --git a/gbdk-support/png2asset/image_utils.h b/gbdk-support/png2asset/image_utils.h @@ -5,7 +5,7 @@ #include <vector> -bool image_indexed_ensure_8bpp(vector <unsigned char> &data, int width, int height, int bitdepth, int colortype); +bool image_indexed_ensure_8bpp(vector <unsigned char> &data, int bitdepth, int colortype); bool image_indexed_repair_tile_palettes(PNGImage & image, bool use_2x2_map_attributes); #endif diff --git a/gbdk-support/png2asset/main.cpp b/gbdk-support/png2asset/main.cpp @@ -39,34 +39,47 @@ int main(int argc, char* argv[]) return errorCode; } + // The png2AssetInstance tile and palette data is retained after + // processing source tilesets and so is shared with the main image PNG2AssetData png2AssetInstance; // If we have a source tileset - if(arguments.source_tilesets.size() > 0) { + if (arguments.source_tilesets.size() > 0) { - vector<string>::iterator sourceTilesetsIterator = arguments.source_tilesets.begin(); + vector<string>::iterator sourceTilesetFileNameIter = arguments.source_tilesets.begin(); // Iterate through each source tileset and execute - while (sourceTilesetsIterator < arguments.source_tilesets.end()) { + while (sourceTilesetFileNameIter < arguments.source_tilesets.end()) { - // Run with our source tileset filename - errorCode = png2AssetInstance.Execute(&arguments, *sourceTilesetsIterator); + // Run with current source tileset filename + arguments.processing_mode = MODE_SOURCE_TILESET; + errorCode = png2AssetInstance.Execute(&arguments, *sourceTilesetFileNameIter); // Return the error code if the function returns non-zero if(errorCode != 0) { return errorCode; } - sourceTilesetsIterator++; + sourceTilesetFileNameIter++; } // Save these values for later usage on the main execution - arguments.source_tileset_size = (unsigned int)png2AssetInstance.tiles.size(); + arguments.source_tileset_size = (unsigned int)png2AssetInstance.tiles.size(); arguments.source_total_color_count = png2AssetInstance.image.total_color_count; + + // Clearing map and attributes isn't needed here since adding them is blocked for source tileset data + // (pre-refactor png2asset used map.clear() and map_attributes.clear() ) + + arguments.has_source_tilesets = true; + printf("Got %d tiles from the source tileset.\n", (unsigned int)arguments.source_tileset_size); + printf("Got %d palettes from the source tileset.\n", (unsigned int)(arguments.source_total_color_count / png2AssetInstance.image.colors_per_pal)); } + + // Run the primary input file // Return the error code if the function returns non-zero + arguments.processing_mode = MODE_MAIN_IMAGE; if((errorCode = png2AssetInstance.Execute(&arguments, arguments.input_filename)) != 0) { return errorCode; } diff --git a/gbdk-support/png2asset/map_attributes.cpp b/gbdk-support/png2asset/map_attributes.cpp @@ -26,7 +26,7 @@ unsigned char GetMapAttribute(size_t x, size_t y,PNG2AssetData* assetData) return 0; } -void ReduceMapAttributes2x2(const vector< SetPal >& palettes,PNG2AssetData* assetData) +void ReduceMapAttributes2x2(PNG2AssetData* assetData) { size_t w = (assetData->args->map_attributes_size.width + 1) / 2; size_t h = (assetData->args->map_attributes_size.height + 1) / 2; @@ -113,7 +113,7 @@ void HandleMapAttributes(PNG2AssetData* assetData) { if(assetData->args->use_2x2_map_attributes) { // NES attribute map dimensions are half-resolution - ReduceMapAttributes2x2(assetData->palettes, assetData); + ReduceMapAttributes2x2(assetData); } // Optionally align and pack map attributes into NES PPU format if(assetData->args->pack_map_attributes) diff --git a/gbdk-support/png2asset/map_attributes.h b/gbdk-support/png2asset/map_attributes.h @@ -4,7 +4,7 @@ #include "cmp_int_color.h" using namespace std; unsigned char GetMapAttribute(size_t x, size_t y); -void ReduceMapAttributes2x2(const vector< SetPal >& palettes); +void ReduceMapAttributes2x2(PNG2AssetData* assetData); void AlignMapAttributes(PNG2AssetData* assetData); void PackMapAttributes(PNG2AssetData* assetData); void HandleMapAttributes( PNG2AssetData* assetData); diff --git a/gbdk-support/png2asset/maps.cpp b/gbdk-support/png2asset/maps.cpp @@ -34,8 +34,10 @@ void GetMap( PNG2AssetData* assetData) size_t idx; unsigned char props; - if(assetData->args->keep_duplicate_tiles) - { + // When both -keep_duplicate_tiles and source tilesets are used then + // keep_duplicate_tiles should only apply to source tilesets, not the main image + if ((assetData->args->keep_duplicate_tiles) && + ((assetData->args->has_source_tilesets == false) || (assetData->args->processing_mode == MODE_SOURCE_TILESET))) { assetData->tiles.push_back(tile); idx = assetData->tiles.size() - 1; props = assetData->args->props_default; @@ -44,7 +46,7 @@ void GetMap( PNG2AssetData* assetData) { if(!FindTile(tile, idx, props,assetData)) { - if(assetData->args->source_tilesets.size() > 0) { + if ((assetData->args->processing_mode == MODE_MAIN_IMAGE) && (assetData->args->has_source_tilesets)) { printf("found a tile not in the source tileset at %d,%d. The target tileset has %d extra tiles.\n", x, y, (unsigned int)assetData->args->extra_tile_count + 1); assetData->args->extra_tile_count++; assetData->args->includeTileData = true; @@ -61,36 +63,41 @@ void GetMap( PNG2AssetData* assetData) } } - assetData->map.push_back((unsigned char)idx + assetData->args->tile_origin); - if(assetData->args->use_map_attributes) - { - unsigned char pal_idx = assetData->image.data[y * assetData->image.w + x] >> assetData->args->bpp; //We can pick the palette from the first pixel of this tile - if(assetData->args->pack_mode == Tile::SGB) - { - props = props << 1; //Mirror flags in SGB are on bit 7 - props |= (pal_idx + 4) << 2; //Pals are in bits 2,3,4 and need to go from 4 to 7 - assetData->map.push_back(props); //Also they are stored within the map tiles - } - else if(assetData->args->pack_mode == Tile::SMS) - { - props = props >> 4; - if(idx > 255) - props |= 1; - assetData->map.push_back(props); - } - else + // Don't add map tiles and attributes for source tilesets + if (assetData->args->processing_mode == MODE_MAIN_IMAGE) { + assetData->map.push_back((unsigned char)idx + assetData->args->tile_origin); + + if(assetData->args->use_map_attributes) { - props |= pal_idx; - assetData->map_attributes.push_back(props); + unsigned char pal_idx = assetData->image.data[y * assetData->image.w + x] >> assetData->args->bpp; //We can pick the palette from the first pixel of this tile + if(assetData->args->pack_mode == Tile::SGB) + { + props = props << 1; //Mirror flags in SGB are on bit 7 + props |= (pal_idx + 4) << 2; //Pals are in bits 2,3,4 and need to go from 4 to 7 + assetData->map.push_back(props); //Also they are stored within the map tiles + } + else if(assetData->args->pack_mode == Tile::SMS) + { + props = props >> 4; + if(idx > 255) + props |= 1; + assetData->map.push_back(props); + } + else + { + props |= pal_idx; + assetData->map_attributes.push_back(props); + } } } - } } - - HandleMapAttributes( assetData); + // Don't add map tiles and attributes for source tilesets + if (assetData->args->processing_mode == MODE_MAIN_IMAGE) { + HandleMapAttributes( assetData); + } } diff --git a/gbdk-support/png2asset/metasprites.cpp b/gbdk-support/png2asset/metasprites.cpp @@ -36,8 +36,10 @@ void GetMetaSprite(int _x, int _y, int _w, int _h, int pivot_x, int pivot_y, PNG unsigned char props; unsigned char pal_idx = assetData->image.data[y * assetData->image.w + x] >> 2; //We can pick the palette from the first pixel of this tile - if(assetData->args->keep_duplicate_tiles) - { + // When both -keep_duplicate_tiles and source tilesets are used then + // keep_duplicate_tiles should only apply to source tilesets, not the main image + if ((assetData->args->keep_duplicate_tiles) && + ((assetData->args->has_source_tilesets == false) || (assetData->args->processing_mode == MODE_SOURCE_TILESET))) { assetData->tiles.push_back(tile); idx = assetData->tiles.size() - 1; props = assetData->args->props_default; @@ -46,7 +48,7 @@ void GetMetaSprite(int _x, int _y, int _w, int _h, int pivot_x, int pivot_y, PNG { if(!FindTile(tile, idx, props, assetData)) { - if(assetData->args->source_tilesets.size() > 0) { + if ((assetData->args->processing_mode == MODE_MAIN_IMAGE) && (assetData->args->has_source_tilesets)) { printf("found a tile not in the source tileset at %d,%d. The target tileset has %d extra tiles.\n", x, y, (unsigned int)assetData->args->extra_tile_count + 1); assetData->args->extra_tile_count++; assetData->args->includeTileData = true; @@ -57,17 +59,21 @@ void GetMetaSprite(int _x, int _y, int _w, int _h, int pivot_x, int pivot_y, PNG } } - props |= pal_idx; + // Don't add metasprite tiles for source tilesets + if (assetData->args->processing_mode == MODE_MAIN_IMAGE) { + + props |= pal_idx; - // Scale up index based on 8x8 tiles-per-hardware sprite - if(assetData->args->sprite_mode == SPR_8x16) - idx *= 2; - else if(assetData->args->sprite_mode == SPR_16x16_MSX) - idx *= 4; + // Scale up index based on 8x8 tiles-per-hardware sprite + if(assetData->args->sprite_mode == SPR_8x16) + idx *= 2; + else if(assetData->args->sprite_mode == SPR_16x16_MSX) + idx *= 4; - mt_sprite.push_back(MTTile(x - last_x, y - last_y, (unsigned char)idx, props)); - last_x = x; - last_y = y; + mt_sprite.push_back(MTTile(x - last_x, y - last_y, (unsigned char)idx, props)); + last_x = x; + last_y = y; + } } } } diff --git a/gbdk-support/png2asset/png2asset.h b/gbdk-support/png2asset/png2asset.h @@ -28,7 +28,6 @@ public: vector< MetaSprite > sprites; vector< unsigned char > map; vector< unsigned char > map_attributes; - PNGImage source_tileset_image; PNGImage image; }; diff --git a/gbdk-support/png2asset/png_image.h b/gbdk-support/png2asset/png_image.h @@ -12,6 +12,12 @@ enum { SPR_16x16_MSX }; +// processing_mode states +enum { + MODE_MAIN_IMAGE, + MODE_SOURCE_TILESET +}; + struct PNGImage { vector< unsigned char > data; //data in indexed format @@ -30,7 +36,8 @@ struct PNGImage size_t colors_per_pal; // Number of colors per palette (ex: CGB has 4 colors per palette x 8 palettes total) size_t total_color_count; // Total number of colors across all palettes (palette_count x colors_per_pal) - unsigned char* palette; //palette colors in RGBA (1 color == 4 bytes) + unsigned char * palette = NULL; //palette colors in RGBA (1 color == 4 bytes) + unsigned char * source_tileset_palette = NULL; // Mostly used for ensuring source tileset and primary image palettes match sufficiently private: bool zero_palette = false; diff --git a/gbdk-support/png2asset/process_arguments.cpp b/gbdk-support/png2asset/process_arguments.cpp @@ -32,41 +32,50 @@ int processPNG2AssetArguments(int argc, char* argv[], PNG2AssetArguments* args) //default values for some params args->spriteSize.width = 0; args->spriteSize.height = 0; + args->map_attributes_size.width = 0; + args->map_attributes_size.height = 0; + args->map_attributes_packed_size.width = 0; + args->map_attributes_packed_size.height = 0; + args->pivot.x = 0xFFFFFF; args->pivot.y = 0xFFFFFF; args->pivot.width = 0xFFFFFF; args->pivot.height = 0xFFFFFF; - args->bank = -1; + + + args->max_palettes = 8; + args->keep_palette_order = false; args->repair_indexed_pal = false; args->output_binary = false; args->output_transposed = false; - args->max_palettes = 8; - - args->pack_mode = Tile::GB; - args->map_entry_size_bytes = 1; - args->flip_tiles = true; - args->props_default = 0; - args->keep_duplicate_tiles = false; - args->include_palettes = true; - args->includedMapOrMetaspriteData = true; - args->includeTileData = true; - args->use_structs = false; args->export_as_map = false; args->use_map_attributes = false; args->use_2x2_map_attributes = false; args->pack_map_attributes = false; args->convert_rgb_to_nes = false; - args->map_attributes_size.width = 0; - args->map_attributes_size.height = 0; - args->map_attributes_packed_size.width = 0; - args->map_attributes_packed_size.height = 0; - args->sprite_mode = SPR_8x16; - args->source_tileset_size = 0; - args->source_total_color_count = 0; + args->includeTileData = true; + args->includedMapOrMetaspriteData = true; + args->keep_duplicate_tiles = false; + args->include_palettes = true; + args->use_structs = false; + args->flip_tiles = true; + // args->errorCode; + args->bank = -1; + args->sprite_mode = SPR_8x16; args->bpp = 2; + args->props_default = 0; + args->tile_origin = 0; // Default to no tile index offset + // args->extra_tile_count; + args->source_total_color_count = 0; + args->source_tileset_size = 0; + args->has_source_tilesets = false; + args->processing_mode = MODE_MAIN_IMAGE; + + args->pack_mode = Tile::GB; + args->map_entry_size_bytes = 1; if(argc < 2) diff --git a/gbdk-support/png2asset/process_arguments.h b/gbdk-support/png2asset/process_arguments.h @@ -59,6 +59,8 @@ struct PNG2AssetArguments { size_t extra_tile_count; size_t source_total_color_count; // Total number of colors (palette_count x colors_per_palette) unsigned int source_tileset_size; + bool has_source_tilesets; + int processing_mode; // Whether the current image being processed is a source tileset is (MODE_SOURCE_TILESET) or the main image (MODE_MAIN_IMAGE) Tile::PackMode pack_mode; int map_entry_size_bytes;
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.