diff options
author | Bad Diode <bd@badd10de.dev> | 2021-04-20 09:39:07 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-04-20 09:39:07 +0200 |
commit | ca98cb0c5d3e38b8e12e2d8a3aa66b4cbe0cca3d (patch) | |
tree | 0a39ae26dbb7310a2d8813efdb8b08cbfadafda3 | |
parent | e280be223ea70a63d636809046f475183ffd38ed (diff) | |
download | gba-dev-tools-ca98cb0c5d3e38b8e12e2d8a3aa66b4cbe0cca3d.tar.gz gba-dev-tools-ca98cb0c5d3e38b8e12e2d8a3aa66b4cbe0cca3d.zip |
Clean up variable and macro naming
-rw-r--r-- | src/main.c | 98 |
1 files changed, 38 insertions, 60 deletions
@@ -5,9 +5,9 @@ | |||
5 | #define STB_IMAGE_IMPLEMENTATION | 5 | #define STB_IMAGE_IMPLEMENTATION |
6 | #include "stb_image.h" | 6 | #include "stb_image.h" |
7 | 7 | ||
8 | #define TILE_SIZE_X 8 | 8 | #define TILE_SIZE 8 |
9 | #define TILE_SIZE_Y 8 | 9 | #define DEFAULT_PALETTE_SIZE 16 |
10 | #define PALETTE_SIZE 16 | 10 | #define DEFAULT_BG_COLOR 0x0000 |
11 | 11 | ||
12 | typedef u16 Color; | 12 | typedef u16 Color; |
13 | 13 | ||
@@ -16,25 +16,19 @@ rgb15(u32 red, u32 green, u32 blue ) { | |||
16 | return (blue << 10) | (green << 5) | red; | 16 | return (blue << 10) | (green << 5) | red; |
17 | } | 17 | } |
18 | 18 | ||
19 | // TODO: Assuming 8bpp tiles. The tile is an index to a palette. | 19 | // TODO: Assuming 4bpp tiles. The tile is an index to a palette. |
20 | typedef struct Tile { | 20 | // TODO: Write more documentation about this. |
21 | u32 data[8]; | 21 | typedef u32 Tile[TILE_SIZE]; |
22 | } Tile; | ||
23 | |||
24 | typedef u16 Palette[PALETTE_SIZE]; | ||
25 | |||
26 | // TODO: Support passing a background parameter. | ||
27 | // TODO: Add option to flip sprites horizontally or vertically. | ||
28 | 22 | ||
29 | void | 23 | void |
30 | export_c_file(Tile *tiles, Palette palette, size_t n_tiles) { | 24 | export_c_file(Tile *tiles, Color *palette, size_t n_tiles, size_t palette_size) { |
31 | // Output tiles. | 25 | // Output tiles. |
32 | printf("u32 tiles[][8] = {\n"); | 26 | printf("u32 tiles[][%u] = {\n", TILE_SIZE); |
33 | for (size_t i = 0; i < n_tiles; ++i) { | 27 | for (size_t i = 0; i < n_tiles; ++i) { |
34 | printf(" {"); | 28 | printf(" {"); |
35 | for (size_t j = 0; j < 8; ++j) { | 29 | for (size_t j = 0; j < TILE_SIZE; ++j) { |
36 | printf("0x%08x", tiles[i].data[j]); | 30 | printf("0x%08x", tiles[i][j]); |
37 | if (j != (8 - 1)) { | 31 | if (j != (TILE_SIZE - 1)) { |
38 | printf(", "); | 32 | printf(", "); |
39 | } | 33 | } |
40 | } | 34 | } |
@@ -44,15 +38,17 @@ export_c_file(Tile *tiles, Palette palette, size_t n_tiles) { | |||
44 | printf("\n"); | 38 | printf("\n"); |
45 | 39 | ||
46 | // Output palette. | 40 | // Output palette. |
47 | printf("u16 palette[16] = {\n"); | 41 | printf("u16 palette[%u] = {\n", palette_size); |
48 | printf(" "); | 42 | printf(" "); |
49 | for (size_t i = 0; i < PALETTE_SIZE; ++i) { | 43 | size_t counter = 0; |
50 | if (i % 4 == 0 && i != 0) { | 44 | for (size_t i = 0; i < palette_size; ++i, ++counter) { |
45 | if (counter == 4) { | ||
46 | counter = 0; | ||
51 | printf("\n "); | 47 | printf("\n "); |
52 | } | 48 | } |
53 | printf("0x%04x", palette[i]); | 49 | printf("0x%04x,", palette[i]); |
54 | if (i != (PALETTE_SIZE - 1)) { | 50 | if (counter < 3) { |
55 | printf(", "); | 51 | printf(" "); |
56 | } | 52 | } |
57 | } | 53 | } |
58 | printf("\n"); | 54 | printf("\n"); |
@@ -70,7 +66,8 @@ print_usage(void) { | |||
70 | 66 | ||
71 | int | 67 | int |
72 | main(int argc, char *argv[]) { | 68 | main(int argc, char *argv[]) { |
73 | u16 background_color = 0x0000; | 69 | u16 background_color = DEFAULT_BG_COLOR; |
70 | u16 palette_size = DEFAULT_PALETTE_SIZE; | ||
74 | 71 | ||
75 | int option; | 72 | int option; |
76 | while ((option = getopt(argc, argv, "hb:")) != -1) { | 73 | while ((option = getopt(argc, argv, "hb:")) != -1) { |
@@ -81,15 +78,14 @@ main(int argc, char *argv[]) { | |||
81 | // number of parameters 8x8, 16x16, 16x8, etc. | 78 | // number of parameters 8x8, 16x16, 16x8, etc. |
82 | case 'b': { | 79 | case 'b': { |
83 | char *bg = optarg; | 80 | char *bg = optarg; |
84 | printf("Background color: %s\n", bg); | ||
85 | if (strlen(bg) != 6) { | 81 | if (strlen(bg) != 6) { |
86 | fprintf(stderr, "%s: Invalid background color.\n", BIN_NAME); | 82 | fprintf(stderr, "%s: Invalid background color.\n", BIN_NAME); |
87 | print_usage(); | 83 | print_usage(); |
88 | return EXIT_FAILURE; | 84 | return EXIT_FAILURE; |
89 | } | 85 | } |
90 | int red = 0; | 86 | unsigned int red = 0; |
91 | int green = 0; | 87 | unsigned int green = 0; |
92 | int blue = 0; | 88 | unsigned int blue = 0; |
93 | 89 | ||
94 | // Undefined behaviour, here we go! Also, no error checks, so if | 90 | // Undefined behaviour, here we go! Also, no error checks, so if |
95 | // an invalid number is given it will be parsed as 0. | 91 | // an invalid number is given it will be parsed as 0. |
@@ -126,8 +122,8 @@ main(int argc, char *argv[]) { | |||
126 | char *file_name = argv[optind]; | 122 | char *file_name = argv[optind]; |
127 | 123 | ||
128 | // Fill the palette with the background color if one was given. | 124 | // Fill the palette with the background color if one was given. |
129 | Palette palette; | 125 | Color *palette = malloc(palette_size * sizeof(Color)); |
130 | for (size_t i = 0; i < PALETTE_SIZE; ++i) { | 126 | for (size_t i = 0; i < palette_size; ++i) { |
131 | palette[i] = background_color; | 127 | palette[i] = background_color; |
132 | } | 128 | } |
133 | 129 | ||
@@ -145,17 +141,16 @@ main(int argc, char *argv[]) { | |||
145 | 141 | ||
146 | // TODO: Implement support for different file inputs. | 142 | // TODO: Implement support for different file inputs. |
147 | if (n != 3) { | 143 | if (n != 3) { |
148 | fprintf(stderr, "No support for this file format. Only supporting 3 channel files for now.\n"); | 144 | fprintf(stderr, "File format not supported. Only 3 channel files for now.\n"); |
149 | return EXIT_FAILURE; | 145 | return EXIT_FAILURE; |
150 | } | 146 | } |
151 | 147 | ||
152 | int n_tiles_x = x / TILE_SIZE_X; | 148 | int n_tiles_x = x / TILE_SIZE; |
153 | int n_tiles_y = y / TILE_SIZE_Y; | 149 | int n_tiles_y = y / TILE_SIZE; |
154 | int n_tiles = n_tiles_x * n_tiles_y; | 150 | int n_tiles = n_tiles_x * n_tiles_y; |
155 | 151 | ||
156 | // Allocate memory for the tiles in this file. | 152 | // Allocate memory for the tiles in this file. |
157 | Tile *tiles = malloc(n_tiles * sizeof(Tile)); | 153 | Tile *tiles = malloc(n_tiles * sizeof(Tile)); |
158 | // Palette palette = {0}; | ||
159 | 154 | ||
160 | // NOTE: We are going to brute-force this for now. Checking if the color is | 155 | // NOTE: We are going to brute-force this for now. Checking if the color is |
161 | // in the palette and add it to the next slot if possible. In the future we | 156 | // in the palette and add it to the next slot if possible. In the future we |
@@ -164,9 +159,9 @@ main(int argc, char *argv[]) { | |||
164 | int tile_offset_x = 0; | 159 | int tile_offset_x = 0; |
165 | int tile_offset_y = 0; | 160 | int tile_offset_y = 0; |
166 | for (size_t k = 0; k < n_tiles; ++k) { | 161 | for (size_t k = 0; k < n_tiles; ++k) { |
167 | for (size_t j = 0; j < TILE_SIZE_Y; ++j) { | 162 | for (size_t j = 0; j < TILE_SIZE; ++j) { |
168 | u32 col_index = 0x00000000; | 163 | u32 col_index = 0x00000000; |
169 | for (size_t i = 0; i < TILE_SIZE_X; ++i) { | 164 | for (size_t i = 0; i < TILE_SIZE; ++i) { |
170 | // Find the memory index for this pixel. | 165 | // Find the memory index for this pixel. |
171 | int idx = (i + tile_offset_x) + (j + tile_offset_y) * x; | 166 | int idx = (i + tile_offset_x) + (j + tile_offset_y) * x; |
172 | idx *= n; | 167 | idx *= n; |
@@ -182,7 +177,9 @@ main(int argc, char *argv[]) { | |||
182 | 177 | ||
183 | Color clr = rgb15(red, green, blue); | 178 | Color clr = rgb15(red, green, blue); |
184 | bool found = false; | 179 | bool found = false; |
185 | for (size_t p = 0; p < PALETTE_SIZE; ++p) { | 180 | // TODO: If the palette is full, find the closest perceived |
181 | // color instead. | ||
182 | for (size_t p = 0; p < palette_size; ++p) { | ||
186 | if (clr == palette[p]) { | 183 | if (clr == palette[p]) { |
187 | col_index |= (p & 0xF) << ((i) * 4) ; | 184 | col_index |= (p & 0xF) << ((i) * 4) ; |
188 | found = true; | 185 | found = true; |
@@ -194,35 +191,16 @@ main(int argc, char *argv[]) { | |||
194 | palette[pal_idx++] = clr; | 191 | palette[pal_idx++] = clr; |
195 | } | 192 | } |
196 | } | 193 | } |
197 | (tiles + k)->data[j] = col_index; | 194 | tiles[k][j] = col_index; |
198 | } | 195 | } |
199 | tile_offset_x += TILE_SIZE_X; | 196 | tile_offset_x += TILE_SIZE; |
200 | if (tile_offset_x >= x) { | 197 | if (tile_offset_x >= x) { |
201 | tile_offset_x = 0; | 198 | tile_offset_x = 0; |
202 | tile_offset_y += TILE_SIZE_Y; | 199 | tile_offset_y += TILE_SIZE; |
203 | } | 200 | } |
204 | } | 201 | } |
205 | 202 | ||
206 | // DEBUG:... | 203 | export_c_file(tiles, palette, n_tiles, palette_size); |
207 | // printf("PALETTE:\n"); | ||
208 | // for (size_t j = 0; j < PALETTE_SIZE; ++j) { | ||
209 | // printf("0x%x ", palette[j]); | ||
210 | // } | ||
211 | // printf("\n"); | ||
212 | |||
213 | // printf("TILES:\n"); | ||
214 | // for (size_t j = 0; j < n_tiles; ++j) { | ||
215 | // for (size_t i = 0; i < 8; ++i) { | ||
216 | // printf("0x%08x ", (tiles + j)->data[i]); | ||
217 | // } | ||
218 | // printf("\n"); | ||
219 | // } | ||
220 | |||
221 | // printf("x: %d y: %d n: %d\n", x, y, n); | ||
222 | // for (size_t i = 0; i < 3 * 8; ++i) { | ||
223 | // printf("0x%x\n", data[i]); | ||
224 | // } | ||
225 | export_c_file(tiles, palette, n_tiles); | ||
226 | stbi_image_free(data); | 204 | stbi_image_free(data); |
227 | return EXIT_SUCCESS; | 205 | return EXIT_SUCCESS; |
228 | } | 206 | } |