diff --git a/src/Makefile b/src/Makefile index 4641ec8..0e063a9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,6 +5,7 @@ CORE_SOURCES := \ inputs.c \ asset_manager.c \ texture.c \ + map.c \ event_bus.c \ ecs.c \ display.c \ @@ -15,7 +16,7 @@ CORE_CC = gcc ifeq ($(RELEASE), 1) CORE_CFLAGS = --std=c17 --pedantic -O3 -DASSET_FILE_NAME=\"$(notdir $(ASSET_OUTPUT))\" -D_GNU_SOURCE=1 $(shell pkg-config --cflags sdl3) # RELEASE else -CORE_CFLAGS = --std=c17 --pedantic -O1 -g -DDEBUG=2 -DASSET_FILE_NAME=\"$(notdir $(ASSET_OUTPUT))\" -D_GNU_SOURCE=1 $(shell pkg-config --cflags sdl3) \ +CORE_CFLAGS = --std=c17 --pedantic -O0 -g -DDEBUG=2 -DASSET_FILE_NAME=\"$(notdir $(ASSET_OUTPUT))\" -D_GNU_SOURCE=1 $(shell pkg-config --cflags sdl3) \ -Wall -Wno-missing-braces -Wextra -Wno-missing-field-initializers \ -Wformat=2 -Wswitch-default -Wswitch -Wcast-align \ -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \ diff --git a/src/asset_manager.c b/src/asset_manager.c index 54cf812..034cfdc 100644 --- a/src/asset_manager.c +++ b/src/asset_manager.c @@ -5,12 +5,24 @@ #include "game.h" #include "linked_list.h" #include "texture.h" +#include "map.h" #include "memory_alloc.h" #include "errors.h" +// Args : asset_type_t asset_type, const char *asset_name int create_asset(asset_t *asset, va_list args) { asset->type = va_arg(args, asset_type_t); + + const char *asset_name = va_arg(args, const char *); + asset->name = calloc(strlen(asset_name) + 1, sizeof(char)); + if(!asset->name) + { + error_printf("Failed to allocate memory for asset name : %d", errno); + return EXIT_FAILURE; + } + strcpy(asset->name, asset_name); + asset->nb_refs = 0; return EXIT_SUCCESS; @@ -26,9 +38,13 @@ int destroy_asset(asset_t *asset) destroy_texture(asset->data); break; + case ASSET_MAP: + destroy_map(asset->data); + break; + default: error_printf("Asset type not valid : %d", asset->type); - break; + return EXIT_FAILURE; } return EXIT_SUCCESS; @@ -153,20 +169,10 @@ asset_t *asset_manager_load_asset(char *asset_name, asset_type_t asset_type) } asset_header_def_t *asset_header_def = asset_header_def_elem->data; - asset_elem = create_elem(&game.asset_manager.assets, asset_type); + asset_elem = create_elem(&game.asset_manager.assets, asset_type, asset_name); if(!asset_elem) return_failure_ptr; asset_t *asset = asset_elem->data; - asset->name = calloc(strlen(asset_name) + 1, sizeof(char)); - if(!asset->name) - { - error_printf("Failed to allocate memory for asset name : %d", errno); - destroy_asset(asset); - return NULL; - } - strcpy(asset->name, asset_name); - asset->type = asset_type; - if(fseek(game.asset_manager.asset_file, asset_header_def->start, SEEK_SET)) { error_printf("Failed to seek in asset file : %d", errno); @@ -176,7 +182,12 @@ asset_t *asset_manager_load_asset(char *asset_name, asset_type_t asset_type) switch(asset_type) { case ASSET_TEXTURE: - asset->data = texture_load(game.asset_manager.asset_file); + asset->data = load_texture(); + if(!asset->data) return_failure_ptr; + break; + + case ASSET_MAP: + asset->data = load_map(); if(!asset->data) return_failure_ptr; break; diff --git a/src/ecs.c b/src/ecs.c index f59f3fb..48bbbe9 100644 --- a/src/ecs.c +++ b/src/ecs.c @@ -6,6 +6,20 @@ #include "memory_alloc.h" #include "errors.h" +const char *component_type_to_name(component_type_t component_type) +{ + switch(component_type) + { +#define COMPONENT(NAME, ...) \ + case NAME: \ + return #NAME; +#include "components.def" + + default: + return "INVALID"; + } +} + // Arg : component_type_t component_type int create_component(component_t *component, va_list args) { diff --git a/src/headers/asset_manager.h b/src/headers/asset_manager.h index 02f4af2..e12688a 100644 --- a/src/headers/asset_manager.h +++ b/src/headers/asset_manager.h @@ -6,7 +6,8 @@ #include "linked_list.h" typedef enum asset_type_t { - ASSET_TEXTURE + ASSET_TEXTURE, + ASSET_MAP } asset_type_t; typedef struct asset_t { diff --git a/src/headers/ecs.h b/src/headers/ecs.h index c9f8ba2..9353b25 100644 --- a/src/headers/ecs.h +++ b/src/headers/ecs.h @@ -10,6 +10,8 @@ typedef enum component_type_t { #include "components.def" } component_type_t; +const char *component_type_to_name(component_type_t component_type); + // Component forwarding typedef struct component_t component_t; diff --git a/src/headers/map.h b/src/headers/map.h new file mode 100644 index 0000000..f8f22be --- /dev/null +++ b/src/headers/map.h @@ -0,0 +1,34 @@ +#ifndef MAP_H +#define MAP_H + +#include + +typedef struct map_entity_component_def_t { + uint32_t component_type; + void *attributes; +} map_entity_component_def_t; + +typedef struct map_entity_def_t { + uint32_t id; + uint32_t components_nb; + map_entity_component_def_t *components_defs; +} map_entity_def_t; + +typedef struct map_t { + uint32_t width, height; + uint16_t *backgroundlayer1; + uint16_t *backgroundlayer2; + uint16_t *backgroundlayer3; + uint16_t *foregroundlayer; + uint8_t *hitboxes; + uint32_t entities_nb; + map_entity_def_t *entities_defs; +} map_t; + +map_t *load_map(void); + +void destroy_map(map_t *map); + +void map_load_entities(map_t *map); + +#endif // MAP_H \ No newline at end of file diff --git a/src/headers/texture.h b/src/headers/texture.h index c360e56..3431453 100644 --- a/src/headers/texture.h +++ b/src/headers/texture.h @@ -2,11 +2,10 @@ #define TEXTURE_H #include -#include "asset_manager.h" typedef SDL_Texture texture_t; -texture_t *texture_load(FILE *asset_file); +texture_t *load_texture(void); void destroy_texture(texture_t *texture); #endif // TEXTURE_H \ No newline at end of file diff --git a/src/map.c b/src/map.c new file mode 100644 index 0000000..24bcc13 --- /dev/null +++ b/src/map.c @@ -0,0 +1,208 @@ +#include "map.h" + +#include "asset_manager.h" +#include "game.h" +#include "ecs.h" +#include "vector2d.h" +#include "memory_alloc.h" +#include "errors.h" + +map_t *load_map(void) +{ + map_t *map = malloc(sizeof(map_t)); + + if(fread(&map->width, sizeof(map->width), 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + if(fread(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + map->backgroundlayer1 = malloc(map->width * map->height * sizeof(uint16_t)); + if(fread(map->backgroundlayer1, sizeof(uint16_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + map->backgroundlayer2 = malloc(map->width * map->height * sizeof(uint16_t)); + if(fread(map->backgroundlayer2, sizeof(uint16_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + map->backgroundlayer3 = malloc(map->width * map->height * sizeof(uint16_t)); + if(fread(map->backgroundlayer3, sizeof(uint16_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + map->foregroundlayer = malloc(map->width * map->height * sizeof(uint16_t)); + if(fread(map->foregroundlayer, sizeof(uint16_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + map->hitboxes = malloc(map->width * map->height * sizeof(uint8_t)); + if(fread(map->hitboxes, sizeof(uint8_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + if(fread(&map->entities_nb, sizeof(map->entities_nb), 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + map->entities_defs = calloc(map->entities_nb, sizeof(map_entity_def_t)); + + for(uint32_t i = 0; i < map->entities_nb; i++) + { + map_entity_def_t *entity_def = &map->entities_defs[i]; + + if(fread(&entity_def->id, sizeof(entity_def->id), 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + if(fread(&entity_def->components_nb, sizeof(entity_def->components_nb), 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + entity_def->components_defs = calloc(entity_def->components_nb, sizeof(map_entity_component_def_t)); + + for(uint32_t j = 0; j < entity_def->components_nb; j++) + { + map_entity_component_def_t *component_def = &entity_def->components_defs[j]; + + if(fread(&component_def->component_type, sizeof(component_def->component_type), 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + + switch(component_def->component_type) + { + size_t data_size; + + case TRANSFORM_COMPONENT: + case HITBOX_COMPONENT: + data_size = sizeof(frect_t); + component_def->attributes = malloc(data_size); + if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + break; + + case SPRITE_COMPONENT: + { + size_t cap = 0; + if(getdelim((char **)&component_def->attributes, &cap, '\0', game.asset_manager.asset_file) == -1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + break; + } + + case ANIMATION_SYSTEM: + data_size = sizeof(int) * 2 + sizeof(float) + sizeof(bool) * 3; + component_def->attributes = malloc(data_size); + if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + break; + + case INTERACT_COMPONENT: + data_size = sizeof(frect_t) + sizeof(int) * 3; + component_def->attributes = malloc(data_size); + if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + break; + + case DOOR_COMPONENT: + data_size = sizeof(int) + sizeof(bool); + component_def->attributes = malloc(data_size); + if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + break; + + case LEVER_COMPONENT: + data_size = sizeof(int) + sizeof(bool); + component_def->attributes = malloc(data_size); + if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) + { + error_printf("Failed to read in asset file."); + return NULL; + } + break; + + default: + error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->component_type), component_def->component_type); + return NULL; + } + } + } + + return map; +} + +void destroy_map(map_t *map) +{ + if(map->entities_defs) + { + for(uint32_t i = 0; i < map->entities_nb; i++) + { + map_entity_def_t *entity_def = &map->entities_defs[i]; + + if(entity_def->components_defs) + { + for(uint32_t j = 0; j < map->entities_defs->components_nb; j++) + { + map_entity_component_def_t *component_def = &entity_def->components_defs[j]; + if(component_def->attributes) free(component_def->attributes); + } + + free(entity_def->components_defs); + } + } + + free(map->entities_defs); + } + + if(map->hitboxes) free(map->hitboxes); + + if(map->foregroundlayer) free(map->foregroundlayer); + + if(map->backgroundlayer3) free(map->backgroundlayer3); + if(map->backgroundlayer2) free(map->backgroundlayer2); + if(map->backgroundlayer1) free(map->backgroundlayer1); +} + +void map_load_entities(map_t *map) +{ + +} diff --git a/src/texture.c b/src/texture.c index c2c0c16..6412ef4 100644 --- a/src/texture.c +++ b/src/texture.c @@ -1,20 +1,21 @@ #include "texture.h" #include "asset_manager.h" +#include "game.h" #include "display.h" #include "errors.h" -texture_t *texture_load(FILE *asset_file) +texture_t *load_texture(void) { uint32_t width, height; - if(fread(&width, sizeof(width), 1, asset_file) != 1) + if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1) { error_printf("Failed to read in asset file."); return NULL; } - if(fread(&height, sizeof(height), 1, asset_file) != 1) + if(fread(&height, sizeof(height), 1, game.asset_manager.asset_file) != 1) { error_printf("Failed to read in asset file."); return NULL; @@ -34,7 +35,7 @@ texture_t *texture_load(FILE *asset_file) return NULL; } - if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, asset_file) != width * height) + if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != width * height) { error_printf("Failed to read asset file."); SDL_DestroySurface(tmp_surface);