Debian Codegraph @

res/asset_packer/src/asset_packer.c

raw
1 #include "log.h"
2
3 #include "assets/assets.h"
4 #include "assets/group.h"
5 #include "assets/image.h"
6 #include "assets/layer.h"
7 #include "assets/xml.h"
8 #include "core/array.h"
9 #include "core/buffer.h"
10 #include "core/dir.h"
11 #include "core/file.h"
12 #include "core/image_packer.h"
13 #include "core/io.h"
14 #include "core/png_read.h"
15 #include "core/string.h"
16 #include "core/xml_exporter.h"
17 #include "graphics/color.h"
18 #include "platform/file_manager.h"
19
20 #include "png.h"
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifdef _WIN32
30 #include <windows.h>
31 #else
32 #include <errno.h>
33 #include <sys/stat.h>
34 #endif
35
36 #define ASSETS_IMAGE_SIZE 2048
37 #define CURSOR_IMAGE_SIZE 256
38 #define PACKED_ASSETS_DIR "packed_assets"
39 #define CURSORS_DIR "Cursors"
40 #define CURSORS_NAME "Color_Cursors"
41 #define BYTES_PER_PIXEL 4
42
43 #ifdef FORMAT_XML
44 #define FORMAT_NEWLINE "\n"
45 #define FORMAT_IDENT " "
46 #else
47 #define FORMAT_NEWLINE ""
48 #define FORMAT_IDENT ""
49 #endif
50
51 static const char *LAYER_PART[] = { "footprint", "top" };
52 static const char *LAYER_ROTATE[] = { "90", "180", "270" };
53 static const char *LAYER_INVERT[] = { "horizontal", "vertical", "both" };
54 static const char *LAYER_MASK[] = { "grayscale", "alpha" };
55
56 static char current_file[FILE_NAME_MAX];
57
58 static color_t *final_image_pixels;
59 static unsigned int final_image_width;
60 static unsigned int final_image_height;
61
62 typedef struct {
63 int id;
64 const char *path;
65 image_packer_rect *rect;
66 color_t *pixels;
67 } packed_asset;
68
69 #define PACKED_ASSETS_BLOCK_SIZE 256
70
71 static array(packed_asset) packed_assets;
72
73 static int remove_file(const char *filename, long unused)
74 {
75 snprintf(current_file, FILE_NAME_MAX, "%s/%s", PACKED_ASSETS_DIR, filename);
76 platform_file_manager_remove_file(current_file);
77 return LIST_CONTINUE;
78 }
79
80 static int find_packed_assets_dir(const char *dir, long unused)
81 {
82 return strcmp(dir, PACKED_ASSETS_DIR) == 0 ? LIST_MATCH : LIST_NO_MATCH;
83 }
84
85 static int prepare_packed_assets_dir(void)
86 {
87 if (platform_file_manager_list_directory_contents(0, TYPE_DIR, 0, find_packed_assets_dir) == LIST_MATCH) {
88 log_info("The packed assets dir exists, deleting its contents", 0, 0);
89 if (!platform_file_manager_remove_directory(PACKED_ASSETS_DIR)) {
90 log_error("There was a problem deleting the packed assets directory.", 0, 0);
91 return 0;
92 }
93 }
94 if (!platform_file_manager_create_directory(PACKED_ASSETS_DIR "/" ASSETS_IMAGE_PATH, 1) ||
95 !platform_file_manager_create_directory(PACKED_ASSETS_DIR "/" CURSORS_DIR, 1)) {
96 log_error("Failed to create directories", 0, 0);
97 return 0;
98 }
99 return 1;
100 }
101
102 static void add_attribute_int(const char *name, int value)
103 {
104 if (value != 0) {
105 xml_exporter_add_attribute_int(name, value);
106 }
107 }
108
109 static void add_attribute_bool(const char *name, int value, const char *expression_if_true)
110 {
111 if (value != 0) {
112 xml_exporter_add_attribute_text(name, string_from_ascii(expression_if_true));
113 }
114 }
115
116 static void add_attribute_enum(const char *name, int value, const char **display_value, int max_values)
117 {
118 if (value > 0 && value <= max_values) {
119 xml_exporter_add_attribute_text(name, string_from_ascii(display_value[value - 1]));
120 }
121 }
122
123 static void add_attribute_string(const char *name, const char *value)
124 {
125 if (value && *value != 0) {
126 xml_exporter_add_attribute_text(name, string_from_ascii(value));
127 }
128 }
129
130 static void create_image_xml_line(const asset_image *image)
131 {
132 xml_exporter_new_element("image", 1);
133 add_attribute_string("id", image->id);
134 if (image->has_defined_size) {
135 add_attribute_int("width", image->img.width);
136 add_attribute_int("height", image->img.height);
137 }
138 add_attribute_bool("isometric", image->img.is_isometric, "true");
139 }
140
141 static void create_layer_xml_line(const layer *l)
142 {
143 xml_exporter_new_element("layer", 1);
144
145 add_attribute_string("group", l->original_image_group);
146 add_attribute_string("image", l->original_image_id);
147 add_attribute_int("src_x", l->src_x);
148 add_attribute_int("src_y", l->src_y);
149 add_attribute_int("x", l->x_offset);
150 add_attribute_int("y", l->y_offset);
151 add_attribute_int("width", l->width);
152 add_attribute_int("height", l->height);
153 add_attribute_enum("invert", l->invert, LAYER_INVERT, 3);
154 add_attribute_enum("rotate", l->rotate, LAYER_ROTATE, 3);
155 add_attribute_enum("part", l->part, LAYER_PART, 2);
156 add_attribute_enum("mask", l->mask, LAYER_MASK, 2);
157
158 xml_exporter_close_element(0);
159 }
160
161 static void create_animation_xml_line(const asset_image *image)
162 {
163 xml_exporter_new_element("animation", 1);
164
165 if (!image->has_frame_elements) {
166 add_attribute_int("frames", image->img.animation->num_sprites);
167 }
168 add_attribute_int("speed", image->img.animation->speed_id);
169 add_attribute_int("x", image->img.animation->sprite_offset_x);
170 add_attribute_int("y", image->img.animation->sprite_offset_y);
171 add_attribute_bool("reversible", image->img.animation->can_reverse, "true");
172 }
173
174 static void create_frame_xml_line(const layer *l)
175 {
176 xml_exporter_new_element("frame", 1);
177
178 add_attribute_string("group", l->original_image_group);
179 add_attribute_string("image", l->original_image_id);
180 add_attribute_int("src_x", l->src_x);
181 add_attribute_int("src_y", l->src_y);
182 add_attribute_int("width", l->width);
183 add_attribute_int("height", l->height);
184 add_attribute_enum("invert", l->invert, LAYER_INVERT, 3);
185 add_attribute_enum("rotate", l->rotate, LAYER_ROTATE, 3);
186
187 xml_exporter_close_element(0);
188 }
189
190 void new_packed_asset(packed_asset *asset, int index)
191 {
192 asset->id = index;
193 }
194
195 int packed_asset_active(const packed_asset *asset)
196 {
197 return asset->path != 0;
198 }
199
200 static packed_asset *get_asset_image_from_list(const layer *l)
201 {
202 packed_asset *asset;
203 array_foreach(packed_assets, asset)
204 {
205 if (strcmp(l->asset_image_path, asset->path) == 0) {
206 return asset;
207 }
208 }
209 return 0;
210 }
211
212 static void add_asset_image_to_list(layer *l)
213 {
214 packed_asset *asset = get_asset_image_from_list(l);
215 if (!asset) {
216 array_new_item(packed_assets, 0, asset);
217 if (!asset) {
218 log_error("Out of memory.", 0, 0);
219 return;
220 }
221 asset->path = l->asset_image_path;
222 }
223 l->calculated_image_id = asset->id;
224 }
225
226 static void get_assets_for_group(int group_id)
227 {
228 const image_groups *group = group_get_from_id(group_id);
229 for (int image_id = group->first_image_index; image_id <= group->last_image_index; image_id++) {
230 asset_image *image = asset_image_get_from_id(image_id);
231 for (layer *l = &image->first_layer; l; l = l->next) {
232 if (l->asset_image_path) {
233 add_asset_image_to_list(l);
234 }
235 }
236 }
237 }
238
239 static void populate_asset_rects(image_packer *packer)
240 {
241 packed_asset *asset;
242 array_foreach(packed_assets, asset)
243 {
244 int width, height;
245 asset->rect = &packer->rects[asset->id];
246 if (!png_get_image_size(asset->path, &width, &height)) {
247 continue;
248 }
249 if (!width || !height) {
250 continue;
251 }
252 asset->pixels = malloc(sizeof(color_t) * width * height);
253 if (!asset->pixels) {
254 log_error("Out of memory.", 0, 0);
255 continue;
256 }
257 if (!png_read(asset->path, asset->pixels, 0, 0, width, height, 0, 0, width, 0)) {
258 free(asset->pixels);
259 asset->pixels = 0;
260 continue;
261 }
262 asset->rect->input.width = width;
263 asset->rect->input.height = height;
264 }
265 }
266
267 static void copy_to_final_image(const color_t *pixels, const image_packer_rect *rect)
268 {
269 if (!rect->output.rotated) {
270 for (unsigned int y = 0; y < rect->input.height; y++) {
271 const color_t *src_pixel = &pixels[y * rect->input.width];
272 color_t *dst_pixel = &final_image_pixels[(y + rect->output.y) * final_image_width + rect->output.x];
273 memcpy(dst_pixel, src_pixel, rect->input.width * sizeof(color_t));
274 }
275 } else {
276 for (unsigned int y = 0; y < rect->input.height; y++) {
277 const color_t *src_pixel = &pixels[y * rect->input.width];
278 color_t *dst_pixel = &final_image_pixels[(rect->output.y + rect->input.width - 1) *
279 final_image_width + y + rect->output.x];
280 for (unsigned int x = 0; x < rect->input.width; x++) {
281 *dst_pixel = *src_pixel++;
282 dst_pixel -= final_image_width;
283 }
284 }
285 }
286 }
287
288 static void create_final_image(const image_packer *packer)
289 {
290 packed_asset *asset;
291 array_foreach(packed_assets, asset)
292 {
293 copy_to_final_image(asset->pixels, asset->rect);
294 }
295 }
296
297 static void save_final_image(const char *path, unsigned int width, unsigned int height, const color_t *pixels)
298 {
299 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
300
301 if (!png_ptr) {
302 log_error("Error creating png structure for", path, 0);
303 return;
304 }
305 png_infop info_ptr = png_create_info_struct(png_ptr);
306 if (!info_ptr) {
307 log_error("Error creating png structure for", path, 0);
308 png_destroy_write_struct(&png_ptr, &info_ptr);
309 return;
310 }
311 png_set_compression_level(png_ptr, 3);
312
313 FILE *fp = fopen(path, "wb");
314 if (!fp) {
315 log_error("Error creating final png file at", path, 0);
316 png_destroy_write_struct(&png_ptr, &info_ptr);
317 return;
318 }
319 png_init_io(png_ptr, fp);
320
321 if (setjmp(png_jmpbuf(png_ptr))) {
322 log_error("Error constructing png file", path, 0);
323 fclose(fp);
324 png_destroy_write_struct(&png_ptr, &info_ptr);
325 return;
326 }
327 png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGBA,
328 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
329 png_write_info(png_ptr, info_ptr);
330
331 uint8_t *row_pixels = malloc(width * BYTES_PER_PIXEL);
332 if (!row_pixels) {
333 log_error("Out of memory for png creation", path, 0);
334 fclose(fp);
335 png_destroy_write_struct(&png_ptr, &info_ptr);
336 return;
337 }
338 memset(row_pixels, 0, width * BYTES_PER_PIXEL);
339
340 if (setjmp(png_jmpbuf(png_ptr))) {
341 log_error("Error constructing png file", path, 0);
342 free(row_pixels);
343 fclose(fp);
344 png_destroy_write_struct(&png_ptr, &info_ptr);
345 return;
346 }
347 for (unsigned int y = 0; y < height; ++y) {
348 uint8_t *pixel = row_pixels;
349 for (unsigned int x = 0; x < width; x++) {
350 color_t input = pixels[y * width + x];
351 *(pixel + 0) = (uint8_t) COLOR_COMPONENT(input, COLOR_BITSHIFT_RED);
352 *(pixel + 1) = (uint8_t) COLOR_COMPONENT(input, COLOR_BITSHIFT_GREEN);
353 *(pixel + 2) = (uint8_t) COLOR_COMPONENT(input, COLOR_BITSHIFT_BLUE);
354 *(pixel + 3) = (uint8_t) COLOR_COMPONENT(input, COLOR_BITSHIFT_ALPHA);
355 pixel += BYTES_PER_PIXEL;
356 }
357 png_write_row(png_ptr, row_pixels);
358 }
359 png_write_end(png_ptr, info_ptr);
360
361 free(row_pixels);
362 fclose(fp);
363 png_destroy_write_struct(&png_ptr, &info_ptr);
364 }
365
366 static void pack_layer(const image_packer *packer, layer *l)
367 {
368 if (!l->asset_image_path) {
369 return;
370 }
371 image_packer_rect *rect = &packer->rects[l->calculated_image_id];
372 l->src_x = rect->output.x;
373 l->src_y = rect->output.y;
374 if (!rect->output.rotated) {
375 l->width = rect->input.width;
376 l->height = rect->input.height;
377 }
378 if (rect->output.rotated) {
379 l->width = rect->input.height;
380 l->height = rect->input.width;
381 switch (l->rotate) {
382 case ROTATE_90_DEGREES:
383 l->rotate = ROTATE_180_DEGREES;
384 break;
385 case ROTATE_180_DEGREES:
386 l->rotate = ROTATE_270_DEGREES;
387 break;
388 case ROTATE_270_DEGREES:
389 l->rotate = ROTATE_NONE;
390 break;
391 case ROTATE_NONE:
392 default:
393 l->rotate = ROTATE_90_DEGREES;
394 break;
395 }
396 }
397 }
398
399 static void pack_group(int group_id)
400 {
401 const image_groups *group = group_get_from_id(group_id);
402
403 if (!group || !*group->name) {
404 log_error("Could not retreive a valid group from id", 0, group_id);
405 return;
406 }
407
408 array_init(packed_assets, PACKED_ASSETS_BLOCK_SIZE, new_packed_asset, packed_asset_active);
409
410 get_assets_for_group(group_id);
411
412 image_packer packer;
413 image_packer_init(&packer, packed_assets.size, ASSETS_IMAGE_SIZE, ASSETS_IMAGE_SIZE);
414
415 packer.options.allow_rotation = 1;
416 packer.options.reduce_image_size = 1;
417
418 log_info("Packing", group->name, 0);
419
420 populate_asset_rects(&packer);
421
422 if (image_packer_pack(&packer) != packed_assets.size) {
423 log_error("Error during pack.", 0, 0);
424 image_packer_free(&packer);
425 return;
426 }
427
428 final_image_width = packer.result.last_image_width;
429 final_image_height = packer.result.last_image_height;
430 final_image_pixels = malloc(sizeof(color_t) * final_image_width * final_image_height);
431 if (!final_image_pixels) {
432 log_error("Out of memory when creating the final image.", 0, 0);
433 image_packer_free(&packer);
434 return;
435 }
436 memset(final_image_pixels, 0, sizeof(color_t) * final_image_width * final_image_height);
437
438 create_final_image(&packer);
439
440 printf("Info: %d Images packed. Texture size: %dx%d.\n", packed_assets.size,
441 packer.result.last_image_width, packer.result.last_image_height);
442
443 log_info("Creating xml file...", 0, 0);
444
445 snprintf(current_file, FILE_NAME_MAX, "%s/%s/%s", PACKED_ASSETS_DIR, ASSETS_IMAGE_PATH, group->path);
446
447 buffer buf;
448 int buf_size = 5 * 1024 * 1024;
449 uint8_t *buf_data = malloc(buf_size);
450 buffer_init(&buf, buf_data, buf_size);
451 xml_exporter_init(&buf, "assetlist");
452 xml_exporter_add_text(string_from_ascii("<!-- XML auto packed by asset_packer. DO NOT use as a reference."));
453 xml_exporter_newline();
454 xml_exporter_add_text(string_from_ascii(" Use the assets directory from the source code instead. -->"));
455 xml_exporter_newline();
456 xml_exporter_newline();
457 xml_exporter_new_element("assetlist", 0);
458 xml_exporter_add_attribute_text("name", string_from_ascii(group->name));
459
460 for (int image_id = group->first_image_index; image_id <= group->last_image_index; image_id++) {
461 asset_image *image = asset_image_get_from_id(image_id);
462 create_image_xml_line(image);
463 for (layer *l = &image->first_layer; l; l = l->next) {
464 pack_layer(&packer, l);
465 create_layer_xml_line(l);
466 }
467 if (image->img.animation) {
468 create_animation_xml_line(image);
469 if (image->has_frame_elements) {
470 for (int i = 0; i < image->img.animation->num_sprites; i++) {
471 image_id++;
472 asset_image *frame = asset_image_get_from_id(image_id);
473 layer *l = frame->last_layer;
474 pack_layer(&packer, l);
475 create_frame_xml_line(l);
476 }
477 }
478 xml_exporter_close_element(0);
479 }
480 xml_exporter_close_element(0);
481 }
482
483 xml_exporter_close_element(0);
484
485 packed_asset *asset;
486 array_foreach(packed_assets, asset)
487 {
488 free(asset->pixels);
489 }
490
491 FILE *xml_dest = fopen(current_file, "wb");
492
493 if (!xml_dest) {
494 log_error("Failed to create file", group->path, 0);
495 return;
496 }
497
498 fwrite(buf.data, 1, (size_t) buf.index, xml_dest);
499
500 fclose(xml_dest);
501 image_packer_free(&packer);
502 free(buf_data);
503
504 snprintf(current_file, FILE_NAME_MAX, "%s/%s/%s.png", PACKED_ASSETS_DIR, ASSETS_IMAGE_PATH, group->name);
505
506 log_info("Creating png file...", 0, 0);
507
508 save_final_image(current_file, final_image_width, final_image_height, final_image_pixels);
509
510 free(final_image_pixels);
511 }
512
513 static void pack_cursors(void)
514 {
515 static const char *cursor_names[] = { "Arrow", "Shovel", "Sword" };
516 static const char *cursor_sizes[] = { "150", "200" };
517
518 #define NUM_CURSOR_NAMES (sizeof(cursor_names) / sizeof(cursor_names[0]))
519 #define NUM_CURSOR_SIZES (sizeof(cursor_sizes) / sizeof(cursor_sizes[0]) + 1)
520
521 static layer cursors[NUM_CURSOR_NAMES * NUM_CURSOR_SIZES];
522
523 image_packer packer;
524 image_packer_init(&packer, NUM_CURSOR_NAMES * NUM_CURSOR_SIZES, CURSOR_IMAGE_SIZE, CURSOR_IMAGE_SIZE);
525
526 packer.options.allow_rotation = 1;
527 packer.options.reduce_image_size = 1;
528 packer.options.sort_by = IMAGE_PACKER_SORT_BY_AREA;
529
530 for (int i = 0; i < NUM_CURSOR_NAMES; i++) {
531 for (int j = 0; j < NUM_CURSOR_SIZES; j++) {
532 int index = i * NUM_CURSOR_SIZES + j;
533 layer *cursor = &cursors[index];
534 cursor->calculated_image_id = index;
535 cursor->asset_image_path = malloc(FILE_NAME_MAX);
536 if (!cursor->asset_image_path) {
537 log_error("Out of memory.", 0, 0);
538 image_packer_free(&packer);
539 return;
540 }
541 if (j > 0) {
542 snprintf(cursor->asset_image_path, FILE_NAME_MAX, "%s/%s/%s_%s.png", CURSORS_DIR, CURSORS_NAME,
543 cursor_names[i], cursor_sizes[j - 1]);
544 } else {
545 snprintf(cursor->asset_image_path, FILE_NAME_MAX, "%s/%s/%s.png", CURSORS_DIR, CURSORS_NAME,
546 cursor_names[i]);
547 }
548 if (!png_get_image_size(cursor->asset_image_path, &cursor->width, &cursor->height)) {
549 image_packer_free(&packer);
550 return;
551 }
552 color_t *data = malloc(cursor->width * cursor->height * sizeof(color_t));
553 if (!data) {
554 log_error("Out of memory.", 0, 0);
555 image_packer_free(&packer);
556 return;
557 }
558 png_read(cursor->asset_image_path, data, 0, 0,
559 cursor->width, cursor->height, 0, 0, cursor->width, 0);
560 packer.rects[index].input.width = cursor->width;
561 packer.rects[index].input.height = cursor->height;
562 cursor->data = data;
563 }
564 }
565
566 image_packer_pack(&packer);
567
568 final_image_width = packer.result.last_image_width;
569 final_image_height = packer.result.last_image_height;
570 final_image_pixels = malloc(sizeof(color_t) * final_image_width * final_image_height);
571 if (!final_image_pixels) {
572 log_error("Out of memory when creating the final cursor image.", 0, 0);
573 image_packer_free(&packer);
574 return;
575 }
576 memset(final_image_pixels, 0, sizeof(color_t) * final_image_width * final_image_height);
577
578 log_info("Cursor positions and sizes in packed image:", 0, 0);
579
580 printf(" Name x y width height\n");
581
582 for (int i = 0; i < NUM_CURSOR_NAMES * NUM_CURSOR_SIZES; i++) {
583 layer *cursor = &cursors[i];
584 pack_layer(&packer, cursor);
585 copy_to_final_image(cursor->data, &packer.rects[i]);
586 printf("%-16s %3d %3d %3d %3d\n",
587 cursor->asset_image_path + strlen(CURSORS_DIR) + strlen(CURSORS_NAME) + 2,
588 packer.rects[i].output.x, packer.rects[i].output.y, cursor->width, cursor->height);
589 }
590
591 snprintf(current_file, FILE_NAME_MAX, "%s/%s/%s.png", PACKED_ASSETS_DIR, CURSORS_DIR, CURSORS_NAME);
592
593 save_final_image(current_file, final_image_width, final_image_height, final_image_pixels);
594
595 free(final_image_pixels);
596
597 image_packer_free(&packer);
598 }
599
600 int main(int argc, char **argv)
601 {
602 int using_custom_path = 0;
603 if (argc == 2) {
604 log_info("Attempting to use the path", argv[1], 0);
605 if (!platform_file_manager_set_base_path(argv[1])) {
606 log_info("Unable to change the base path. Attempting to run from local directory...", 0, 0);
607 } else {
608 using_custom_path = 1;
609 }
610 }
611 const dir_listing *xml_files = dir_find_files_with_extension(ASSETS_DIRECTORY "/" ASSETS_IMAGE_PATH, "xml");
612 if (xml_files->num_files == 0) {
613 if (using_custom_path) {
614 log_error("No assets found on", argv[1], 0);
615 }
616 log_error("Please add a valid assets folder to this directory.\n"
617 "Alternatively, you can run as:\n\n"
618 "asset_packer.exe [WORK_DIRECTORY]\n\n"
619 "where WORK_DIRECTORY is the directory where the assets folder is in.", 0, 0);
620 return 1;
621 }
622
623 if (!prepare_packed_assets_dir()) {
624 return 2;
625 }
626
627 #ifdef PACK_XMLS
628 if (!group_create_all(xml_files->num_files) || !asset_image_init_array()) {
629 log_error("Not enough memory to initialize extra assets.", 0, 0);
630 return 3;
631 }
632
633 xml_init();
634
635 for (int i = 0; i < xml_files->num_files; ++i) {
636 xml_process_assetlist_file(xml_files->files[i].name);
637 }
638
639 xml_finish();
640
641 log_info("Preparing to pack...", 0, 0);
642
643 for (int i = 0; i < group_get_total(); i++) {
644 pack_group(i);
645 }
646
647 #endif
648
649 #ifdef PACK_CURSORS
650
651 log_info("Packing cursors...", 0, 0);
652
653 pack_cursors();
654
655 #endif
656
657 log_info("Copying other assets...", 0, 0);
658
659 platform_file_manager_copy_directory(ASSETS_DIRECTORY, PACKED_ASSETS_DIR);
660
661 log_info("All done!", 0, 0);
662
663 png_unload();
664 return 0;
665 }