Merge pull request #843 from bbbbbr/png2asset/metafile_read

png2asset: -use_metafile option to load args from <inputfile>.meta #802
This commit is contained in:
bbbbbr
2025-12-01 15:38:44 -08:00
committed by GitHub
6 changed files with 170 additions and 12 deletions

View File

@@ -7,6 +7,8 @@
#include <fstream>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include "lodepng.h"
#include "mttile.h"
@@ -25,11 +27,23 @@
#include "png_image.h"
#include "tiles.h"
#define ARG_SKIP_NONE 0
#define ARG_AT_INPUT_FILENAME 1
#define ARG_AFTER_INPUT_FILENAME 2
using namespace std;
static string str_remove_path(string str_in);
static void logArguments(int startIndex, int argc, const char* argv[], PNG2AssetArguments* args);
static void initArguments(PNG2AssetArguments* args);
static void showHelp(void);
static int processArguments(int startIndex, int argc, const char* argv[], PNG2AssetArguments* args);
static int handleMetaFileArgs(PNG2AssetArguments* args);
// Strip any leading path and slashes
string str_remove_path(string str_in) {
static string str_remove_path(string str_in) {
size_t slash_pos = str_in.find_last_of('/');
if (slash_pos != str_in.npos)
str_in = str_in.substr(slash_pos, str_in.length() - slash_pos);
@@ -41,7 +55,8 @@ string str_remove_path(string str_in) {
return str_in;
}
int processPNG2AssetArguments(int argc, char* argv[], PNG2AssetArguments* args) {
static void initArguments(PNG2AssetArguments* args) {
//default values for some params
args->spriteSize.width = 0;
@@ -98,9 +113,11 @@ int processPNG2AssetArguments(int argc, char* argv[], PNG2AssetArguments* args)
args->args_for_logging_to_output = "";
args->relative_paths = false;
args->use_metafile = false;
}
if(argc < 2)
{
void showHelp(void) {
printf("usage: png2asset <file>.png [options]\n");
printf("-o <filename> ouput file (if not used then default is <png file>.c)\n");
printf("-c <filename> deprecated, same as -o\n");
@@ -143,21 +160,23 @@ int processPNG2AssetArguments(int argc, char* argv[], PNG2AssetArguments* args)
printf("-transposed export transposed (column-by-column instead of row-by-row)\n");
printf("-rel_paths paths to tilesets are relative to the input file path\n");
return EXIT_SUCCESS;
}
printf("-use_metafile Read extra options from file <inputfile>.meta (file missing not an error)\n");
}
//default params
args->input_filename = argv[1];
args->output_filename = argv[1];
args->output_filename = args->output_filename.substr(0, args->output_filename.size() - 4) + ".c";
static void logArguments(int startIndex, int argc, const char* argv[], PNG2AssetArguments* args) {
// Save all args for logging into output files
for(int i = 1; i < argc; ++i) {
for (int i = startIndex; i < argc; ++i) {
args->args_for_logging_to_output.append(" ").append( str_remove_path((string)argv[i]) );
}
}
static int processArguments(int startIndex, int argc, const char* argv[], PNG2AssetArguments* args) {
//Parse argv
for(int i = 2; i < argc; ++i)
for (int i = startIndex; i < argc; ++i)
{
if(!strcmp(argv[i], "-sw"))
{
@@ -345,11 +364,85 @@ int processPNG2AssetArguments(int argc, char* argv[], PNG2AssetArguments* args)
else if(!strcmp(argv[i], "-rel_paths")) {
args->relative_paths = true;
}
else if(!strcmp(argv[i], "-use_metafile")) {
args->use_metafile = true;
}
else {
printf("Warning: Argument \"%s\" not recognized\n", argv[i]);
}
}
return EXIT_SUCCESS;
}
// Read in and process a set of arguments from a file named <inputfile>.meta
static int handleMetaFileArgs(PNG2AssetArguments* args) {
string fname = args->input_filename + ".meta";
ifstream metaFile(fname);
if ( metaFile )
{
static vector<string> argStrings;
static std::vector<char const*> metafile_argv; // Static for program scope, const to ensure c_str() pointers remain valid
// Read file contents
stringstream metaFileBuffer;
metaFileBuffer << metaFile.rdbuf();
metaFile.close();
// Split strings on spaces/newlines
string argEntry;
argStrings.clear();
while (metaFileBuffer >> argEntry) {
argStrings.push_back(argEntry);
}
// Build argv style array
int metafile_argc = static_cast<int>(argStrings.size());
metafile_argv.clear();
metafile_argv.reserve(metafile_argc + 1); // +1 for null terminator entry (optional with our usage)
for (const auto& s : argStrings) {
metafile_argv.push_back(s.c_str());
}
metafile_argv.push_back(nullptr);
// Append args to logged ones and then process them
logArguments(ARG_SKIP_NONE, metafile_argc, metafile_argv.data(), args);
if (processArguments(ARG_SKIP_NONE, metafile_argc, metafile_argv.data(), args) == EXIT_FAILURE)
return EXIT_FAILURE;
} else {
printf("Warning: -use_metafile specified but no meta file found at: %s\n", fname.c_str());
}
return EXIT_SUCCESS;
}
int processPNG2AssetArguments(int argc, char* argv[], PNG2AssetArguments* args) {
initArguments(args);
if (argc < 2) {
showHelp();
return EXIT_SUCCESS;
}
//default params
args->input_filename = argv[ARG_AT_INPUT_FILENAME];
args->output_filename = argv[ARG_AT_INPUT_FILENAME];
args->output_filename = args->output_filename.substr(0, args->output_filename.size() - 4) + ".c";
logArguments(ARG_AT_INPUT_FILENAME, argc, (const char **)argv, args);
if (processArguments(ARG_AFTER_INPUT_FILENAME, argc, (const char **)argv, args) == EXIT_FAILURE)
return EXIT_FAILURE;
if (args->use_metafile) {
if (handleMetaFileArgs(args) == EXIT_FAILURE)
return EXIT_FAILURE;
}
int slash_pos = (int)args->output_filename.find_last_of('/');
if(slash_pos == -1)
slash_pos = (int)args->output_filename.find_last_of('\\');

View File

@@ -56,6 +56,7 @@ struct PNG2AssetArguments {
bool use_structs;
bool flip_tiles;
bool relative_paths;
bool use_metafile;
int errorCode;
int bank;

View File

@@ -157,3 +157,6 @@ generate-assets:
# -no_palettes
$(PNG2ASSET) $(RES)/no_pal_map_test.png -c $(OUTDIR)/no_pal_map_test_keep_pal_order.c -noflip -map -keep_palette_order -no_palettes
$(PNG2ASSET) $(RES)/no_pal_map_test.png -c $(OUTDIR)/no_pal_map_test.c -noflip -map -no_palettes
# -use_metafile
$(PNG2ASSET) $(RES)/no_pal_map_test.png -c $(OUTDIR)/no_pal_map_test_keep_pal_order_usemetafile.c -use_metafile

View File

@@ -0,0 +1,31 @@
//AUTOGENERATED FILE FROM png2asset
// Conversion args: /no_pal_map_test.png -c /no_pal_map_test_keep_pal_order_usemetafile.c -use_metafile -noflip -map -keep_palette_order -no_palettes
#include <stdint.h>
#include <gbdk/platform.h>
#include <gbdk/metasprites.h>
BANKREF(no_pal_map_test_keep_pal_order_usemetafile)
const uint8_t no_pal_map_test_keep_pal_order_usemetafile_tiles[144] = {
0x7f,0x7f,0xff,0xff,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,
0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xfe,0xfe,0xff,0xff,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xff,0xff,0x7f,0x7f,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,
0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0xff,0xff,0xfe,0xfe
};
const unsigned char no_pal_map_test_keep_pal_order_usemetafile_map[108] = {
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02,
0x03,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x05,
0x03,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x05,
0x03,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x05,
0x03,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x05,
0x06,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x08,
};

View File

@@ -0,0 +1,25 @@
//AUTOGENERATED FILE FROM png2asset
// Conversion args: /no_pal_map_test.png -c /no_pal_map_test_keep_pal_order_usemetafile.c -use_metafile -noflip -map -keep_palette_order -no_palettes
#ifndef METASPRITE_no_pal_map_test_keep_pal_order_usemetafile_H
#define METASPRITE_no_pal_map_test_keep_pal_order_usemetafile_H
#include <stdint.h>
#include <gbdk/platform.h>
#include <gbdk/metasprites.h>
#define no_pal_map_test_keep_pal_order_usemetafile_TILE_ORIGIN 0
#define no_pal_map_test_keep_pal_order_usemetafile_TILE_W 8
#define no_pal_map_test_keep_pal_order_usemetafile_TILE_H 8
#define no_pal_map_test_keep_pal_order_usemetafile_WIDTH 144
#define no_pal_map_test_keep_pal_order_usemetafile_HEIGHT 48
#define no_pal_map_test_keep_pal_order_usemetafile_TILE_COUNT 9
#define no_pal_map_test_keep_pal_order_usemetafile_MAP_ATTRIBUTES 0
extern const unsigned char no_pal_map_test_keep_pal_order_usemetafile_map[108];
#define no_pal_map_test_keep_pal_order_usemetafile_map_attributes no_pal_map_test_keep_pal_order_usemetafile_map
BANKREF_EXTERN(no_pal_map_test_keep_pal_order_usemetafile)
extern const uint8_t no_pal_map_test_keep_pal_order_usemetafile_tiles[144];
#endif

View File

@@ -0,0 +1,5 @@
-noflip -map
-keep_palette_order
-no_palettes