Preparing for official windows support.

This commit is contained in:
Ulysse Cura 2026-09-27 21:30:57 +02:00
parent 0f9bb7c188
commit 518b5d9438
7 changed files with 79 additions and 38 deletions

View File

@ -42,6 +42,14 @@ int destroy_asset(asset_t *asset)
return EXIT_SUCCESS; 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) int destroy_asset_header_def(asset_header_def_t *asset_header_def)
{ {
free(asset_header_def->name); 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) 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, 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"); game.asset_manager.asset_file = fopen(ASSET_FILE_NAME, "rb");
if(!game.asset_manager.asset_file) if(!game.asset_manager.asset_file)
@ -64,7 +72,7 @@ int asset_manager_init(void)
uint32_t nb_assets; uint32_t nb_assets;
if(fread(&nb_assets, sizeof(nb_assets), 1, game.asset_manager.asset_file) != 1) 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; return EXIT_FAILURE;
} }
@ -76,17 +84,33 @@ int asset_manager_init(void)
if(!asset_header_def_elem) return_failure_int; if(!asset_header_def_elem) return_failure_int;
asset_header_def_t *asset_header_def = asset_header_def_elem->data; 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."); size_t name_size = 0;
return EXIT_FAILURE; 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) 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; return EXIT_FAILURE;
} }

View File

@ -29,6 +29,7 @@ typedef struct asset_header_def_t {
uint32_t start; uint32_t start;
} asset_header_def_t; } 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); int destroy_asset_header_def(asset_header_def_t *asset_header_def);
typedef struct asset_manager_t { typedef struct asset_manager_t {

View File

@ -7,6 +7,8 @@
#include "texture.h" #include "texture.h"
#include "errors.h" #include "errors.h"
#define DRIVERS "vulkan,gpu,software"
inline int display_init(void) inline int display_init(void)
{ {
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
@ -29,13 +31,13 @@ inline int display_init(void)
} }
#ifdef DRIVERS #ifdef DRIVERS
warning_printf("This build of 2D_Engine_C only innitialize for the following drivers : %s", DRIVERS);
debug_printf("Listing available render drivers."); debug_printf("Listing available render drivers.");
const int NB_DRIVERS = SDL_GetNumRenderDrivers(); const int NB_DRIVERS = SDL_GetNumRenderDrivers();
for(int i = 0; i < NB_DRIVERS; i++) for(int i = 0; i < NB_DRIVERS; i++)
debug_printf("%d : %s", i, SDL_GetRenderDriver(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); game.display.renderer = SDL_CreateRenderer(game.display.window, DRIVERS);
#else #else
game.display.renderer = SDL_CreateRenderer(game.display.window, NULL); game.display.renderer = SDL_CreateRenderer(game.display.window, NULL);

View File

@ -14,8 +14,6 @@
#define DISPLAY_WIDTH 720 #define DISPLAY_WIDTH 720
#define DISPLAY_HEIGHT 480 #define DISPLAY_HEIGHT 480
#define DRIVERS "vulkan,gpu,software"
typedef struct display_t { typedef struct display_t {
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;

View File

@ -11,13 +11,13 @@ texture_t *load_texture(void)
if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
if(fread(&height, sizeof(height), 1, game.asset_manager.asset_file) != 1) 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; 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) 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; goto_failure;
} }

View File

@ -7,8 +7,8 @@
typedef SDL_Texture texture_t; typedef SDL_Texture texture_t;
#define get_width(T) T->w #define texture_get_width(T) T->w
#define get_height(T) T->h #define texture_get_height(T) T->h
texture_t *load_texture(void); texture_t *load_texture(void);
void destroy_texture(texture_t *texture); void destroy_texture(texture_t *texture);

View File

@ -15,19 +15,19 @@ map_t *load_map(void)
if(fread(&map->width, sizeof(map->width), 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
if(fread(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
if(fread(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
@ -42,7 +42,7 @@ map_t *load_map(void)
uint16_t *backgroundlayer = backgroundlayer_elem->data; 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) 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; return NULL;
} }
@ -52,20 +52,20 @@ map_t *load_map(void)
map->foregroundlayer = malloc(map->width * map->height * sizeof(*map->foregroundlayer)); 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) 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; return NULL;
} }
map->hitboxes = malloc(map->width * map->height * sizeof(*map->hitboxes)); 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) 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; return NULL;
} }
if(fread(&map->entities_nb, sizeof(map->entities_nb), 1, game.asset_manager.asset_file) != 1) 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; 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) 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; return NULL;
} }
if(fread(&entity_def->components_nb, sizeof(entity_def->components_nb), 1, game.asset_manager.asset_file) != 1) 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; 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) 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; return NULL;
} }
@ -109,19 +109,35 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size); component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
break; break;
case SPRITE_COMPONENT: case SPRITE_COMPONENT:
{ {
size_t cap = 0; size_t name_size = 0;
if(getdelim((char **)&component_def->attributes, &cap, '\0', game.asset_manager.asset_file) == -1) int ch = EOF;
do
{ {
error_printf("Failed to read in asset file : %d", errno); name_size++;
return NULL;
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; break;
} }
@ -130,7 +146,7 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size); component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
break; break;
@ -140,7 +156,7 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size); component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
break; break;
@ -150,7 +166,7 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size); component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
break; break;
@ -160,7 +176,7 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size); component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1) 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; return NULL;
} }
break; 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; if(!tile_id) return EXIT_SUCCESS;
rect_t src_frect = { rect_t src_frect = {
.x = tile_id * TILE_SIZE % get_width(game.map->tileset), .x = tile_id * TILE_SIZE % texture_get_width(game.map->tileset),
.y = tile_id * TILE_SIZE / get_width(game.map->tileset) * TILE_SIZE, .y = tile_id * TILE_SIZE / texture_get_width(game.map->tileset) * TILE_SIZE,
.w = TILE_SIZE, .w = TILE_SIZE,
.h = TILE_SIZE .h = TILE_SIZE
}; };