From 3dfe19d64c56001903209137880bb64ae3385ab5 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Tue, 29 Sep 2026 12:38:37 +0200 Subject: [PATCH] Fixed compatibility with Windows. --- asset_builder/image.c | 83 ++++++++++++++++++++++++++------------- asset_builder/map.c | 4 +- core/host/linux/texture.c | 8 ++-- 3 files changed, 61 insertions(+), 34 deletions(-) diff --git a/asset_builder/image.c b/asset_builder/image.c index 483ead6..962132c 100644 --- a/asset_builder/image.c +++ b/asset_builder/image.c @@ -15,42 +15,42 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_ return EXIT_FAILURE; } - png_structp png_ptr = NULL; - png_infop info_ptr = NULL; + png_struct *img = NULL; + png_info *img_info = NULL; // Check png autenticity. - png_byte sig[8]; - if(file_read(sig, sizeof(png_byte), sizeof(sig), input_file)) goto_failure; + uint8_t sig[8]; + if(file_read(sig, sizeof(*sig), sizeof(sig), input_file)) goto_failure; if(!png_check_sig(sig, 8)) { error_printf("Png file is not valid."); - goto_failure; + goto failure; } // PNG structs. - png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if(!png_ptr) + img = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if(!img) { error_printf("Failed to allocate png ptr."); - goto_failure; + goto failure; } - info_ptr = png_create_info_struct(png_ptr); - if(!info_ptr) + img_info = png_create_info_struct(img); + if(!img_info) { error_printf("Failed to allocate png info."); - goto_failure; + goto failure; } // Fill structs and load data. - png_init_io(png_ptr, input_file); - png_set_sig_bytes(png_ptr, 8); - png_read_info(png_ptr, info_ptr); + png_init_io(img, input_file); + png_set_sig_bytes(img, 8); + png_read_info(img, img_info); { - uint16_t width = png_get_image_width(png_ptr, info_ptr); - uint16_t height = png_get_image_height(png_ptr, info_ptr); - size_t row_length = png_get_rowbytes(png_ptr, info_ptr); + uint16_t width = png_get_image_width(img, img_info); + uint16_t height = png_get_image_height(img, img_info); + size_t row_length = png_get_rowbytes(img, img_info); // Write image size. if(file_write(&width, sizeof(width), 1, output_file)) goto_failure; @@ -60,15 +60,37 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_ *asset_size += sizeof(height); // Prepare storage. - png_byte src_pixels[height * row_length]; - png_bytep src_row_ptrs[height]; - for(png_uint_32 i = 0; i < height; i++) + uint8_t *src_pixels = calloc(height * row_length, sizeof(*src_pixels)); + if(!src_pixels) + { + error_printf("Failed to alloc memory : %d", errno); + goto failure; + } + + uint8_t **src_row_ptrs = calloc(height, sizeof(*src_row_ptrs)); + if(!src_row_ptrs) + { + error_printf("Failed to alloc memory : %d", errno); + free(src_pixels); + goto failure; + } + + for(uint16_t i = 0; i < height; i++) src_row_ptrs[i] = &src_pixels[i * row_length]; // Read pixels. - png_read_image(png_ptr, src_row_ptrs); + png_read_image(img, src_row_ptrs); - uint16_t dst_pixels[width * height]; + free(src_row_ptrs); + png_destroy_read_struct(&img, &img_info, NULL); + + uint16_t *dst_pixels = calloc(width * height, sizeof(*dst_pixels)); + if(!dst_pixels) + { + error_printf("Failed to alloc memory : %d", errno); + free(src_pixels); + goto failure; + } for(uint16_t i = 0; i < height; i++) { @@ -89,11 +111,18 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_ } } - if(file_write(&dst_pixels, sizeof(*dst_pixels), width * height, output_file)) goto_failure; - *asset_size += sizeof(dst_pixels); - } + free(src_pixels); - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + if(file_write(dst_pixels, sizeof(*dst_pixels), width * height, output_file)) + { + free(dst_pixels); + goto_failure; + } + + *asset_size += width * height * sizeof(*dst_pixels); + + free(dst_pixels); + } if(fclose(input_file)) { @@ -104,8 +133,6 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_ return EXIT_SUCCESS; failure: - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - if(fclose(input_file)) error_printf("Failed to close input file : %d", errno); diff --git a/asset_builder/map.c b/asset_builder/map.c index c7f8c4f..eba3d24 100644 --- a/asset_builder/map.c +++ b/asset_builder/map.c @@ -7,7 +7,7 @@ #include "utils.h" #include "errors.h" -static int json_get_object_of_type(const json_object *obj, const char *key, json_type type, json_object **result) +static int json_get_object_of_type(json_object *obj, const char *key, json_type type, json_object **result) { if(!json_object_object_get_ex(obj, key, result)) { @@ -24,7 +24,7 @@ static int json_get_object_of_type(const json_object *obj, const char *key, json return EXIT_SUCCESS; } -static inline int json_get_idx_of_type(const json_object *obj, size_t idx, json_type type, json_object **result) +static inline int json_get_idx_of_type(json_object *obj, size_t idx, json_type type, json_object **result) { *result = json_object_array_get_idx(obj, idx); diff --git a/core/host/linux/texture.c b/core/host/linux/texture.c index 0616975..3b293ed 100644 --- a/core/host/linux/texture.c +++ b/core/host/linux/texture.c @@ -31,26 +31,26 @@ texture_t *load_texture(void) if(!SDL_LockSurface(tmp_surface)) { error_printf("Failed to lock temporary surface : %s", SDL_GetError()); - goto_failure; + goto failure; } if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height) { error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file)); - goto_failure; + goto failure; } if(!SDL_SetSurfaceColorKey(tmp_surface, true, TEXTURE_TRANSPARENT_COLOR)) { error_printf("Failed to set color key of temporary surface : %s", SDL_GetError()); - goto_failure; + goto failure; } texture_t *texture = SDL_CreateTextureFromSurface(game.display.renderer, tmp_surface); if(!texture) { error_printf("Failed to create texture from temporary surface : %s", SDL_GetError()); - goto_failure; + goto failure; } SDL_DestroySurface(tmp_surface);