diff --git a/asset_builder/image.c b/asset_builder/image.c index 962132c..c94a180 100644 --- a/asset_builder/image.c +++ b/asset_builder/image.c @@ -20,7 +20,12 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_ // Check png autenticity. uint8_t sig[8]; - if(file_read(sig, sizeof(*sig), sizeof(sig), input_file)) goto_failure; + if(fread(sig, sizeof(sig[0]), ARRAY_SIZE(sig), input_file) != ARRAY_SIZE(sig)) + { + error_printf("Failed to read output file : %d", errno); + goto failure; + } + if(!png_check_sig(sig, 8)) { error_printf("Png file is not valid."); @@ -53,10 +58,10 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_ size_t row_length = png_get_rowbytes(img, img_info); // Write image size. - if(file_write(&width, sizeof(width), 1, output_file)) goto_failure; + if(file_write(&target_host_16b(width), sizeof(width), 1, output_file)) goto_failure; *asset_size += sizeof(width); - if(file_write(&height, sizeof(height), 1, output_file)) goto_failure; + if(file_write(&target_host_16b(height), sizeof(height), 1, output_file)) goto_failure; *asset_size += sizeof(height); // Prepare storage. @@ -108,12 +113,14 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_ { dst_pixels[i * width + j] = 0x0001; } + + dst_pixels[i * width + j] = target_host_16b(dst_pixels[i * width + j]); } } free(src_pixels); - if(file_write(dst_pixels, sizeof(*dst_pixels), width * height, output_file)) + if(file_write(dst_pixels, sizeof(dst_pixels[0]), width * height, output_file)) { free(dst_pixels); goto_failure; diff --git a/asset_builder/main.c b/asset_builder/main.c index a29d9ed..ff9a3e2 100644 --- a/asset_builder/main.c +++ b/asset_builder/main.c @@ -54,9 +54,9 @@ int main(int argc, char *argv[]) { uint32_t header_size = 0; - const int nb_input_object_files = argc - 2; + const uint32_t nb_input_object_files = argc - 2; - if(file_write(&nb_input_object_files, sizeof(nb_input_object_files), 1, output_file)) goto_failure; + if(file_write(&target_host_32b(nb_input_object_files), sizeof(nb_input_object_files), 1, output_file)) goto_failure; header_size += sizeof(nb_input_object_files); FILE *input_object_files[nb_input_object_files]; @@ -81,8 +81,10 @@ int main(int argc, char *argv[]) goto_failure; } - if(file_read(&input_object_files_sizes[i], sizeof(*input_object_files_sizes), 1, input_object_files[i])) + if(fread(&input_object_files_sizes[i], sizeof(input_object_files_sizes[0]), 1, input_object_files[i]) != 1) { + error_printf("Failed to read output file : %d", errno); + for(; i >= 0; i--) if(fclose(input_object_files[i])) error_printf("Failed to close input object file : %d", errno); @@ -112,7 +114,7 @@ int main(int argc, char *argv[]) input_object_file_ext[0] = '\00'; uint32_t input_object_file_base_name_length = strlen(input_object_file_name) - strlen(input_object_file_ext) + 1; - if(file_write(input_object_file_name, sizeof(*input_object_file_name), input_object_file_base_name_length, output_file)) + if(file_write(input_object_file_name, sizeof(input_object_file_name[0]), input_object_file_base_name_length, output_file)) { for(i = 0; i < nb_input_object_files; i++) if(fclose(input_object_files[i])) @@ -121,7 +123,7 @@ int main(int argc, char *argv[]) goto_failure; } - if(file_write(&input_object_files_offsets[i], sizeof(*input_object_files_offsets), 1, output_file)) + if(file_write(&target_host_32b(input_object_files_offsets[i]), sizeof(input_object_files_offsets[0]), 1, output_file)) { for(i = 0; i < nb_input_object_files; i++) if(fclose(input_object_files[i])) @@ -131,12 +133,14 @@ int main(int argc, char *argv[]) } } - for(int i = 0; i < nb_input_object_files; i++) + for(uint32_t i = 0; i < nb_input_object_files; i++) { char buffer[input_object_files_sizes[i]]; - if(file_read(buffer, sizeof(*buffer), input_object_files_sizes[i], input_object_files[i])) + if(fread(buffer, sizeof(buffer[0]), input_object_files_sizes[i], input_object_files[i]) != input_object_files_sizes[i]) { + error_printf("Failed to read input file : %d", errno); + for(i = 0; i < nb_input_object_files; i++) if(fclose(input_object_files[i])) error_printf("Failed to close input object file : %d", errno); @@ -144,8 +148,10 @@ int main(int argc, char *argv[]) goto_failure; } - if(file_write(buffer, sizeof(*buffer), input_object_files_sizes[i], output_file)) + if(fwrite(buffer, sizeof(buffer[0]), input_object_files_sizes[i], output_file) != input_object_files_sizes[i]) { + error_printf("Failed to write output file : %d", errno); + for(i = 0; i < nb_input_object_files; i++) if(fclose(input_object_files[i])) error_printf("Failed to close input object file : %d", errno); diff --git a/asset_builder/map.c b/asset_builder/map.c index eba3d24..630f2fb 100644 --- a/asset_builder/map.c +++ b/asset_builder/map.c @@ -66,10 +66,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si if(json_get_object_of_type(root, "MapHeight", json_type_int, &tmp_obj)) goto_failure; uint16_t map_height = json_object_get_int(tmp_obj); - if(file_write(&map_width, sizeof(map_width), 1, output_file)) goto_failure; + if(file_write(&target_host_16b(map_width), sizeof(map_width), 1, output_file)) goto_failure; *asset_size += sizeof(map_width); - if(file_write(&map_height, sizeof(map_height), 1, output_file)) goto_failure; + if(file_write(&target_host_16b(map_height), sizeof(map_height), 1, output_file)) goto_failure; *asset_size += sizeof(map_height); // BackgroundLayers @@ -91,10 +91,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si for(size_t j = 0; j < (size_t)map_width * (size_t)map_height; j++) { if(json_get_idx_of_type(backgroundlayer, j, json_type_int, &tmp_obj)) goto_failure; - layer_data[j] = json_object_get_int(tmp_obj); + layer_data[j] = target_host_16b((uint16_t){ json_object_get_int(tmp_obj) }); } - if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; + if(file_write(&layer_data, sizeof(layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; *asset_size += sizeof(layer_data); } @@ -105,10 +105,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++) { if(json_get_idx_of_type(foregroundlayer, i, json_type_int, &tmp_obj)) goto_failure; - layer_data[i] = json_object_get_int(tmp_obj); + layer_data[i] = target_host_16b((uint16_t){ json_object_get_int(tmp_obj) }); } - if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; + if(file_write(&layer_data, sizeof(layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; *asset_size += sizeof(layer_data); } // Hitboxes @@ -124,7 +124,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si hitboxes_layer_data[i] = json_object_get_int(tmp_obj); } - if(file_write(&hitboxes_layer_data, sizeof(*hitboxes_layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; + if(file_write(&hitboxes_layer_data, sizeof(hitboxes_layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; *asset_size += sizeof(hitboxes_layer_data); } @@ -134,7 +134,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si if(json_get_object_of_type(root, "Entities", json_type_array, &entities)) goto_failure; uint16_t entities_nb = json_object_array_length(entities); - if(file_write(&entities_nb, sizeof(entities_nb), 1, output_file)) goto_failure; + if(file_write(&target_host_16b(entities_nb), sizeof(entities_nb), 1, output_file)) goto_failure; *asset_size += sizeof(entities_nb); for(uint32_t i = 0; i < entities_nb; i++) @@ -145,7 +145,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si uint32_t entity_id = json_object_get_int(tmp_obj); - if(file_write(&entity_id, sizeof(entity_id), 1, output_file)) goto_failure; + if(file_write(&target_host_32b(entity_id), sizeof(entity_id), 1, output_file)) goto_failure; *asset_size += sizeof(entity_id); json_object *components; @@ -154,7 +154,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si uint16_t components_nb = json_object_array_length(components); - if(file_write(&components_nb, sizeof(components_nb), 1, output_file)) goto_failure; + if(file_write(&target_host_16b(components_nb), sizeof(components_nb), 1, output_file)) goto_failure; *asset_size += sizeof(components_nb); for(uint32_t j = 0; j < components_nb; j++) @@ -164,14 +164,14 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj)) goto_failure; - uint16_t component_type; + uint16_t component_type = -1; #define COMPONENT(NAME, ...) \ if(!strcmp(#NAME, json_object_get_string(tmp_obj))) \ component_type = NAME; #include "components.def" - if(file_write(&component_type, sizeof(component_type), 1, output_file)) goto_failure; + if(file_write(&target_host_16b(component_type), sizeof(component_type), 1, output_file)) goto_failure; *asset_size += sizeof(component_type); @@ -188,8 +188,8 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si { case json_type_int: { - uint32_t value = json_object_get_int(attribute); - if(file_write(&value, sizeof(value), 1, output_file)) goto_failure; + int32_t value = json_object_get_int(attribute); + if(file_write(&target_host_32b(value), sizeof(value), 1, output_file)) goto_failure; *asset_size += sizeof(value); break; } @@ -197,7 +197,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si case json_type_double: { float value = json_object_get_double(attribute); - if(file_write(&value, sizeof(value), 1, output_file)) goto_failure; + if(file_write(&target_host_32b(value), sizeof(value), 1, output_file)) goto_failure; *asset_size += sizeof(value); break; } @@ -213,8 +213,8 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si case json_type_string: { const char *str = json_object_get_string(attribute); - if(file_write(str, sizeof(*str), strlen(str) + 1, output_file)) goto_failure; - *asset_size += (strlen(str) + 1) * sizeof(*str); + if(file_write(str, sizeof(str[0]), strlen(str) + 1, output_file)) goto_failure; + *asset_size += (strlen(str) + 1) * sizeof(str[0]); break; } diff --git a/asset_builder/utils.c b/asset_builder/utils.c index 32f9546..b225007 100644 --- a/asset_builder/utils.c +++ b/asset_builder/utils.c @@ -4,18 +4,7 @@ #include #include "errors.h" -inline int file_read(void *ptr, size_t size, size_t nb, FILE *file) -{ - if(fread(ptr, size, nb, file) != nb) - { - error_printf("Failed to read output file : %d", errno); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; -} - -inline int file_write(const void *ptr, size_t size, size_t nb, FILE *file) +int file_write(const void *ptr, size_t size, size_t nb, FILE *file) { if(fwrite(ptr, size, nb, file) != nb) { diff --git a/asset_builder/utils.h b/asset_builder/utils.h index 053948b..2069ea3 100644 --- a/asset_builder/utils.h +++ b/asset_builder/utils.h @@ -3,8 +3,19 @@ #include -int file_read(void *ptr, size_t size, size_t nb, FILE *file); +#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0])) int file_write(const void *ptr, size_t size, size_t nb, FILE *file); +#define file_write_stack_array(A, F) file_write(A, sizeof(A[0]), sizeof(A) / sizeof(A[0]), F) + +#ifdef BIGENDIAN +#include +#define target_host_16b(V) (uint16_t){ htobe16(*(uint16_t *)&V) } +#define target_host_32b(V) (uint32_t){ htobe32(*(uint32_t *)&V) } +#else +#define target_host_16b(V) (*(uint16_t *)&V) +#define target_host_32b(V) (*(uint32_t *)&V) +#endif + #endif // _UTILS_H \ No newline at end of file diff --git a/configure b/configure index 7cabc90..483277d 100755 --- a/configure +++ b/configure @@ -234,13 +234,30 @@ case "${TARGET_HOST}" in TARGET_HOST_CFLAGS=( "--std=c17" "-Os" - "-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" "-DFXCG50" "-DTARGET_FXCG50" - "-mb" "-m4-nofpu" - "-ffreestanding" "-nostdlib" "-fstrict-volatile-bitfields" + "-DFXCG50" "-DTARGET_FXCG50" + "-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" + "-mb" + "-m4-nofpu" + "-ffreestanding" "-nostdlib" + "-fstrict-volatile-bitfields" ) - TARGET_HOST_LDFLAGS="-T fxcg50.ld" + TARGET_HOST_LDFLAGS=( + "-Wl,--no-warn-rwx-segments" + "-T $(fxsdk path sysroot)/sh3eb-elf/lib/fxcg50.ld" "-lc" + "-lgint-cg" "-lgcc" "-lc" + "-lopenlibm" + "-flto" + ) + OUTPUT_EXT="" STATIC_LIB_OUTPUT_EXT=".a" + + POST_BUILD_COMMAND="sh-elf-objcopy -O binary -R .bss -R .gint_bss \$(OUTPUT) \$(OUTPUT).bin && fxgxa --g3a -n 2D_Engine --icon-uns=\$(ROOT)/assets/icons/fxcg50-icon-uns.png --icon-sel=\$(ROOT)/assets/icons/fxcg50-icon-sel.png \$(OUTPUT).bin -o \$(OUTPUT).g3a" + RUN_COMMAND="fxlink -isw \$(OUTPUT).g3a \$(ASSET_OUTPUT)" + + ASSET_BUILDER_CFLAGS+=( + "-DBIGENDIAN" "-D_DEFAULT_SOURCE" + ) ;; *)