gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/png2asset/png2asset.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 "export_h_file.h"
15 #include "export_c_file.h"
16 #include "export_binary.h"
17 #include "map_attributes.h"
18 #include "palettes.h"
19
20 #include "cmp_int_color.h"
21
22 #include "png2asset.h"
23 #include "image_utils.h"
24 #include "image_data.h"
25 #include "maps.h"
26 #include "metasprites.h"
27 #include "metasprites_functions.h"
28 #include "process_arguments.h"
29 #include "png_image.h"
30 #include "tiles.h"
31
32 using namespace std;
33
34 int PNG2AssetData::Execute(PNG2AssetArguments* arguments, string input_filename){
35
36 this->args = arguments;
37
38 if(arguments->sprite_mode == SPR_8x8)
39 {
40 this->image.tile_w = 8;
41 this->image.tile_h = 8;
42 }
43 else if(arguments->sprite_mode == SPR_8x16)
44 {
45 this->image.tile_w = 8;
46 this->image.tile_h = 16;
47 }
48 else if(arguments->sprite_mode == SPR_16x16_MSX)
49 {
50 this->image.tile_w = 16;
51 this->image.tile_h = 16;
52 }
53
54 int errorCode = ReadImageData(this, input_filename);
55
56 // Return the error code if the function returns non-zero
57 if (errorCode != EXIT_SUCCESS) {
58 return errorCode;
59 }
60
61 // Extract the data depending on what type it is
62 if (this->args->processing_mode == MODE_ENTITY_TILESET) {
63 // Entity tileset extracts into a separate tileset
64 // For entity tilesets ALWAYS keep all of it's tiles, never deduplicate them
65 ExtractTileset(this, this->entity_tiles, true);
66 } else if (this->args->processing_mode == MODE_SOURCE_TILESET) {
67 // Source tileset extracts into the main shared tileset
68 ExtractTileset(this, this->tiles, this->args->keep_duplicate_tiles);
69 } else if (this->args->export_as_map)
70 GetMap(this);
71 else
72 GetAllMetasprites(this);
73
74 return EXIT_SUCCESS;
75 }
76
77 int PNG2AssetData::Export() {
78
79 // Header file export
80 if(export_h_file(this) == false) return EXIT_FAILURE;
81
82 if((this->args->export_as_map) && (this->args->output_binary)) {
83 // Handle special case of binary map export
84 export_map_binary(this);
85 }
86 else {
87 // Normal source file export
88 if(export_c_file(this) == false) return EXIT_FAILURE;
89 }
90
91 return EXIT_SUCCESS;
92 }
93
94
95 bool FindTile(const Tile& t, size_t& idx, unsigned char& props, vector< Tile > & tileset, PNG2AssetData* assetData)
96 {
97 vector< Tile >::iterator it;
98 it = find(tileset.begin(), tileset.end(), t);
99 if(it != tileset.end())
100 {
101 idx = (size_t)(it - tileset.begin());
102 props = assetData->args->props_default;
103 return true;
104 }
105 if(assetData->args->flip_tiles)
106 {
107 Tile tile = FlipV(t);
108 it = find(tileset.begin(), tileset.end(), tile);
109 if(it != tileset.end())
110 {
111 idx = (size_t)(it - tileset.begin());
112 props = assetData->args->props_default | (1 << 5);
113 return true;
114 }
115
116 tile = FlipH(tile);
117 it = find(tileset.begin(), tileset.end(), tile);
118 if(it != tileset.end())
119 {
120 idx = (size_t)(it - tileset.begin());
121 props = assetData->args->props_default | (1 << 5) | (1 << 6);
122 return true;
123 }
124
125 tile = FlipV(tile);
126 it = find(tileset.begin(), tileset.end(), tile);
127 if(it != tileset.end())
128 {
129 idx = (size_t)(it - tileset.begin());
130 props = assetData->args->props_default | (1 << 6);
131 return true;
132 }
133 }
134
135 return false;
136 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.