Compare commits
No commits in common. "9ad694d8b0b21049c5ec536a907390b164f0068e" and "99422b085ea3e390c6e6cba04fc686320faa6a7e" have entirely different histories.
9ad694d8b0
...
99422b085e
|
|
@ -1 +1 @@
|
||||||
Subproject commit 6ce19277e54e0c56be105d8edac118aa3f41ba67
|
Subproject commit 6bfa68db05dafd773860bcea000f0c3fcba0176a
|
||||||
|
|
@ -27,18 +27,11 @@
|
||||||
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
} while (0);
|
} while (0);
|
||||||
#define goto_failure \
|
|
||||||
do { \
|
|
||||||
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
|
||||||
goto failure; \
|
|
||||||
} while (0);
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define debug_printf(...)
|
#define debug_printf(...)
|
||||||
|
|
||||||
#define return_failure_int return EXIT_FAILURE;
|
#define return_failure_int return EXIT_FAILURE;
|
||||||
#define return_failure_ptr return NULL;
|
#define return_failure_ptr return NULL;
|
||||||
#define goto_failure goto failure;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define warning_printf(...) \
|
#define warning_printf(...) \
|
||||||
|
|
|
||||||
|
|
@ -15,31 +15,35 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
png_structp png_ptr = NULL;
|
|
||||||
png_infop info_ptr = NULL;
|
|
||||||
|
|
||||||
// Check png autenticity.
|
// Check png autenticity.
|
||||||
png_byte sig[8];
|
png_byte sig[8];
|
||||||
if(file_read(sig, sizeof(png_byte), sizeof(sig), input_file)) goto_failure;
|
if(file_read(sig, sizeof(png_byte), sizeof(sig), input_file)) return_failure_int;
|
||||||
if(!png_check_sig(sig, 8))
|
if(!png_check_sig(sig, 8))
|
||||||
{
|
{
|
||||||
error_printf("Png file is not valid.");
|
error_printf("Png file is not valid.");
|
||||||
goto_failure;
|
if(fclose(input_file))
|
||||||
|
error_printf("Failed to close input file : %d", errno);
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PNG structs.
|
// PNG structs.
|
||||||
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||||
if(!png_ptr)
|
if(!png_ptr)
|
||||||
{
|
{
|
||||||
error_printf("Failed to allocate png ptr.");
|
error_printf("Failed to allocate png ptr.");
|
||||||
goto_failure;
|
if(fclose(input_file))
|
||||||
|
error_printf("Failed to close input file : %d", errno);
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
info_ptr = png_create_info_struct(png_ptr);
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||||
if(!info_ptr)
|
if(!info_ptr)
|
||||||
{
|
{
|
||||||
error_printf("Failed to allocate png info.");
|
error_printf("Failed to allocate png info.");
|
||||||
goto_failure;
|
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
||||||
|
if(fclose(input_file))
|
||||||
|
error_printf("Failed to close input file : %d", errno);
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fill structs and load data.
|
// Fill structs and load data.
|
||||||
|
|
@ -47,16 +51,29 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
|
||||||
png_set_sig_bytes(png_ptr, 8);
|
png_set_sig_bytes(png_ptr, 8);
|
||||||
png_read_info(png_ptr, info_ptr);
|
png_read_info(png_ptr, info_ptr);
|
||||||
|
|
||||||
{
|
|
||||||
uint16_t width = png_get_image_width(png_ptr, info_ptr);
|
uint16_t width = png_get_image_width(png_ptr, info_ptr);
|
||||||
uint16_t height = png_get_image_height(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);
|
size_t row_length = png_get_rowbytes(png_ptr, info_ptr);
|
||||||
|
|
||||||
// Write image size.
|
// Write image size.
|
||||||
if(file_write(&width, sizeof(width), 1, output_file)) goto_failure;
|
if(file_write(&width, sizeof(width), 1, output_file))
|
||||||
|
{
|
||||||
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
|
if(fclose(input_file))
|
||||||
|
error_printf("Failed to close input file : %d", errno);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(width);
|
*asset_size += sizeof(width);
|
||||||
|
|
||||||
if(file_write(&height, sizeof(height), 1, output_file)) goto_failure;
|
if(file_write(&height, sizeof(height), 1, output_file))
|
||||||
|
{
|
||||||
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
|
if(fclose(input_file))
|
||||||
|
error_printf("Failed to close input file : %d", errno);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(height);
|
*asset_size += sizeof(height);
|
||||||
|
|
||||||
// Prepare storage.
|
// Prepare storage.
|
||||||
|
|
@ -89,12 +106,17 @@ 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;
|
if(file_write(&dst_pixels, sizeof(*dst_pixels), width * height, output_file))
|
||||||
*asset_size += sizeof(dst_pixels);
|
{
|
||||||
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
|
if(fclose(input_file))
|
||||||
|
error_printf("Failed to close input file : %d", errno);
|
||||||
|
return_failure_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
*asset_size += sizeof(dst_pixels);
|
||||||
|
|
||||||
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
if(fclose(input_file))
|
if(fclose(input_file))
|
||||||
{
|
{
|
||||||
error_printf("Failed to close input file : %d", errno);
|
error_printf("Failed to close input file : %d", errno);
|
||||||
|
|
@ -102,12 +124,4 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
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);
|
|
||||||
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,13 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
const char *input_file_path = argv[3];
|
const char *input_file_path = argv[3];
|
||||||
|
|
||||||
if(build_object(input_file_path, output_file)) goto_failure;
|
if(build_object(input_file_path, output_file))
|
||||||
|
{
|
||||||
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(build_function == BUILD_ASSET_FILE)
|
else if(build_function == BUILD_ASSET_FILE)
|
||||||
{
|
{
|
||||||
|
|
@ -56,7 +62,13 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
const int nb_input_object_files = argc - 2;
|
const int 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(&nb_input_object_files, sizeof(nb_input_object_files), 1, output_file))
|
||||||
|
{
|
||||||
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
header_size += sizeof(nb_input_object_files);
|
header_size += sizeof(nb_input_object_files);
|
||||||
|
|
||||||
FILE *input_object_files[nb_input_object_files];
|
FILE *input_object_files[nb_input_object_files];
|
||||||
|
|
@ -78,7 +90,10 @@ int main(int argc, char *argv[])
|
||||||
if(fclose(input_object_files[i]))
|
if(fclose(input_object_files[i]))
|
||||||
error_printf("Failed to close input object file : %d", errno);
|
error_printf("Failed to close input object file : %d", errno);
|
||||||
|
|
||||||
goto_failure;
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(file_read(&input_object_files_sizes[i], sizeof(*input_object_files_sizes), 1, input_object_files[i]))
|
if(file_read(&input_object_files_sizes[i], sizeof(*input_object_files_sizes), 1, input_object_files[i]))
|
||||||
|
|
@ -87,7 +102,10 @@ int main(int argc, char *argv[])
|
||||||
if(fclose(input_object_files[i]))
|
if(fclose(input_object_files[i]))
|
||||||
error_printf("Failed to close input object file : %d", errno);
|
error_printf("Failed to close input object file : %d", errno);
|
||||||
|
|
||||||
goto_failure;
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return_failure_int
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *input_object_file_slash = strrchr(input_object_file_path, '/');
|
const char *input_object_file_slash = strrchr(input_object_file_path, '/');
|
||||||
|
|
@ -118,7 +136,10 @@ int main(int argc, char *argv[])
|
||||||
if(fclose(input_object_files[i]))
|
if(fclose(input_object_files[i]))
|
||||||
error_printf("Failed to close input object file : %d", errno);
|
error_printf("Failed to close input object file : %d", errno);
|
||||||
|
|
||||||
goto_failure;
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return_failure_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(file_write(&input_object_files_offsets[i], sizeof(*input_object_files_offsets), 1, output_file))
|
if(file_write(&input_object_files_offsets[i], sizeof(*input_object_files_offsets), 1, output_file))
|
||||||
|
|
@ -127,7 +148,10 @@ int main(int argc, char *argv[])
|
||||||
if(fclose(input_object_files[i]))
|
if(fclose(input_object_files[i]))
|
||||||
error_printf("Failed to close input object file : %d", errno);
|
error_printf("Failed to close input object file : %d", errno);
|
||||||
|
|
||||||
goto_failure;
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return_failure_int;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -141,7 +165,10 @@ int main(int argc, char *argv[])
|
||||||
if(fclose(input_object_files[i]))
|
if(fclose(input_object_files[i]))
|
||||||
error_printf("Failed to close input object file : %d", errno);
|
error_printf("Failed to close input object file : %d", errno);
|
||||||
|
|
||||||
goto_failure;
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return_failure_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(file_write(buffer, sizeof(*buffer), input_object_files_sizes[i], output_file))
|
if(file_write(buffer, sizeof(*buffer), input_object_files_sizes[i], output_file))
|
||||||
|
|
@ -150,7 +177,10 @@ int main(int argc, char *argv[])
|
||||||
if(fclose(input_object_files[i]))
|
if(fclose(input_object_files[i]))
|
||||||
error_printf("Failed to close input object file : %d", errno);
|
error_printf("Failed to close input object file : %d", errno);
|
||||||
|
|
||||||
goto_failure;
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return_failure_int;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -164,7 +194,10 @@ int main(int argc, char *argv[])
|
||||||
if(fclose(input_object_files[i]))
|
if(fclose(input_object_files[i]))
|
||||||
error_printf("Failed to close input object file : %d", errno);
|
error_printf("Failed to close input object file : %d", errno);
|
||||||
|
|
||||||
goto_failure;
|
if(fclose(output_file))
|
||||||
|
error_printf("Failed to close output file : %d", errno);
|
||||||
|
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -176,10 +209,4 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
failure:
|
|
||||||
if(fclose(output_file))
|
|
||||||
error_printf("Failed to close output file : %d", errno);
|
|
||||||
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,8 +51,7 @@ typedef enum component_type_t {
|
||||||
int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_size)
|
int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_size)
|
||||||
{
|
{
|
||||||
json_object *root = json_object_from_file(input_file_name);
|
json_object *root = json_object_from_file(input_file_name);
|
||||||
|
if (!root)
|
||||||
if(!root)
|
|
||||||
{
|
{
|
||||||
error_printf("Failed to load input file.");
|
error_printf("Failed to load input file.");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
@ -60,109 +59,213 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
|
||||||
|
|
||||||
json_object *tmp_obj;
|
json_object *tmp_obj;
|
||||||
|
|
||||||
if(json_get_object_of_type(root, "MapWidth", json_type_int, &tmp_obj)) goto_failure;
|
if(json_get_object_of_type(root, "MapWidth", json_type_int, &tmp_obj))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t map_width = json_object_get_int(tmp_obj);
|
uint16_t map_width = json_object_get_int(tmp_obj);
|
||||||
|
|
||||||
if(json_get_object_of_type(root, "MapHeight", json_type_int, &tmp_obj)) goto_failure;
|
if(json_get_object_of_type(root, "MapHeight", json_type_int, &tmp_obj))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t map_height = json_object_get_int(tmp_obj);
|
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(&map_width, sizeof(map_width), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(map_width);
|
*asset_size += sizeof(map_width);
|
||||||
|
|
||||||
if(file_write(&map_height, sizeof(map_height), 1, output_file)) goto_failure;
|
if(file_write(&map_height, sizeof(map_height), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(map_height);
|
*asset_size += sizeof(map_height);
|
||||||
|
|
||||||
// BackgroundLayers
|
// BackgroundLayers
|
||||||
json_object *backgroundlayers;
|
json_object *backgroundlayers;
|
||||||
if(json_get_object_of_type(root, "BackgroundLayers", json_type_array, &backgroundlayers)) goto_failure;
|
if(json_get_object_of_type(root, "BackgroundLayers", json_type_array, &backgroundlayers))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t layer_count = json_object_array_length(backgroundlayers);
|
uint8_t layer_count = json_object_array_length(backgroundlayers);
|
||||||
|
|
||||||
if(file_write(&layer_count, sizeof(layer_count), 1, output_file)) goto_failure;
|
if(file_write(&layer_count, sizeof(layer_count), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(layer_count);
|
*asset_size += sizeof(layer_count);
|
||||||
|
|
||||||
{
|
|
||||||
uint16_t layer_data[(size_t)map_width * (size_t)map_height];
|
uint16_t layer_data[(size_t)map_width * (size_t)map_height];
|
||||||
|
|
||||||
for(uint8_t i = 0; i < layer_count; i++)
|
for(uint8_t i = 0; i < layer_count; i++)
|
||||||
{
|
{
|
||||||
json_object *backgroundlayer;
|
json_object *backgroundlayer;
|
||||||
if(json_get_idx_of_type(backgroundlayers, i, json_type_array, &backgroundlayer)) goto_failure;
|
if(json_get_idx_of_type(backgroundlayers, i, json_type_array, &backgroundlayer))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
for(size_t j = 0; j < (size_t)map_width * (size_t)map_height; j++)
|
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;
|
if(json_get_idx_of_type(backgroundlayer, j, json_type_int, &tmp_obj))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
layer_data[j] = json_object_get_int(tmp_obj);
|
layer_data[j] = 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), (size_t)map_width * (size_t)map_height, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(layer_data);
|
*asset_size += sizeof(layer_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Foreground
|
// Foreground
|
||||||
json_object *foregroundlayer;
|
json_object *foregroundlayer;
|
||||||
if(json_get_object_of_type(root, "ForegroundLayer", json_type_array, &foregroundlayer)) goto_failure;
|
if(json_get_object_of_type(root, "ForegroundLayer", json_type_array, &foregroundlayer))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++)
|
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;
|
if(json_get_idx_of_type(foregroundlayer, i, json_type_int, &tmp_obj))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
layer_data[i] = json_object_get_int(tmp_obj);
|
layer_data[i] = 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), (size_t)map_width * (size_t)map_height, output_file))
|
||||||
*asset_size += sizeof(layer_data);
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*asset_size += sizeof(layer_data);
|
||||||
|
|
||||||
// Hitboxes
|
// Hitboxes
|
||||||
json_object *hitboxes;
|
json_object *hitboxes;
|
||||||
if(json_get_object_of_type(root, "Hitboxes", json_type_array, &hitboxes)) goto_failure;
|
if(json_get_object_of_type(root, "Hitboxes", json_type_array, &hitboxes))
|
||||||
|
|
||||||
{
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t hitboxes_layer_data[(size_t)map_width * (size_t)map_height];
|
uint8_t hitboxes_layer_data[(size_t)map_width * (size_t)map_height];
|
||||||
|
|
||||||
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++)
|
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++)
|
||||||
{
|
{
|
||||||
if(json_get_idx_of_type(hitboxes, i, json_type_int, &tmp_obj)) goto_failure;
|
if(json_get_idx_of_type(hitboxes, i, json_type_int, &tmp_obj))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
hitboxes_layer_data[i] = json_object_get_int(tmp_obj);
|
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), (size_t)map_width * (size_t)map_height, output_file))
|
||||||
*asset_size += sizeof(hitboxes_layer_data);
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*asset_size += sizeof(hitboxes_layer_data);
|
||||||
|
|
||||||
// Entities
|
// Entities
|
||||||
json_object *entities;
|
json_object *entities;
|
||||||
|
|
||||||
if(json_get_object_of_type(root, "Entities", json_type_array, &entities)) goto_failure;
|
if(json_get_object_of_type(root, "Entities", json_type_array, &entities))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t entities_nb = json_object_array_length(entities);
|
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(&entities_nb, sizeof(entities_nb), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(entities_nb);
|
*asset_size += sizeof(entities_nb);
|
||||||
|
|
||||||
for(uint32_t i = 0; i < entities_nb; i++)
|
for(uint32_t i = 0; i < entities_nb; i++)
|
||||||
{
|
{
|
||||||
json_object *entity = json_object_array_get_idx(entities, i);
|
json_object *entity = json_object_array_get_idx(entities, i);
|
||||||
|
|
||||||
if(json_get_object_of_type(entity, "ID", json_type_int, &tmp_obj)) goto_failure;
|
if(json_get_object_of_type(entity, "ID", json_type_int, &tmp_obj))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t entity_id = json_object_get_int(tmp_obj);
|
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(&entity_id, sizeof(entity_id), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(entity_id);
|
*asset_size += sizeof(entity_id);
|
||||||
|
|
||||||
json_object *components;
|
json_object *components;
|
||||||
|
|
||||||
if(json_get_object_of_type(entity, "Components", json_type_array, &components)) goto_failure;
|
if(json_get_object_of_type(entity, "Components", json_type_array, &components))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t components_nb = json_object_array_length(components);
|
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(&components_nb, sizeof(components_nb), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(components_nb);
|
*asset_size += sizeof(components_nb);
|
||||||
|
|
||||||
for(uint32_t j = 0; j < components_nb; j++)
|
for(uint32_t j = 0; j < components_nb; j++)
|
||||||
{
|
{
|
||||||
json_object *component;
|
json_object *component;
|
||||||
if(json_get_idx_of_type(components, j, json_type_object, &component)) goto_failure;
|
if(json_get_idx_of_type(components, j, json_type_object, &component))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj)) goto_failure;
|
json_object *attributes;
|
||||||
|
|
||||||
|
if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t component_type;
|
uint16_t component_type;
|
||||||
|
|
||||||
|
|
@ -171,12 +274,19 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
|
||||||
component_type = NAME;
|
component_type = NAME;
|
||||||
#include "components.def"
|
#include "components.def"
|
||||||
|
|
||||||
if(file_write(&component_type, sizeof(component_type), 1, output_file)) goto_failure;
|
if(file_write(&component_type, sizeof(component_type), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
*asset_size += sizeof(component_type);
|
*asset_size += sizeof(component_type);
|
||||||
|
|
||||||
json_object *attributes;
|
if(json_get_object_of_type(component, "Attributes", json_type_array, &attributes))
|
||||||
if(json_get_object_of_type(component, "Attributes", json_type_array, &attributes)) goto_failure;
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t attributes_nb = json_object_array_length(attributes);
|
uint32_t attributes_nb = json_object_array_length(attributes);
|
||||||
|
|
||||||
|
|
@ -189,7 +299,11 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
|
||||||
case json_type_int:
|
case json_type_int:
|
||||||
{
|
{
|
||||||
uint32_t value = json_object_get_int(attribute);
|
uint32_t value = json_object_get_int(attribute);
|
||||||
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure;
|
if(file_write(&value, sizeof(value), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
*asset_size += sizeof(value);
|
*asset_size += sizeof(value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -197,7 +311,11 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
|
||||||
case json_type_double:
|
case json_type_double:
|
||||||
{
|
{
|
||||||
float value = json_object_get_double(attribute);
|
float value = json_object_get_double(attribute);
|
||||||
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure;
|
if(file_write(&value, sizeof(value), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
*asset_size += sizeof(value);
|
*asset_size += sizeof(value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -205,7 +323,11 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
|
||||||
case json_type_boolean:
|
case json_type_boolean:
|
||||||
{
|
{
|
||||||
bool value = json_object_get_boolean(attribute);
|
bool value = json_object_get_boolean(attribute);
|
||||||
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure;
|
if(file_write(&value, sizeof(value), 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
*asset_size += sizeof(value);
|
*asset_size += sizeof(value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -213,14 +335,18 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
|
||||||
case json_type_string:
|
case json_type_string:
|
||||||
{
|
{
|
||||||
const char *str = json_object_get_string(attribute);
|
const char *str = json_object_get_string(attribute);
|
||||||
if(file_write(str, sizeof(*str), strlen(str) + 1, output_file)) goto_failure;
|
if(file_write(str, sizeof(*str), strlen(str) + 1, output_file))
|
||||||
|
{
|
||||||
|
json_object_put(root);
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
*asset_size += (strlen(str) + 1) * sizeof(*str);
|
*asset_size += (strlen(str) + 1) * sizeof(*str);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
error_printf("Component attribute of type %s is not handled.", json_type_to_name(json_object_get_type(attribute)));
|
error_printf("Component attribute of type %s is not handled.", json_type_to_name(json_object_get_type(attribute)));
|
||||||
goto_failure;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -229,9 +355,4 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
|
||||||
json_object_put(root);
|
json_object_put(root);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
failure:
|
|
||||||
json_object_put(root);
|
|
||||||
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -98,29 +98,21 @@ inline int animation_system_apply_changes(component_t *system)
|
||||||
|
|
||||||
if(linked_list_clear(&system_data->frames)) return_failure_int;
|
if(linked_list_clear(&system_data->frames)) return_failure_int;
|
||||||
|
|
||||||
for(size_t i = 0; i < system_data->nb_frames; i++)
|
for(size_t current_frame_nb = 0; current_frame_nb < system_data->nb_frames; current_frame_nb++)
|
||||||
{
|
{
|
||||||
elem_t *frame_elem = create_elem(&system_data->frames);
|
elem_t *frame = create_elem(&system_data->frames);
|
||||||
if(!frame_elem) return_failure_int;
|
if(!frame) return_failure_int;
|
||||||
|
|
||||||
rect_t *frame = (rect_t *)frame_elem->data;
|
((rect_t *)frame->data)->x = sprite_component_data->src_rect.w * current_frame_nb;
|
||||||
|
((rect_t *)frame->data)->y = 0;
|
||||||
|
((rect_t *)frame->data)->w = sprite_component_data->src_rect.w;
|
||||||
|
((rect_t *)frame->data)->h = sprite_component_data->src_rect.h;
|
||||||
|
|
||||||
frame->x = sprite_component_data->src_rect.w * i;
|
linked_list_push_back(&system_data->frames, frame);
|
||||||
frame->y = 0;
|
|
||||||
frame->w = sprite_component_data->src_rect.w;
|
|
||||||
frame->h = sprite_component_data->src_rect.h;
|
|
||||||
|
|
||||||
linked_list_push_back(&system_data->frames, frame_elem);
|
|
||||||
|
|
||||||
if(system_data->current_frame_nb == i)
|
|
||||||
system_data->current_frame = frame_elem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!system_data->current_frame)
|
system_data->current_frame = linked_list_get(&system_data->frames, system_data->current_frame_nb);
|
||||||
{
|
if(!system_data->current_frame) return_failure_int;
|
||||||
error_printf("Current frame number out of range.");
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
sprite_component_data->src_rect = *(rect_t *)system_data->current_frame->data;
|
sprite_component_data->src_rect = *(rect_t *)system_data->current_frame->data;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,13 +52,14 @@ int physics_system_update(component_t *component)
|
||||||
transform_component_data_t *transform_component_data = system_data->transform_component->data;
|
transform_component_data_t *transform_component_data = system_data->transform_component->data;
|
||||||
hitbox_component_data_t *hitbox_component_data = system_data->hitbox_component->data;
|
hitbox_component_data_t *hitbox_component_data = system_data->hitbox_component->data;
|
||||||
|
|
||||||
system_data->velocity.x = lerp_time(system_data->velocity.x, system_data->target_velocity.x * system_data->speed, system_data->smoothing_factor, game.delta_time_ms);
|
system_data->velocity.x = lerp(system_data->velocity.x, system_data->target_velocity.x, system_data->smoothing_factor);
|
||||||
system_data->velocity.y = lerp_time(system_data->velocity.y, system_data->target_velocity.y * system_data->speed, system_data->smoothing_factor, game.delta_time_ms);
|
system_data->velocity.y = lerp(system_data->velocity.y, system_data->target_velocity.y, system_data->smoothing_factor);
|
||||||
|
|
||||||
if(hitbox_component_data->activated)
|
if(hitbox_component_data->activated)
|
||||||
if(linked_list_for_each(&game.entity_manager.entities, action_detect_solve_entities_collision, component)) return EXIT_FAILURE;
|
if(linked_list_for_each(&game.entity_manager.entities, action_detect_solve_entities_collision, component)) return EXIT_FAILURE;
|
||||||
|
|
||||||
transform_component_data->bounds.x += system_data->velocity.x * (float)game.delta_time_ms / 1000.0f;
|
transform_component_data->bounds.x += system_data->velocity.x * system_data->speed * (float)game.delta_time_ms / 1000.0f;
|
||||||
transform_component_data->bounds.y += system_data->velocity.y * (float)game.delta_time_ms / 1000.0f;
|
transform_component_data->bounds.y += system_data->velocity.y * system_data->speed * (float)game.delta_time_ms / 1000.0f;
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -107,14 +107,7 @@ int game_init(void)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
if(!entity_new_component(player_entity, PHYSICS_SYSTEM)) return_failure_int;
|
||||||
component_t *physics_component = entity_new_component(player_entity, PHYSICS_SYSTEM);
|
|
||||||
if(!physics_component) return_failure_int;
|
|
||||||
|
|
||||||
physics_system_data_t *physics_system_data = physics_component->data;
|
|
||||||
|
|
||||||
physics_system_data->smoothing_factor = 0.0005;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!entity_new_component(player_entity, PLAYER_SYSTEM)) return_failure_int;
|
if(!entity_new_component(player_entity, PLAYER_SYSTEM)) return_failure_int;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue