gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/png2asset/main.cpp
1 #include <vector>
2 #include <string>
3 #include <algorithm>
4 #include <cstring>
5 #include <set>
6 #include <stdio.h>
7 #include <fstream>
8 #include <cstdint>
9 #include <cstdlib>
10
11 #include "lodepng.h"
12 #include "mttile.h"
13 #include "export.h"
14 #include "map_attributes.h"
15 #include "palettes.h"
16
17 #include "cmp_int_color.h"
18
19 #include "png2asset.h"
20 #include "image_utils.h"
21 #include "image_data.h"
22 #include "maps.h"
23 #include "metasprites.h"
24 #include "metasprites_functions.h"
25 #include "process_arguments.h"
26 #include "png_image.h"
27 #include "tiles.h"
28
29 using namespace std;
30
31 string extract_path(bool extract, string const & path, string const & name)
32 {
33 int p = path.find_last_of("/\\");
34 return ((extract) && (p > 0)) ? path.substr(0, p + 1) + name: name;
35 }
36
37 int main(int argc, char* argv[])
38 {
39 int errorCode = EXIT_SUCCESS;
40
41 // Read all arguments
42 PNG2AssetArguments arguments;
43
44 // Make sure we had no errors
45 if ((errorCode = processPNG2AssetArguments(argc, argv, &arguments)) != EXIT_SUCCESS) {
46 return errorCode;
47 }
48
49 // The png2AssetInstance tile and palette data is retained after
50 // processing source tilesets and so is shared with the main image
51 PNG2AssetData png2AssetInstance;
52
53 // If we have a source tileset
54 if (arguments.source_tilesets.size() > 0) {
55
56 vector<string>::iterator sourceTilesetFileNameIter = arguments.source_tilesets.begin();
57
58 // Iterate through each source tileset and execute
59 while (sourceTilesetFileNameIter < arguments.source_tilesets.end()) {
60 // Run with current source tileset filename
61 arguments.processing_mode = MODE_SOURCE_TILESET;
62 errorCode = png2AssetInstance.Execute(&arguments, extract_path(arguments.relative_paths, arguments.input_filename, *sourceTilesetFileNameIter));
63
64 // Return the error code if the function returns non-zero
65 if (errorCode != EXIT_SUCCESS) {
66 return errorCode;
67 }
68
69 sourceTilesetFileNameIter++;
70 }
71
72 // Save these values for later usage on the main execution
73 arguments.source_tileset_size = (unsigned int)png2AssetInstance.tiles.size();
74 arguments.source_total_color_count = png2AssetInstance.image.total_color_count;
75
76 // Clearing map and attributes isn't needed here since adding them is blocked for source tileset data
77 // (pre-refactor png2asset used map.clear() and map_attributes.clear() )
78
79 arguments.has_source_tilesets = true;
80 printf("Got %d tiles from the source tileset.\n", (unsigned int)arguments.source_tileset_size);
81 printf("Got %d palettes from the source tileset.\n", (unsigned int)(arguments.source_total_color_count / png2AssetInstance.image.colors_per_pal));
82 }
83
84 // If there is an entity tileset
85 if (!arguments.entity_tileset_filename.empty()) {
86 // Run with entity tileset filename
87 arguments.processing_mode = MODE_ENTITY_TILESET;
88 errorCode = png2AssetInstance.Execute(&arguments, extract_path(arguments.relative_paths, arguments.input_filename, arguments.entity_tileset_filename));
89
90 // Return the error code if the function returns non-zero
91 if (errorCode != EXIT_SUCCESS) {
92 return errorCode;
93 }
94
95 arguments.has_entity_tileset = true;
96 printf("Got %d tiles from the entity tileset.\n", (unsigned int)(unsigned int)png2AssetInstance.entity_tiles.size());
97 }
98
99 // Run the primary input file
100 // Return the error code if the function returns non-zero
101 arguments.processing_mode = MODE_MAIN_IMAGE;
102 if ((errorCode = png2AssetInstance.Execute(&arguments, arguments.input_filename)) != EXIT_SUCCESS) {
103 return errorCode;
104 }
105
106 return png2AssetInstance.Export();
107 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.