Compare commits

...

9 Commits

12 changed files with 143 additions and 117 deletions

View File

@ -16,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 -O0 -g -DDEBUG=2 -DASSET_FILE_NAME=\"$(notdir $(ASSET_OUTPUT))\" -D_GNU_SOURCE=1 $(shell pkg-config --cflags sdl3) \
CORE_CFLAGS = --std=c17 --pedantic -O1 -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 \

View File

@ -99,9 +99,13 @@ inline int display_clear(void)
return EXIT_SUCCESS;
}
inline void display_update(void)
inline int display_update(void)
{
SDL_RenderPresent(renderer);
if(!SDL_RenderPresent(renderer))
{
error_printf("Failed to present renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
#if DEBUG >= 1
float fps = 1000.0f / game.delta_time_ms;
@ -118,8 +122,16 @@ inline void display_update(void)
{
fps_update_delta_time = 0.0f;
char title[100];
sprintf(title, "2D_Engine | cur: %.2ffps | max: %.2ffps | low: %.2ffps", fps, max_fps_10s, low_fps_10s);
SDL_SetWindowTitle(window, title);
if(sprintf(title, "2D_Engine | cur: %.2ffps | max: %.2ffps | low: %.2ffps", fps, max_fps_10s, low_fps_10s) == -1)
{
error_printf("Sprintf failed : %d", errno);
return EXIT_FAILURE;
}
if(!SDL_SetWindowTitle(window, title))
{
error_printf("Failed to set window title : %s", SDL_GetError());
return EXIT_FAILURE;
}
}
static float fps_max_low_update_delta_time = 0.0f;
@ -132,6 +144,8 @@ inline void display_update(void)
low_fps_10s = 10000000.0f;
}
#endif // DEBUG >= 1
return EXIT_SUCCESS;
}
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const frect_t *dst_frect, bool flip)

View File

@ -197,16 +197,18 @@ inline int entity_manager_draw(void)
}
// Only for this file
// Arg : unsigned int id
// Arg : size_t id
static inline bool condition_is_entity_name(elem_t *elem, void *arg)
{
return ((entity_t *)elem->data)->id == *(unsigned int *)arg;
return ((entity_t *)elem->data)->id == *(size_t *)arg;
}
inline int entity_manager_remove_entity(unsigned int id)
inline int entity_manager_remove_entity(size_t id)
{
if(linked_list_remove_if(&game.entity_manager.entities, condition_is_entity_name, &id)) return_failure_int;
debug_printf("Removed entity %lu from entity manager.", id);
return EXIT_SUCCESS;
}

View File

@ -47,8 +47,16 @@ inline int event_bus_exit(void)
return EXIT_SUCCESS;
}
// Arg : size_t id
static inline bool condition_is_topic_id(elem_t *elem, void *arg)
{
return ((topic_t *)elem->data)->id == *(size_t *)arg;
}
int event_bus_new_topic(size_t id)
{
if(linked_list_get_if(&game.event_bus.topics, condition_is_topic_id, &id)) return EXIT_SUCCESS;
elem_t *topic_elem = create_elem(&game.event_bus.topics, id);
if(!topic_elem) return_failure_int;
@ -59,26 +67,11 @@ int event_bus_new_topic(size_t id)
return EXIT_SUCCESS;
}
// Arg : size_t id
static inline bool condition_is_topic_id(elem_t *elem, void *arg)
{
return ((topic_t *)elem->data)->id == *(size_t *)arg;
}
inline int event_bus_remove_topic(size_t id)
{
if(linked_list_remove_if(&game.event_bus.topics, condition_is_topic_id, &id)) return_failure_int;
return EXIT_SUCCESS;
}
int event_bus_change_topic_id(size_t old_id, size_t new_id)
{
elem_t *topic_elem = linked_list_get_if(&game.event_bus.topics, condition_is_topic_id, &old_id);
if(!topic_elem) return_failure_int;
topic_t *topic = topic_elem->data;
topic->id = new_id;
debug_printf("Removed topic with id : %lu", id);
return EXIT_SUCCESS;
}

View File

@ -26,7 +26,7 @@ int display_init(void);
void display_exit(void);
int display_clear(void);
void display_update(void);
int display_update(void);
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const frect_t *dst_frect, bool flip);
int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a);

View File

@ -66,6 +66,6 @@ int entity_manager_exit(void);
entity_t *entity_manager_new_entity(size_t id);
int entity_manager_update(void);
int entity_manager_draw(void);
int entity_manager_remove_entity(const unsigned int id);
int entity_manager_remove_entity(size_t id);
#endif // ECS_H

View File

@ -30,7 +30,6 @@ int event_bus_exit(void);
int event_bus_new_topic(size_t id);
int event_bus_remove_topic(size_t id);
int event_bus_change_topic_id(size_t old_id, size_t new_id);
int event_bus_publish(size_t id, void *arg);
int event_bus_subscribe(size_t id, callback_t callback, void *data);

View File

@ -4,7 +4,7 @@
#include <stdint.h>
typedef struct map_entity_component_def_t {
uint32_t component_type;
uint32_t type;
void *attributes;
} map_entity_component_def_t;
@ -31,4 +31,6 @@ void destroy_map(map_t *map);
int map_load_entities(map_t *map);
int map_remove_entities(map_t *map);
#endif // MAP_H

View File

@ -3,6 +3,8 @@
#include <SDL3/SDL_render.h>
#define TEXTURE_TRANSPARENT_COLOR 0x0001
typedef SDL_Texture texture_t;
texture_t *load_texture(void);

View File

@ -320,7 +320,9 @@ int linked_list_remove(linked_list_t *linked_list, elem_t *elem)
elem_t *next_elem = elem->next;
if(prev_elem) prev_elem->next = next_elem;
else linked_list->first = next_elem;
if(next_elem) next_elem->prev = prev_elem;
else linked_list->last = prev_elem;
if(destroy_elem(linked_list, elem)) return_failure_int;

188
src/map.c
View File

