summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-04-20 11:55:27 +0200
committerBad Diode <bd@badd10de.dev>2021-04-20 11:55:27 +0200
commita1ba114f1cd89ceda63cc8d32e0f3facf401dc39 (patch)
tree496526ec9d7c146438d0529a1fe5d94482ea8fd9
parente649308594354a6fc3553e9faeb733e3776d3c26 (diff)
downloadgba-dev-tools-a1ba114f1cd89ceda63cc8d32e0f3facf401dc39.tar.gz
gba-dev-tools-a1ba114f1cd89ceda63cc8d32e0f3facf401dc39.zip
Add option to output to file directly instead of stdout
-rw-r--r--src/main.c60
1 files changed, 38 insertions, 22 deletions
diff --git a/src/main.c b/src/main.c
index ae4cf76..dcc9075 100644
--- a/src/main.c
+++ b/src/main.c
@@ -40,38 +40,38 @@ typedef struct Image {
40} Image; 40} Image;
41 41
42void 42void
43export_c_file(Tiles *tiles, Palette *palette) { 43export_c_file(Tiles *tiles, Palette *palette, FILE *file) {
44 // Output tiles. 44 // Output tiles.
45 printf("u32 tiles[%u][%u] = {\n", tiles->capacity, TILE_SIZE); 45 fprintf(file, "u32 tiles[%u][%u] = {\n", tiles->capacity, TILE_SIZE);
46 for (size_t i = 0; i < tiles->capacity; ++i) { 46 for (size_t i = 0; i < tiles->capacity; ++i) {
47 printf(" {"); 47 fprintf(file, " {");
48 for (size_t j = 0; j < TILE_SIZE; ++j) { 48 for (size_t j = 0; j < TILE_SIZE; ++j) {
49 printf("0x%08x", tiles->data[i][j]); 49 fprintf(file, "0x%08x", tiles->data[i][j]);
50 if (j != (TILE_SIZE - 1)) { 50 if (j != (TILE_SIZE - 1)) {
51 printf(", "); 51 fprintf(file, ", ");
52 } 52 }
53 } 53 }
54 printf("},\n"); 54 fprintf(file, "},\n");
55 } 55 }
56 printf("};\n"); 56 fprintf(file, "};\n");
57 printf("\n"); 57 fprintf(file, "\n");
58 58
59 // Output palette. 59 // Output palette.
60 printf("u16 palette[%u] = {\n", palette->capacity); 60 fprintf(file, "u16 palette[%u] = {\n", palette->capacity);
61 printf(" "); 61 fprintf(file, " ");
62 size_t counter = 0; 62 size_t counter = 0;
63 for (size_t i = 0; i < palette->capacity; ++i, ++counter) { 63 for (size_t i = 0; i < palette->capacity; ++i, ++counter) {
64 if (counter == 4) { 64 if (counter == 4) {
65 counter = 0; 65 counter = 0;
66 printf("\n "); 66 fprintf(file, "\n ");
67 } 67 }
68 printf("0x%04x,", palette->data[i]); 68 fprintf(file, "0x%04x,", palette->data[i]);
69 if (counter < 3) { 69 if (counter < 3) {
70 printf(" "); 70 fprintf(file, " ");
71 } 71 }
72 } 72 }
73 printf("\n"); 73 fprintf(file, "\n");
74 printf("};\n"); 74 fprintf(file, "};\n");
75} 75}
76 76
77void 77void
@@ -79,8 +79,8 @@ print_usage(void) {
79 printf("Usage: %s [options] <filename>\n", BIN_NAME); 79 printf("Usage: %s [options] <filename>\n", BIN_NAME);
80 printf("\n"); 80 printf("\n");
81 printf("\t-b <RRGGBB>\tSelects the background color used for transparency.\n"); 81 printf("\t-b <RRGGBB>\tSelects the background color used for transparency.\n");
82 printf("\t-o <out_file.c>\tPath to the output file. If blank, stdout will be used.\n");
82 printf("\n"); 83 printf("\n");
83 // TODO: Print valid options as a suggestion.
84} 84}
85 85
86void 86void
@@ -153,9 +153,10 @@ int
153main(int argc, char *argv[]) { 153main(int argc, char *argv[]) {
154 u16 background_color = DEFAULT_BG_COLOR; 154 u16 background_color = DEFAULT_BG_COLOR;
155 u16 palette_size = DEFAULT_PALETTE_SIZE; 155 u16 palette_size = DEFAULT_PALETTE_SIZE;
156 char *out_file_path = NULL;
156 157
157 int option; 158 int option;
158 while ((option = getopt(argc, argv, "hb:")) != -1) { 159 while ((option = getopt(argc, argv, "hb:o:")) != -1) {
159 switch (option) { 160 switch (option) {
160 // TODO: -o output file name. If none is given, the output will be 161 // TODO: -o output file name. If none is given, the output will be
161 // redirected to stdout. 162 // redirected to stdout.
@@ -187,6 +188,9 @@ main(int argc, char *argv[]) {
187 blue = (blue * 31 / 255); 188 blue = (blue * 31 / 255);
188 background_color = rgb15(red, green, blue); 189 background_color = rgb15(red, green, blue);
189 } break; 190 } break;
191 case 'o': {
192 out_file_path = optarg;
193 } break;
190 case 'h': { 194 case 'h': {
191 print_usage(); 195 print_usage();
192 return EXIT_SUCCESS; 196 return EXIT_SUCCESS;
@@ -219,11 +223,12 @@ main(int argc, char *argv[]) {
219 Image img = {0}; 223 Image img = {0};
220 224
221 // Open the given file. 225 // Open the given file.
222 img.data = stbi_load(file_name, &img.width, &img.height, &img.n_channels, 0); 226 FILE *input_file = fopen(file_name, "rb");
223 if (img.data == NULL) { 227 if (input_file == NULL) {
224 fprintf(stderr, "Error: can't open the given file.\n"); 228 fprintf(stderr, "%s: can't open input file: %s\n", BIN_NAME, file_name);
225 return EXIT_FAILURE; 229 return EXIT_FAILURE;
226 } 230 }
231 img.data = stbi_load_from_file(input_file, &img.width, &img.height, &img.n_channels, 0);
227 232
228 // TODO: Implement support for different file inputs. 233 // TODO: Implement support for different file inputs.
229 if (img.n_channels != 3) { 234 if (img.n_channels != 3) {
@@ -254,9 +259,20 @@ main(int argc, char *argv[]) {
254 } 259 }
255 } 260 }
256 261
257 export_c_file(&tiles, &palette); 262 if (out_file_path == NULL) {
263 export_c_file(&tiles, &palette, stdout);
264 } else {
265 FILE *file = fopen(out_file_path, "w");
266 if (!file) {
267 fprintf(stderr, "%s: can't open output file: %s\n", out_file_path);
268 return EXIT_FAILURE;
269 }
270 export_c_file(&tiles, &palette, file);
271 }
258 272
259 // TODO: Cleanup other resources. 273 // Cleanup resources and exit.
274 free(tiles.data);
275 free(palette.data);
260 stbi_image_free(img.data); 276 stbi_image_free(img.data);
261 return EXIT_SUCCESS; 277 return EXIT_SUCCESS;
262} 278}