gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/png2asset/png_image.h
1 #pragma once
2 #include <vector>
3 #include "tiles.h"
4 using namespace std;
5
6 #define RGBA32_SZ 4 // RGBA 8:8:8:8 is 4 bytes per pixel
7
8 // ABGR:8888 (in 8 bit array format, OR RGBA:32 packed int on little-endian systems when accessed as bytes)
9 #define ABGR8_R 3 // Alpha channel is [3]
10 #define ABGR8_G 2 // Alpha channel in [2]
11 #define ABGR8_B 1 // Alpha channel in [1]
12 #define ABGR8_ALPHA 0 // Alpha channel in [0]
13
14 // RGBA:8888 (in 8 bit array format)
15 #define RGBA8_R 0 // Alpha channel is [0]
16 #define RGBA8_G 1 // Alpha channel in [1]
17 #define RGBA8_B 2 // Alpha channel in [2]
18 #define RGBA8_ALPHA 3 // Alpha channel in [3]
19
20 #define RGBA32(R,G,B,A) ((R << 24) | (G << 16) | (B << 8) | A)
21 #define RGB24(R,G,B) ((R << 16) | (G << 8) | B)
22
23 #define ALPHA_FULLY_TRANSPARENT 0 // Full alpha channel transparency
24 #define RGBA32_TRANSPARENT_WHITE (RGBA32(255,255,255,ALPHA_FULLY_TRANSPARENT)) // White, full transparency
25
26 #define MAX(A,B) ((A)>(B)?(A):(B))
27
28 enum {
29 SPR_NONE,
30 SPR_8x8,
31 SPR_8x16,
32 SPR_16x16_MSX
33 };
34
35 // processing_mode states
36 enum {
37 MODE_MAIN_IMAGE,
38 MODE_SOURCE_TILESET,
39 MODE_ENTITY_TILESET
40 };
41
42 struct PNGImage
43 {
44 vector< unsigned char > data; //data in indexed format
45 unsigned int w;
46 unsigned int h;
47
48 // Default tile size
49 int tile_w = 8;
50 int tile_h = 16;
51
52 // TODO: embed these instead of deriving them many places in the code
53 // int attribute_w_factor = 1;
54 // int attribute_h_factor = 1;
55 // int get_attribute_tile_w() { tile_w * attribute_w_factor; }
56 // int get_attribute_tile_h() { tile_h * attribute_h_factor; }
57
58 size_t colors_per_pal; // Number of colors per palette (ex: CGB has 4 colors per palette x 8 palettes total)
59 size_t total_color_count; // Total number of colors across all palettes (palette_count x colors_per_pal)
60 unsigned char * palette = NULL; //palette colors in RGBA (1 color == 4 bytes)
61 unsigned char * source_tileset_palette = NULL; // Mostly used for ensuring source tileset and primary image palettes match sufficiently
62
63 public:
64 // This needs separate tile_w and tile_h params since
65 // MSX tile extraction uses it to pull out the 4 sub-tiles
66 bool ExtractGBTile(int x, int y, int extract_tile_w, int extract_tile_h, Tile& tile, int buffer_offset)
67 {
68 tile.pal = 0;
69 bool all_zero = true;
70 for(int j = 0; j < extract_tile_h; ++j)
71 {
72 for(int i = 0; i < extract_tile_w; ++i)
73 {
74 unsigned char color_idx = data[w * (y + j) + (x + i)];
75 tile.data[(j * extract_tile_w) + i + buffer_offset] = color_idx % colors_per_pal;
76 tile.pal = MAX((color_idx / colors_per_pal), tile.pal); // detect palette by maximum
77 all_zero = all_zero && ((color_idx % colors_per_pal) == 0); // don't take palette into account for the empty tiles
78 }
79 }
80 return !all_zero;
81 }
82
83 bool ExtractTile_MSX16x16(int x, int y, Tile& tile)
84 {
85 // MSX 16x16 sprite tiles are composed of four 8x8 tiles in this order UL, LL, UR, LR
86 bool UL_notempty, LL_notempty, UR_notempty, LR_notempty;
87
88 // Call these separately since otherwise some get optimized out during
89 // runtime if any single one before it returns false
90 UL_notempty = ExtractGBTile(x, y, 8, 8, tile, 0);
91 LL_notempty = ExtractGBTile(x, y + 8, 8, 8, tile, ((8 * 8) * 1));
92 UR_notempty = ExtractGBTile(x + 8, y, 8, 8, tile, ((8 * 8) * 2));
93 LR_notempty = ExtractGBTile(x + 8, y + 8, 8, 8, tile, ((8 * 8) * 3));
94 return (UL_notempty || LL_notempty || UR_notempty || LR_notempty);
95 }
96
97 bool ExtractTile(int x, int y, Tile& tile, int sprite_mode, bool export_as_map, bool use_map_attributes)
98 {
99 if(sprite_mode == SPR_16x16_MSX)
100 return ExtractTile_MSX16x16(x, y, tile);
101 else
102 return ExtractGBTile(x, y, tile_w, tile_h, tile, 0); // No buffer offset for normal tile extraction
103 }
104 // private:
105 // bool zero_palette = false;
106 };
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.