diff --git a/core/asset_manager.c b/core/asset_manager.c index 56cc655..114674d 100644 --- a/core/asset_manager.c +++ b/core/asset_manager.c @@ -42,6 +42,14 @@ int destroy_asset(asset_t *asset) return EXIT_SUCCESS; } +// No args +int create_asset_header_def(asset_header_def_t *asset_header_def, va_list args) +{ + asset_header_def->name = NULL; + + return EXIT_SUCCESS; +} + int destroy_asset_header_def(asset_header_def_t *asset_header_def) { free(asset_header_def->name); @@ -52,7 +60,7 @@ int destroy_asset_header_def(asset_header_def_t *asset_header_def) int asset_manager_init(void) { linked_list_init(&game.asset_manager.assets, sizeof(asset_t), (allocator_t)create_asset, (deleter_t)destroy_asset); - linked_list_init(&game.asset_manager.assets_header_defs, sizeof(asset_header_def_t), NULL, (deleter_t)destroy_asset_header_def); + linked_list_init(&game.asset_manager.assets_header_defs, sizeof(asset_header_def_t), (allocator_t)create_asset_header_def, (deleter_t)destroy_asset_header_def); game.asset_manager.asset_file = fopen(ASSET_FILE_NAME, "rb"); if(!game.asset_manager.asset_file) @@ -64,7 +72,7 @@ int asset_manager_init(void) uint32_t nb_assets; if(fread(&nb_assets, sizeof(nb_assets), 1, game.asset_manager.asset_file) != 1) { - error_printf("Failed to read asset file."); + error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file)); return EXIT_FAILURE; } @@ -76,17 +84,33 @@ int asset_manager_init(void) if(!asset_header_def_elem) return_failure_int; asset_header_def_t *asset_header_def = asset_header_def_elem->data; - size_t cap = 0; - - if(getdelim(&asset_header_def->name, &cap, '\0', game.asset_manager.asset_file) == -1) { - error_printf("Failed to read asset file."); - return EXIT_FAILURE; + size_t name_size = 0; + int ch = EOF; + + do + { + name_size++; + + asset_header_def->name = realloc(asset_header_def->name, name_size); + + ch = fgetc(game.asset_manager.asset_file); + if(ch == EOF) + { + error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file)); + return EXIT_FAILURE; + } + + asset_header_def->name[name_size - 1] = ch; + } + while(ch != '\0'); + + asset_header_def->name[name_size] = '\0'; } if(fread(&asset_header_def->start, sizeof(asset_header_def->start), 1, game.asset_manager.asset_file) != 1) { - error_printf("Failed to read asset file."); + error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file)); return EXIT_FAILURE; } diff --git a/core/asset_manager.h b/core/asset_manager.h index 385900a..3068fea 100644 --- a/core/asset_manager.h +++ b/core/asset_manager.h @@ -29,6 +29,7 @@ typedef struct asset_header_def_t { uint32_t start; } asset_header_def_t; +int create_asset_header_def(asset_header_def_t *asset_header_def, va_list args); int destroy_asset_header_def(asset_header_def_t *asset_header_def); typedef struct asset_manager_t { diff --git a/core/host/linux/display.c b/core/host/linux/display.c index 37ce96b..ca8bc4c 100644 --- a/core/host/linux/display.c +++ b/core/host/linux/display.c @@ -7,6 +7,8 @@ #include "texture.h" #include "errors.h" +#define DRIVERS "vulkan,gpu,software" + inline int display_init(void) { if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) @@ -29,13 +31,13 @@ inline int display_init(void) } #ifdef DRIVERS + warning_printf("This build of 2D_Engine_C only innitialize for the following drivers : %s", DRIVERS); + debug_printf("Listing available render drivers."); const int NB_DRIVERS = SDL_GetNumRenderDrivers(); for(int i = 0; i < NB_DRIVERS; i++) debug_printf("%d : %s", i, SDL_GetRenderDriver(i)); - warning_printf("This build of 2D_Engine_C only support the following drivers : %s", DRIVERS); - game.display.renderer = SDL_CreateRenderer(game.display.window, DRIVERS); #else game.display.renderer = SDL_CreateRenderer(game.display.window, NULL); diff --git a/core/host/linux/display.h b/core/host/linux/display.h index 3083c53..d881e7a 100644 --- a/core/host/linux/display.h +++ b/core/host/linux/display.h @@ -14,8 +14,6 @@ #define DISPLAY_WIDTH 720 #define DISPLAY_HEIGHT 480 -#define DRIVERS "vulkan,gpu,software" - typedef struct display_t { SDL_Window *window; SDL_Renderer *renderer; diff --git a/core/host/linux/texture.c b/core/host/linux/texture.c index b159a19..0616975 100644 --- a/core/host/linux/texture.c +++ b/core/host/linux/texture.c @@ -11,13 +11,13 @@ texture_t *load_texture(void) if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1) { - error_printf("Failed to read in asset file."); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } if(fread(&height, sizeof(height), 1, game.asset_manager.asset_file) != 1) { - error_printf("Failed to read in asset file."); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } @@ -36,7 +36,7 @@ texture_t *load_texture(void) 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."); + error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file)); goto_failure; } diff --git a/core/host/linux/texture.h b/core/host/linux/texture.h index b61aabf..5901509 100644 --- a/core/host/linux/texture.h +++ b/core/host/linux/texture.h @@ -7,8 +7,8 @@ typedef SDL_Texture texture_t; -#define get_width(T) T->w -#define get_height(T) T->h +#define texture_get_width(T) T->w +#define texture_get_height(T) T->h texture_t *load_texture(void); void destroy_texture(texture_t *texture); diff --git a/core/map.c b/core/map.c index 300529e..0447d88 100644 --- a/core/map.c +++ b/core/map.c @@ -15,19 +15,19 @@ map_t *load_map(void) if(fread(&map->width, sizeof(map->width), 1, game.asset_manager.asset_file) != 1) { - error_printf("Failed to read in asset file : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } if(fread(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file) != 1) { - error_printf("Failed to read in asset file : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } @@ -42,7 +42,7 @@ map_t *load_map(void) uint16_t *backgroundlayer = backgroundlayer_elem->data; if(fread(backgroundlayer, sizeof(*backgroundlayer), 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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } @@ -52,20 +52,20 @@ map_t *load_map(void) map->foregroundlayer = malloc(map->width * map->height * sizeof(*map->foregroundlayer)); if(fread(map->foregroundlayer, sizeof(*map->foregroundlayer), 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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } map->hitboxes = malloc(map->width * map->height * sizeof(*map->hitboxes)); if(fread(map->hitboxes, sizeof(*map->hitboxes), 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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } @@ -77,13 +77,13 @@ map_t *load_map(void) if(fread(&entity_def->id, sizeof(entity_def->id), 1, game.asset_manager.asset_file) != 1) { - error_printf("Failed to read in asset file : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } @@ -95,7 +95,7 @@ map_t *load_map(void) if(fread(&component_def->type, sizeof(component_def->type), 1, game.asset_manager.asset_file) != 1) { - error_printf("Failed to read in asset file : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } @@ -109,19 +109,35 @@ map_t *load_map(void) 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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.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) + size_t name_size = 0; + int ch = EOF; + + do { - error_printf("Failed to read in asset file : %d", errno); - return NULL; + name_size++; + + component_def->attributes = realloc(component_def->attributes, name_size); + + ch = fgetc(game.asset_manager.asset_file); + if(ch == EOF) + { + error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file)); + return NULL; + } + + ((uint8_t *)component_def->attributes)[name_size - 1] = ch; } + while(ch != '\0'); + + ((uint8_t *)component_def->attributes)[name_size] = '\0'; + break; } @@ -130,7 +146,7 @@ map_t *load_map(void) 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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } break; @@ -140,7 +156,7 @@ map_t *load_map(void) 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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } break; @@ -150,7 +166,7 @@ map_t *load_map(void) 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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } break; @@ -160,7 +176,7 @@ map_t *load_map(void) 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 : %d", errno); + error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); return NULL; } break; @@ -406,8 +422,8 @@ static inline int draw_tile(uint16_t tile_x, uint16_t tile_y, uint16_t tile_id) if(!tile_id) return EXIT_SUCCESS; rect_t src_frect = { - .x = tile_id * TILE_SIZE % get_width(game.map->tileset), - .y = tile_id * TILE_SIZE / get_width(game.map->tileset) * TILE_SIZE, + .x = tile_id * TILE_SIZE % texture_get_width(game.map->tileset), + .y = tile_id * TILE_SIZE / texture_get_width(game.map->tileset) * TILE_SIZE, .w = TILE_SIZE, .h = TILE_SIZE };