@ -89,13 +89,13 @@ map_t *load_map(void)
{
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)
if(fread(&component_def->type, sizeof(component_def->type), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file.");
return NULL;
}
switch(component_def->component_type)
switch(component_def->type)
{
size_t data_size;
@ -162,7 +162,7 @@ map_t *load_map(void)
break;
default:
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->component_type), component_def->component_type);
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->type), component_def->type);
return NULL;
}
}
@ -181,7 +181,7 @@ void destroy_map(map_t *map)
if(entity_def->components_defs)
{
for(uint32_t j = 0; j < map->entities_defs->components_nb; j++)
for(uint32_t j = 0; j < entity_def->components_nb; j++)
{
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
if(component_def->attributes) free(component_def->attributes);
@ -201,6 +201,8 @@ void destroy_map(map_t *map)
if(map->backgroundlayer3) free(map->backgroundlayer3);
if(map->backgroundlayer2) free(map->backgroundlayer2);
if(map->backgroundlayer1) free(map->backgroundlayer1);
free(map);
}
int map_load_entities(map_t *map)
@ -216,40 +218,28 @@ int map_load_entities(map_t *map)
{
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
component_t *component = entity_new_component(entity, component_def->component_type);
component_t *component = entity_new_component(entity, component_def->type);
switch(component_def->component_type)
switch(component_def->type)
{
case TRANSFORM_COMPONENT:
{
union {
void *raw;
struct {
frect_t bounds;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
frect_t bounds;
} *data = component_def->attributes;
transform_component_data_t *component_data = component->data;
component_data->bounds = data.soft->bounds;
component_data->bounds = data->bounds;
break;
}
case SPRITE_COMPONENT:
{
union {
void *raw;
struct {
char texture_name[];
} *soft;
} data;
data.raw = component_def->attributes;
sprite_component_data_t *component_data = component->data;
asset_t *texture_asset = asset_manager_load_asset(data.soft->texture_name, ASSET_TEXTURE);
asset_t *texture_asset = asset_manager_load_asset((char *)component_def->attributes, ASSET_TEXTURE);
if(!texture_asset) return_failure_int;
component_data->texture = texture_asset->data;
@ -259,27 +249,23 @@ int map_load_entities(map_t *map)
case ANIMATION_SYSTEM:
{
union {
void *raw;
struct {
int nb_frames;
int current_frame_nb;
float frame_delay_ms;
bool play;
bool loop;
bool reverse;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
int nb_frames;
int current_frame_nb;
float frame_delay_ms;
bool play;
bool loop;
bool reverse;
} *data = component_def->attributes;
animation_system_data_t *system_data = component->data;
system_data->nb_frames = data.soft->nb_frames;
system_data->current_frame_nb = data.soft->current_frame_nb;
system_data->frame_delay_ms = data.soft->frame_delay_ms;
system_data->play = data.soft->play;
system_data->loop = data.soft->loop;
system_data->reverse = data.soft->reverse;
system_data->nb_frames = data->nb_frames;
system_data->current_frame_nb = data->current_frame_nb;
system_data->frame_delay_ms = data->frame_delay_ms;
system_data->play = data->play;
system_data->loop = data->loop;
system_data->reverse = data->reverse;
animation_system_apply_changes(component);
@ -288,38 +274,30 @@ int map_load_entities(map_t *map)
case HITBOX_COMPONENT:
{
union {
void *raw;
struct {
fvector2d_t position;
fvector2d_t scale;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
fvector2d_t position;
fvector2d_t scale;
} *data = component_def->attributes;
hitbox_component_data_t *component_data = component->data;
component_data->position = data.soft->position;
component_data->scale = data.soft->scale;
component_data->position = data->position;
component_data->scale = data->scale;
break;
}
case DOOR_COMPONENT:
{
union {
void *raw;
struct {
int topic_id;
bool state;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
int topic_id;
bool state;
} *data = component_def->attributes;
door_component_data_t *component_data = component->data;
component_data->topic_id = data.soft->topic_id;
component_data->state = data.soft->state;
component_data->topic_id = data->topic_id;
component_data->state = data->state;
if(door_component_subscribe(component)) return_failure_int;
@ -328,45 +306,37 @@ int map_load_entities(map_t *map)
case INTERACT_COMPONENT:
{
union {
void *raw;
struct {
fvector2d_t position;
fvector2d_t scale;
int topic_id;
int type;
int state;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
fvector2d_t position;
fvector2d_t scale;
int topic_id;
int type;
int state;
} *data = component_def->attributes;
interact_component_data_t *component_data = component->data;
component_data->position = data.soft->position;
component_data->scale = data.soft->scale;
component_data->type = data.soft->type;
component_data->state = data.soft->state;
component_data->position = data->position;
component_data->scale = data->scale;
component_data->type = data->type;
component_data->state = data->state;
if(interact_component_modify_topic_id(component, data.soft->topic_id)) return_failure_int;
if(event_bus_new_topic(data->topic_id)) return_failure_int;
break;
}
case LEVER_COMPONENT:
{
union {
void *raw;
struct {
int topic_id;
bool state;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
int topic_id;
bool state;
} *data = component_def->attributes;
lever_component_data_t *component_data = component->data;
component_data->topic_id = data.soft->topic_id;
component_data->state = data.soft->state;
component_data->topic_id = data->topic_id;
component_data->state = data->state;
if(lever_component_subscribe(component)) return_failure_int;
@ -374,7 +344,7 @@ int map_load_entities(map_t *map)
}
default:
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->component_type), component_def->component_type);
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->type), component_def->type);
return EXIT_FAILURE;
}
}
@ -382,3 +352,45 @@ int map_load_entities(map_t *map)
return EXIT_SUCCESS;
}
int map_remove_entities(map_t *map)
{
for(uint32_t i = 0; i < map->entities_nb; i++)
{
map_entity_def_t *entity_def = &map->entities_defs[i];
if(entity_manager_remove_entity(entity_def->id)) return_failure_int;
for(uint32_t j = 0; j < entity_def->components_nb; j++)
{
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
switch(component_def->type)
{
case SPRITE_COMPONENT:
if(asset_manager_let_go_asset((char *)component_def->attributes)) return_failure_int;
break;
case INTERACT_COMPONENT:
{
struct {
fvector2d_t position;
fvector2d_t scale;
int topic_id;
int type;
int state;
} *data = component_def->attributes;
if(event_bus_remove_topic(data->topic_id)) return_failure_int;
break;
}
default:
break;
}
}
}
return EXIT_SUCCESS;
}

View File

@ -42,7 +42,7 @@ texture_t *load_texture(void)
return NULL;
}
if(!SDL_SetSurfaceColorKey(tmp_surface, true, 0xF81F))
if(!SDL_SetSurfaceColorKey(tmp_surface, true, TEXTURE_TRANSPARENT_COLOR))
{
error_printf("Failed to set color key of temporary surface : %s", SDL_GetError());
SDL_DestroySurface(tmp_surface);