From 30dc3adf1353bcda5bc4466878de0f1688f5e755 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Mon, 2 Nov 2026 22:34:40 +0100 Subject: [PATCH] Renamed macros for compatibility with diferent standar libraries. --- core/asset_manager.c | 41 +++++----- core/ecs.c | 29 +++---- core/event_bus.c | 24 +++--- core/host/fxcg50/Makefile | 3 + core/host/fxcg50/display.c | 44 +++++++++++ core/host/fxcg50/display.h | 9 ++- core/host/fxcg50/errors.h | 80 +++++++++++++++++++ core/host/fxcg50/inputs.c | 5 +- core/host/fxcg50/texture.c | 41 ++++++++++ core/host/fxcg50/texture.h | 4 +- core/host/fxcg50/update_time.c | 30 +++++-- core/host/fxcg50/utils.c | 14 ++++ core/host/fxcg50/utils.h | 8 ++ core/host/linux/Makefile | 1 + core/host/linux/display.c | 41 +++++----- core/host/linux/display.h | 10 +-- core/{ => host/linux}/errors.h | 15 ++-- core/host/linux/texture.c | 22 ++---- core/host/linux/update_time.c | 6 +- core/host/linux/utils.c | 14 ++++ core/host/linux/utils.h | 8 ++ core/linked_list.c | 31 ++++---- core/map.c | 112 ++++++--------------------- src/components/animation_system.c | 12 +-- src/components/door_component.c | 14 ++-- src/components/hitbox_component.c | 12 +-- src/components/interact_component.c | 14 ++-- src/components/lever_component.c | 12 +-- src/components/lua_component.c | 6 +- src/components/physics_system.c | 20 ++--- src/components/player_system.c | 22 +++--- src/components/sprite_component.c | 10 +-- src/components/transform_component.c | 10 +-- src/game.c | 8 +- 34 files changed, 449 insertions(+), 283 deletions(-) create mode 100644 core/host/fxcg50/display.c create mode 100644 core/host/fxcg50/errors.h create mode 100644 core/host/fxcg50/texture.c create mode 100644 core/host/fxcg50/utils.c create mode 100644 core/host/fxcg50/utils.h rename core/{ => host/linux}/errors.h (92%) create mode 100644 core/host/linux/utils.c create mode 100644 core/host/linux/utils.h diff --git a/core/asset_manager.c b/core/asset_manager.c index d8b6345..296ce58 100644 --- a/core/asset_manager.c +++ b/core/asset_manager.c @@ -2,10 +2,12 @@ #include #include +#include #include "game.h" #include "linked_list.h" #include "texture.h" #include "map.h" +#include "utils.h" #include "memory_alloc.h" #include "errors.h" @@ -16,7 +18,7 @@ int create_asset(asset_t *asset, va_list args) asset->type = ASSET_NULL; asset->nb_refs = 0; - return EXIT_SUCCESS; + return SUCCESS; } int destroy_asset(asset_t *asset) @@ -36,10 +38,10 @@ int destroy_asset(asset_t *asset) default: error_printf("Asset type not valid : %d", asset->type); - return EXIT_FAILURE; + return FAILURE; } - return EXIT_SUCCESS; + return SUCCESS; } // No args @@ -47,14 +49,14 @@ int create_asset_header_def(asset_header_def_t *asset_header_def, va_list args _ { asset_header_def->name = NULL; - return EXIT_SUCCESS; + return SUCCESS; } int destroy_asset_header_def(asset_header_def_t *asset_header_def) { free(asset_header_def->name); - return EXIT_SUCCESS; + return SUCCESS; } int asset_manager_init(void) @@ -66,15 +68,11 @@ int asset_manager_init(void) if(!game.asset_manager.asset_file) { error_printf("Failed to open asset file : %d", errno); - return EXIT_FAILURE; + return FAILURE; } 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 : %d", ferror(game.asset_manager.asset_file)); - return EXIT_FAILURE; - } + if(file_read(&nb_assets, sizeof(nb_assets), 1, game.asset_manager.asset_file)) return_failure_int; if(linked_list_reserve(&game.asset_manager.assets_header_defs, nb_assets)) return_failure_int; @@ -98,7 +96,7 @@ int asset_manager_init(void) if(ch == EOF) { error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file)); - return EXIT_FAILURE; + return FAILURE; } asset_header_def->name[name_size - 1] = ch; @@ -108,17 +106,14 @@ int asset_manager_init(void) 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 : %d", ferror(game.asset_manager.asset_file)); - return EXIT_FAILURE; - } + if(file_read(&asset_header_def->start, sizeof(asset_header_def->start), 1, game.asset_manager.asset_file)) return_failure_int; linked_list_push_back(&game.asset_manager.assets_header_defs, asset_header_def_elem); } debug_printf("Initialized asset manager."); - return EXIT_SUCCESS; + + return SUCCESS; } int asset_manager_exit(void) @@ -131,12 +126,12 @@ int asset_manager_exit(void) if(fclose(game.asset_manager.asset_file)) { error_printf("Failed to close asset file : %d", errno); - return EXIT_FAILURE; + return FAILURE; } } debug_printf("Exited asset manager."); - return EXIT_SUCCESS; + return SUCCESS; } // Only for this file @@ -150,7 +145,7 @@ inline int asset_manager_collect_garbage(void) { if(linked_list_remove_if(&game.asset_manager.assets, condition_is_asset_unused, NULL)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } // Only for this file @@ -237,7 +232,7 @@ int asset_manager_let_go_asset(char *asset_name) if(!asset_elem) { error_printf("Asset doesn't exists : %s", asset_name); - return EXIT_FAILURE; + return FAILURE; } asset_t *asset = asset_elem->data; @@ -245,7 +240,7 @@ int asset_manager_let_go_asset(char *asset_name) debug_printf("Asset \"%s\" has been let go, nb refs : %d", asset->name, asset->nb_refs); - return EXIT_SUCCESS; + return SUCCESS; } asset_t *asset_manager_get_asset(char *asset_name) diff --git a/core/ecs.c b/core/ecs.c index 7bf87f4..60987be 100644 --- a/core/ecs.c +++ b/core/ecs.c @@ -1,6 +1,7 @@ #include "ecs.h" #include +#include #include "game.h" #include "components.h" #include "memory_alloc.h" @@ -36,7 +37,7 @@ int create_component(component_t *component, va_list args) if(!component->data) \ { \ error_printf("Failed to allocate memory : %d", errno); \ - return EXIT_FAILURE; \ + return FAILURE; \ } \ component->deleter = PREFIX##_destroy; \ break; @@ -44,17 +45,17 @@ int create_component(component_t *component, va_list args) default: error_printf("Component type doesn't exists : %d", component->type); - return EXIT_FAILURE; + return FAILURE; } - return EXIT_SUCCESS; + return SUCCESS; } inline int destroy_component(component_t *component) { if(component->deleter(component)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } int create_entity(entity_t *entity, va_list args) @@ -64,14 +65,14 @@ int create_entity(entity_t *entity, va_list args) linked_list_init(&entity->components, sizeof(component_t), (allocator_t)create_component, (deleter_t)destroy_component); - return EXIT_SUCCESS; + return SUCCESS; } inline int destroy_entity(entity_t *entity) { if(linked_list_exit(&entity->components)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } component_t *entity_new_component(entity_t *entity, component_type_t component_type) @@ -115,14 +116,14 @@ static inline int action_update_component(elem_t *elem, void *arg __attribute__( if(component->update(component)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } inline int update_entity(entity_t *entity) { if(linked_list_for_each(&entity->components, action_update_component, NULL)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } // No args @@ -133,14 +134,14 @@ static inline int action_draw_component(elem_t *elem, void *arg __attribute__((u if(component->draw) if(component->draw(component)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } inline int draw_entity(entity_t *entity) { if(linked_list_for_each(&entity->components, action_draw_component, NULL)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } inline void entity_manager_init(void) @@ -172,7 +173,7 @@ inline int entity_manager_update(void) { if(linked_list_for_each(&game.entity_manager.entities, action_update_entity, NULL)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } // No args @@ -193,7 +194,7 @@ inline int entity_manager_draw(void) if(linked_list_for_each(&game.entity_manager.entities, action_draw_entity, NULL)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } // Only for this file @@ -209,7 +210,7 @@ inline int entity_manager_remove_entity(size_t id) debug_printf("Removed entity %lu from entity manager.", id); - return EXIT_SUCCESS; + return SUCCESS; } inline int entity_manager_exit(void) @@ -218,5 +219,5 @@ inline int entity_manager_exit(void) debug_printf("Exited entity manager."); - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/core/event_bus.c b/core/event_bus.c index 72a133f..ed572e2 100644 --- a/core/event_bus.c +++ b/core/event_bus.c @@ -12,7 +12,7 @@ inline int create_subscription(subscription_t *subscription, va_list args) subscription->callback = va_arg(args, callback_t); subscription->data = va_arg(args, void *); - return EXIT_SUCCESS; + return SUCCESS; } // Arg : size_t id @@ -22,14 +22,14 @@ inline int create_topic(topic_t *topic, va_list args) linked_list_init(&topic->subscriptions, sizeof(subscription_t), (allocator_t)create_subscription, NULL); - return EXIT_SUCCESS; + return SUCCESS; } inline int destroy_topic(topic_t *topic) { if(linked_list_exit(&topic->subscriptions)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } inline void event_bus_init(void) @@ -44,7 +44,7 @@ inline int event_bus_exit(void) if(linked_list_exit(&game.event_bus.topics)) return_failure_int; debug_printf("Exited event bus."); - return EXIT_SUCCESS; + return SUCCESS; } // Arg : size_t id @@ -55,7 +55,7 @@ static inline bool condition_is_topic_id(elem_t *elem, void *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; + if(linked_list_get_if(&game.event_bus.topics, condition_is_topic_id, &id)) return SUCCESS; elem_t *topic_elem = create_elem(&game.event_bus.topics, id); if(!topic_elem) return_failure_int; @@ -64,7 +64,7 @@ int event_bus_new_topic(size_t id) debug_printf("Created new topic with id : %lu", id); - return EXIT_SUCCESS; + return SUCCESS; } inline int event_bus_remove_topic(size_t id) @@ -73,7 +73,7 @@ inline int event_bus_remove_topic(size_t id) debug_printf("Removed topic with id : %lu", id); - return EXIT_SUCCESS; + return SUCCESS; } // Args : va_list args @@ -83,7 +83,7 @@ static int action_execute_callback(elem_t *elem, void *arg) if(subscription->callback(arg, subscription->data)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } int event_bus_publish(size_t id, void *arg) @@ -97,7 +97,7 @@ int event_bus_publish(size_t id, void *arg) debug_printf("Published event on topic with id : %lu", id); - return EXIT_SUCCESS; + return SUCCESS; } int event_bus_subscribe(size_t id, callback_t callback, void *data) @@ -106,7 +106,7 @@ int event_bus_subscribe(size_t id, callback_t callback, void *data) if(!topic_elem) { error_printf("Topic with id : %lu doesn't exists.", id); - return EXIT_FAILURE; + return FAILURE; } topic_t *topic = topic_elem->data; @@ -118,7 +118,7 @@ int event_bus_subscribe(size_t id, callback_t callback, void *data) debug_printf("Subscribed to topic with id : %lu", id); - return EXIT_SUCCESS; + return SUCCESS; } // Arg : callback_t callback @@ -136,5 +136,5 @@ int event_bus_unsubscribe(size_t id, callback_t callback) if(linked_list_remove_if(&topic->subscriptions, condition_is_callback, &callback)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/core/host/fxcg50/Makefile b/core/host/fxcg50/Makefile index 6613fbb..fca5c5b 100644 --- a/core/host/fxcg50/Makefile +++ b/core/host/fxcg50/Makefile @@ -1,4 +1,7 @@ SOURCES += $(addprefix host/$(TARGET_HOST)/, \ + utils.c \ + display.c \ + texture.c \ vector2d.c \ inputs.c \ update_time.c \ diff --git a/core/host/fxcg50/display.c b/core/host/fxcg50/display.c new file mode 100644 index 0000000..16035e9 --- /dev/null +++ b/core/host/fxcg50/display.c @@ -0,0 +1,44 @@ +#include "display.h" + +#include +#include +#include "game.h" +#include "errors.h" + +int vpos = 0; + +inline int display_init(void) +{ + return SUCCESS; +} + +inline void display_exit(void) +{ + #include + dupdate(); + getkey(); +} + +inline int display_clear(void) +{ + dclear(C_WHITE); + return SUCCESS; +} + +inline int display_update(void) +{ + dupdate(); + return SUCCESS; +} + +inline int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip) +{ + dsubimage_rgb16_effect((int)dst_rect->x, (int)dst_rect->y, texture, (int)src_rect->x, (int)src_rect->y, (int)src_rect->w, (int)src_rect->h, flip ? IMAGE_HFLIP : DIMAGE_NONE); + return SUCCESS; +} + +inline int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a) +{ + drect((int)frect->x, (int)frect->y, (int)(frect->x + frect->w), (int)(frect->y + frect->h), (r >> 3 ) | (g >> 2) | (g >> 3)); + return SUCCESS; +} diff --git a/core/host/fxcg50/display.h b/core/host/fxcg50/display.h index b344c06..2100124 100644 --- a/core/host/fxcg50/display.h +++ b/core/host/fxcg50/display.h @@ -5,11 +5,16 @@ #include "texture.h" #include "vector2d.h" +#define COLOR_BLACK 0, 0, 0 +#define COLOR_RED 255, 0, 0 +#define COLOR_GREEN 0, 255, 0 +#define COLOR_BLUE 0, 0, 255 +#define COLOR_WHITE 255, 255, 255 + #define DISPLAY_WIDTH DWIDTH #define DISPLAY_HEIGHT DHEIGHT -typedef struct display_t { -} display_t; +typedef int display_t; int display_init(void); void display_exit(void); diff --git a/core/host/fxcg50/errors.h b/core/host/fxcg50/errors.h new file mode 100644 index 0000000..c858a55 --- /dev/null +++ b/core/host/fxcg50/errors.h @@ -0,0 +1,80 @@ +#ifndef _ERRORS_H +#define _ERRORS_H + +#include +#include + +#define SUCCESS 0 +#define FAILURE 1 + +#ifndef DEBUG +#define DEBUG 0 +#endif + +extern int vpos; + +#if DEBUG > 0 +#define debug_printf(...) \ + do { \ + dprint_opt(0, vpos, C_BLACK, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "INFO: " __VA_ARGS__); \ + vpos = vpos % DHEIGHT + 10; \ + dupdate(); \ + } while (0) + +#define warning_printf(...) \ + do { \ + dprint_opt(0, vpos, C_BLACK, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "WARNING: " __VA_ARGS__); \ + vpos = vpos % DHEIGHT + 10; \ + dupdate(); \ + } while (0) + +#define error_printf(...) \ + do { \ + dprint_opt(0, vpos, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "ERROR: " __VA_ARGS__); \ + vpos = vpos % DHEIGHT + 10; \ + dupdate(); \ + getkey(); \ + } while (0) + +#define return_failure_int \ + do { \ + dprint_opt(-250, vpos, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "in %s:%d", __FILE__, __LINE__); \ + vpos = vpos % DHEIGHT + 10; \ + return EXIT_FAILURE; \ + } while (0); + +#define return_failure_ptr \ + do { \ + dprint_opt(-250, vpos, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "in %s:%d", __FILE__, __LINE__); \ + vpos = vpos % DHEIGHT + 10; \ + return NULL; \ + } while (0); + +#define goto_failure \ + do { \ + dprint_opt(-250, vpos, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "in %s:%d", __FILE__, __LINE__); \ + vpos = vpos % DHEIGHT + 10; \ + goto failure; \ + } while (0); +#else +#define debug_printf(...) + +#define warning_printf(...) \ + do { \ + dprint_opt(0, 0, C_BLACK, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "WARNING: " __VA_ARGS__); \ + dupdate(); \ + } while (0) + +#define error_printf(...) \ + do { \ + dprint_opt(0, 10, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "ERROR: " __VA_ARGS__); \ + dupdate(); \ + getkey(); \ + } while (0) + +#define return_failure_int return FAILURE; +#define return_failure_ptr return NULL; +#define goto_failure goto failure; +#endif + +#endif // _ERRORS_H \ No newline at end of file diff --git a/core/host/fxcg50/inputs.c b/core/host/fxcg50/inputs.c index 5caf27b..cda48f1 100644 --- a/core/host/fxcg50/inputs.c +++ b/core/host/fxcg50/inputs.c @@ -6,11 +6,14 @@ inline int poll_event(input_event_t *event) { key_event_t gint_event = pollevent(); - if(gint_event.type = KEYEV_NONE) + if(gint_event.type == KEYEV_NONE) return 0; event->type = gint_event.type; event->key = gint_event.key; + if(event->key == KEY_EXIT) + event->type = INPUT_EVENT_QUIT; + return 1; } diff --git a/core/host/fxcg50/texture.c b/core/host/fxcg50/texture.c new file mode 100644 index 0000000..fa3d3e7 --- /dev/null +++ b/core/host/fxcg50/texture.c @@ -0,0 +1,41 @@ +#include "texture.h" + +#include +#include "asset_manager.h" +#include "game.h" +#include "display.h" +#include "utils.h" +#include "errors.h" + +texture_t *load_texture(void) +{ + uint16_t width, height; + + if(file_read(&width, sizeof(width), 1, game.asset_manager.asset_file)) return_failure_ptr; + + if(file_read(&height, sizeof(height), 1, game.asset_manager.asset_file)) return_failure_ptr; + + texture_t *texture = image_alloc(width, height, IMAGE_RGB565A); + if(!texture) + { + error_printf("Failed to create texture."); + return NULL; + } + + for(uint16_t line = 0; line < height; line++) + { + if(file_read(&((uint8_t *)texture->data)[line * texture->stride], sizeof(uint16_t), width, game.asset_manager.asset_file)) goto_failure; + } + + return texture; + + failure: + image_free(texture); + + return NULL; +} + +void destroy_texture(texture_t *texture) +{ + image_free(texture); +} diff --git a/core/host/fxcg50/texture.h b/core/host/fxcg50/texture.h index d92649b..96c689c 100644 --- a/core/host/fxcg50/texture.h +++ b/core/host/fxcg50/texture.h @@ -7,8 +7,8 @@ typedef bopti_image_t texture_t; -#define get_width(T) T->width -#define get_height(T) T->height +#define texture_get_width(T) T->width +#define texture_get_height(T) T->height texture_t *load_texture(void); void destroy_texture(texture_t *texture); diff --git a/core/host/fxcg50/update_time.c b/core/host/fxcg50/update_time.c index c4ad525..0a35698 100644 --- a/core/host/fxcg50/update_time.c +++ b/core/host/fxcg50/update_time.c @@ -1,13 +1,31 @@ #include "update_time.h" -#include +#include +#include #include "game.h" +#define ENGINE_TICK 25 + +static int callback_tick(volatile int *tick) +{ + *tick = 1; + return TIMER_CONTINUE; +} + +static volatile int tick = 1; + +static int t; + inline void update_time(void) { - static clock_t last_clock_state = 0; - clock_t start_clock = clock(); - game.delta_time_ms = (float)(start_clock - last_clock_state) / (float)CLOCKS_PER_SEC; - if(game.delta_time_ms > 100.0f) game.delta_time_ms = 100.0f; - last_clock_state = start_clock; + if(t == 0) + { + t = timer_configure(TIMER_ANY, ENGINE_TICK*1000, GINT_CALL(callback_tick, &tick)); + if(t >= 0) timer_start(t); + } + + game.delta_time_ms = 25.0f; + + while(!tick) sleep(); + tick = 0; } diff --git a/core/host/fxcg50/utils.c b/core/host/fxcg50/utils.c new file mode 100644 index 0000000..0e1a6c1 --- /dev/null +++ b/core/host/fxcg50/utils.c @@ -0,0 +1,14 @@ +#include "utils.h" + +#include "errors.h" + +int file_read(void *ptr, size_t size, size_t nb, FILE *file) +{ + if(fread(ptr, size, nb, file) != size * nb) + { + error_printf("Failed to read file : %d", ferror(file)); + return FAILURE; + } + + return SUCCESS; +} diff --git a/core/host/fxcg50/utils.h b/core/host/fxcg50/utils.h new file mode 100644 index 0000000..9a09536 --- /dev/null +++ b/core/host/fxcg50/utils.h @@ -0,0 +1,8 @@ +#ifndef _UTILS_H +#define _UTILS_H + +#include + +int file_read(void *ptr, size_t size, size_t nb, FILE *file); + +#endif // _UTILS_H \ No newline at end of file diff --git a/core/host/linux/Makefile b/core/host/linux/Makefile index 7961d48..4bba8a4 100644 --- a/core/host/linux/Makefile +++ b/core/host/linux/Makefile @@ -1,4 +1,5 @@ SOURCES += $(addprefix host/$(TARGET_HOST)/, \ + utils.c \ display.c \ vector2d.c \ texture.c \ diff --git a/core/host/linux/display.c b/core/host/linux/display.c index 9aae029..fc10c32 100644 --- a/core/host/linux/display.c +++ b/core/host/linux/display.c @@ -2,6 +2,7 @@ #include #include +#include #include "asset_manager.h" #include "game.h" #include "texture.h" @@ -14,20 +15,20 @@ inline int display_init(void) if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { error_printf("Failed to initialise SDL3 : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } if(!SDL_SetAppMetadata("2D_Engine_SDL3_Core", "1.0", "2D_Engine")) { error_printf("Failed to set app metadata : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } game.display.window = SDL_CreateWindow("2D_Engine", DISPLAY_WIDTH, DISPLAY_HEIGHT, 0); if(!game.display.window) { error_printf("Failed to create window : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } #ifdef DRIVERS @@ -46,19 +47,19 @@ inline int display_init(void) if(!game.display.renderer) { error_printf("Failed to create renderer : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } if(!SDL_SetRenderVSync(game.display.renderer, 1)) { error_printf("Failed to activate adaptative vsync : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } if(!SDL_SetRenderDrawBlendMode(game.display.renderer, SDL_BLENDMODE_BLEND)) { error_printf("Failed to set renderer draw blend mode : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } /* if(!SDL_SetDefaultTextureScaleMode(game.display.renderer, SDL_SCALEMODE_NEAREST)) @@ -69,7 +70,7 @@ inline int display_init(void) */ debug_printf("Initialized display."); - return EXIT_SUCCESS; + return SUCCESS; } inline void display_exit(void) @@ -83,19 +84,19 @@ inline void display_exit(void) inline int display_clear(void) { - if(!SDL_SetRenderDrawColor(game.display.renderer, C_WHITE, 255)) + if(!SDL_SetRenderDrawColor(game.display.renderer, COLOR_WHITE, 255)) { error_printf("Failed to change renderer draw color : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } if(!SDL_RenderClear(game.display.renderer)) { error_printf("Failed to clear renderer : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } - return EXIT_SUCCESS; + return SUCCESS; } inline int display_update(void) @@ -103,7 +104,7 @@ inline int display_update(void) if(!SDL_RenderPresent(game.display.renderer)) { error_printf("Failed to present renderer : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } #if DEBUG >= 1 @@ -124,12 +125,12 @@ inline int display_update(void) 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; + return FAILURE; } if(!SDL_SetWindowTitle(game.display.window, title)) { error_printf("Failed to set window title : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } } @@ -144,7 +145,7 @@ inline int display_update(void) } #endif // DEBUG >= 1 - return EXIT_SUCCESS; + return SUCCESS; } int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip) @@ -164,10 +165,10 @@ int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_ if(!SDL_RenderTextureRotated(game.display.renderer, texture, src_rect ? &src_frect : NULL, dst_rect ? &dst_frect : NULL, 0, NULL, flip)) { error_printf("Failed to copy texture to renderer : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } - return EXIT_SUCCESS; + return SUCCESS; } int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a) @@ -175,7 +176,7 @@ int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, uns if(!SDL_SetRenderDrawColor(game.display.renderer, r, g, b, a)) { error_printf("Failed to change renderer draw color : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } frect_t display_frect = { @@ -188,8 +189,8 @@ int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, uns if(!SDL_RenderRect(game.display.renderer, &display_frect)) { error_printf("Failed to draw rectangle to renderer : %s", SDL_GetError()); - return EXIT_FAILURE; + return FAILURE; } - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/core/host/linux/display.h b/core/host/linux/display.h index 211540b..c009bca 100644 --- a/core/host/linux/display.h +++ b/core/host/linux/display.h @@ -5,11 +5,11 @@ #include "texture.h" #include "vector2d.h" -#define C_BLACK 0, 0, 0 -#define C_RED 255, 0, 0 -#define C_GREEN 0, 255, 0 -#define C_BLUE 0, 0, 255 -#define C_WHITE 255, 255, 255 +#define COLOR_BLACK 0, 0, 0 +#define COLOR_RED 255, 0, 0 +#define COLOR_GREEN 0, 255, 0 +#define COLOR_BLUE 0, 0, 255 +#define COLOR_WHITE 255, 255, 255 #define DISPLAY_WIDTH 720 #define DISPLAY_HEIGHT 480 diff --git a/core/errors.h b/core/host/linux/errors.h similarity index 92% rename from core/errors.h rename to core/host/linux/errors.h index f02d1d3..aaaae6f 100644 --- a/core/errors.h +++ b/core/host/linux/errors.h @@ -1,9 +1,10 @@ -#ifndef ERRORS_H -#define ERRORS_H +#ifndef _ERRORS_H +#define _ERRORS_H #include -#include -#include + +#define SUCCESS 0 +#define FAILURE 1 #ifndef DEBUG #define DEBUG 0 @@ -29,7 +30,7 @@ fputc('\n', stderr); \ } while (0) - #define return_failure_int \ +#define return_failure_int \ do { \ fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \ return EXIT_FAILURE; \ @@ -40,12 +41,12 @@ fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \ return NULL; \ } while (0); + #define goto_failure \ do { \ fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \ goto failure; \ } while (0); - #else #define debug_printf(...) @@ -66,4 +67,4 @@ #define goto_failure goto failure; #endif -#endif // ERRORS_H \ No newline at end of file +#endif // _ERRORS_H \ No newline at end of file diff --git a/core/host/linux/texture.c b/core/host/linux/texture.c index aaad3a7..589a38a 100644 --- a/core/host/linux/texture.c +++ b/core/host/linux/texture.c @@ -3,23 +3,16 @@ #include "asset_manager.h" #include "game.h" #include "display.h" +#include "utils.h" #include "errors.h" texture_t *load_texture(void) { uint16_t width, height; - if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1) - { - error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&width, sizeof(width), 1, game.asset_manager.asset_file)) return_failure_ptr; - if(fread(&height, sizeof(height), 1, game.asset_manager.asset_file) != 1) - { - error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&height, sizeof(height), 1, game.asset_manager.asset_file)) return_failure_ptr; SDL_Surface *tmp_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGB565); if(!tmp_surface) @@ -34,11 +27,7 @@ texture_t *load_texture(void) goto failure; } - 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 : %d", ferror(game.asset_manager.asset_file)); - goto failure; - } + if(file_read(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file)) goto_failure; if(!SDL_SetSurfaceColorKey(tmp_surface, true, TEXTURE_TRANSPARENT_COLOR)) { @@ -63,8 +52,9 @@ texture_t *load_texture(void) return texture; -failure: + failure: SDL_DestroySurface(tmp_surface); + return NULL; } diff --git a/core/host/linux/update_time.c b/core/host/linux/update_time.c index 3778b80..2d8430e 100644 --- a/core/host/linux/update_time.c +++ b/core/host/linux/update_time.c @@ -5,9 +5,9 @@ inline void update_time(void) { - static uint64_t last_clock_state = 0; + static uint64_t last_clock = 0; uint64_t start_clock = SDL_GetTicksNS(); - game.delta_time_ms = SDL_NS_TO_MS((float)(start_clock - last_clock_state)); + game.delta_time_ms = SDL_NS_TO_MS((float)(start_clock - last_clock)); if(game.delta_time_ms > 100.0f) game.delta_time_ms = 100.0f; - last_clock_state = start_clock; + last_clock = start_clock; } diff --git a/core/host/linux/utils.c b/core/host/linux/utils.c new file mode 100644 index 0000000..5cb86fc --- /dev/null +++ b/core/host/linux/utils.c @@ -0,0 +1,14 @@ +#include "utils.h" + +#include "errors.h" + +int file_read(void *ptr, size_t size, size_t nb, FILE *file) +{ + if(fread(ptr, size, nb, file) != nb) + { + error_printf("Failed to read file : %d", ferror(file)); + return FAILURE; + } + + return SUCCESS; +} diff --git a/core/host/linux/utils.h b/core/host/linux/utils.h new file mode 100644 index 0000000..9a09536 --- /dev/null +++ b/core/host/linux/utils.h @@ -0,0 +1,8 @@ +#ifndef _UTILS_H +#define _UTILS_H + +#include + +int file_read(void *ptr, size_t size, size_t nb, FILE *file); + +#endif // _UTILS_H \ No newline at end of file diff --git a/core/linked_list.c b/core/linked_list.c index ff117c9..4484846 100644 --- a/core/linked_list.c +++ b/core/linked_list.c @@ -1,6 +1,9 @@ -#include -#include "memory_alloc.h" #include "linked_list.h" + +#include +#include +#include +#include "memory_alloc.h" #include "errors.h" elem_t *create_elem(linked_list_t *linked_list, ...) @@ -65,7 +68,7 @@ inline int destroy_elem(linked_list_t *linked_list, elem_t *elem) free(elem); } - return EXIT_SUCCESS; + return SUCCESS; } void linked_list_init(linked_list_t *linked_list, size_t data_size, const allocator_t allocator, const deleter_t elem_deleter) @@ -114,7 +117,7 @@ int linked_list_exit(linked_list_t *linked_list) free(tmp); } - return EXIT_SUCCESS; + return SUCCESS; } int linked_list_clear(linked_list_t *linked_list) @@ -138,7 +141,7 @@ int linked_list_clear(linked_list_t *linked_list) linked_list->size = 0; - return EXIT_SUCCESS; + return SUCCESS; } int linked_list_reserve(linked_list_t *linked_list, size_t target_max_size) @@ -153,7 +156,7 @@ int linked_list_reserve(linked_list_t *linked_list, size_t target_max_size) if(!elem) { error_printf("Failed to allocate memory : %d", errno); - return EXIT_FAILURE; + return FAILURE; } elem->data = malloc(linked_list->data_size); @@ -161,14 +164,14 @@ int linked_list_reserve(linked_list_t *linked_list, size_t target_max_size) { free(elem); error_printf("Failed to allocate memory : %d", errno); - return EXIT_FAILURE; + return FAILURE; } elem->next = linked_list->reserved; linked_list->reserved = elem; } - return EXIT_SUCCESS; + return SUCCESS; } void linked_list_push_back(linked_list_t *linked_list, elem_t *elem) @@ -289,12 +292,12 @@ int linked_list_insert(linked_list_t *linked_list, elem_t *elem, size_t index) / if(index == 0) { linked_list_push_front(linked_list, elem); - return EXIT_SUCCESS; + return SUCCESS; } else if(index == linked_list->size) { linked_list_push_back(linked_list, elem); - return EXIT_SUCCESS; + return SUCCESS; } elem_t *next_insert_elem = linked_list_get(linked_list, index); @@ -309,7 +312,7 @@ int linked_list_insert(linked_list_t *linked_list, elem_t *elem, size_t index) / linked_list->size++; - return EXIT_SUCCESS; + return SUCCESS; } int linked_list_remove(linked_list_t *linked_list, elem_t *elem) @@ -328,7 +331,7 @@ int linked_list_remove(linked_list_t *linked_list, elem_t *elem) linked_list->size--; - return EXIT_SUCCESS; + return SUCCESS; } elem_t *linked_list_get_if(const linked_list_t *linked_list, const condition_t condition, void *arg) @@ -377,7 +380,7 @@ int linked_list_remove_if(linked_list_t *linked_list, const condition_t conditio actual_elem = next_elem; } - return EXIT_SUCCESS; + return SUCCESS; } int linked_list_for_each(const linked_list_t *linked_list, const action_t action, void *arg) @@ -397,7 +400,7 @@ int linked_list_for_each(const linked_list_t *linked_list, const action_t action current_elem = current_elem->next; } - return EXIT_SUCCESS; + return SUCCESS; } static inline void swap_elems_data(elem_t *elem_a, elem_t *elem_b) diff --git a/core/map.c b/core/map.c index d8ac1bc..e188d84 100644 --- a/core/map.c +++ b/core/map.c @@ -6,6 +6,7 @@ #include "components.h" #include "vector2d.h" #include "display.h" +#include "utils.h" #include "memory_alloc.h" #include "errors.h" @@ -13,23 +14,11 @@ 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 : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&map->width, sizeof(map->width), 1, game.asset_manager.asset_file)) return_failure_ptr; - if(fread(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file) != 1) - { - error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file)) return_failure_ptr; - if(fread(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file) != 1) - { - error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file)) return_failure_ptr; linked_list_init(&map->backgroundlayers, map->width * map->height * sizeof(uint16_t), NULL, NULL); if(linked_list_reserve(&map->backgroundlayers, map->backgroundlayernb)) return_failure_ptr; @@ -40,34 +29,18 @@ map_t *load_map(void) if(!backgroundlayer_elem) return_failure_ptr; 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", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(backgroundlayer, sizeof(*backgroundlayer), map->width * map->height, game.asset_manager.asset_file)) return_failure_ptr; linked_list_push_back(&map->backgroundlayers, backgroundlayer_elem); } 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", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(map->foregroundlayer, sizeof(*map->foregroundlayer), map->width * map->height, game.asset_manager.asset_file)) return_failure_ptr; 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", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(map->hitboxes, sizeof(*map->hitboxes), map->width * map->height, game.asset_manager.asset_file)) return_failure_ptr; - 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", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&map->entities_nb, sizeof(map->entities_nb), 1, game.asset_manager.asset_file)) return_failure_ptr; map->entities_defs = calloc(map->entities_nb, sizeof(map_entity_def_t)); @@ -75,17 +48,9 @@ map_t *load_map(void) { 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 : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&entity_def->id, sizeof(entity_def->id), 1, game.asset_manager.asset_file)) return_failure_ptr; - 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", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&entity_def->components_nb, sizeof(entity_def->components_nb), 1, game.asset_manager.asset_file)) return_failure_ptr; entity_def->components_defs = calloc(entity_def->components_nb, sizeof(map_entity_component_def_t)); @@ -93,11 +58,7 @@ map_t *load_map(void) { map_entity_component_def_t *component_def = &entity_def->components_defs[j]; - 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", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(&component_def->type, sizeof(component_def->type), 1, game.asset_manager.asset_file)) return_failure_ptr; switch(component_def->type) { @@ -107,11 +68,7 @@ map_t *load_map(void) case HITBOX_COMPONENT: data_size = sizeof(fvector2d_t) * 2; 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", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(component_def->attributes, data_size, 1, game.asset_manager.asset_file)) return_failure_ptr; break; case SPRITE_COMPONENT: @@ -144,41 +101,20 @@ map_t *load_map(void) 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 : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(component_def->attributes, data_size, 1, game.asset_manager.asset_file)) return_failure_ptr; break; case INTERACT_COMPONENT: data_size = sizeof(fvector2d_t) * 2 + 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 : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(component_def->attributes, data_size, 1, game.asset_manager.asset_file)) return_failure_ptr; 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 : %d", ferror(game.asset_manager.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 : %d", ferror(game.asset_manager.asset_file)); - return NULL; - } + if(file_read(component_def->attributes, data_size, 1, game.asset_manager.asset_file)) return_failure_ptr; break; default: @@ -222,7 +158,7 @@ int destroy_map(map_t *map) free(map); - return EXIT_SUCCESS; + return SUCCESS; } int map_load_entities(map_t *map) @@ -366,12 +302,12 @@ int map_load_entities(map_t *map) default: error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->type), component_def->type); - return EXIT_FAILURE; + return FAILURE; } } } - return EXIT_SUCCESS; + return SUCCESS; } int map_remove_entities(map_t *map) @@ -413,13 +349,13 @@ int map_remove_entities(map_t *map) } } - return EXIT_SUCCESS; + return SUCCESS; } 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 SUCCESS; rect_t src_frect = { .x = tile_id * TILE_SIZE % texture_get_width(game.map->tileset), @@ -437,7 +373,7 @@ static inline int draw_tile(uint16_t tile_x, uint16_t tile_y, uint16_t tile_id) if(display_draw_texture(game.map->tileset, &src_frect, &dst_frect, false)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } int map_display_background(void) @@ -470,7 +406,7 @@ int map_display_background(void) } } - return EXIT_SUCCESS; + return SUCCESS; } int map_display_foreground(void) @@ -497,7 +433,7 @@ int map_display_foreground(void) if(draw_tile(tile_x, tile_y, game.map->foregroundlayer[tile_y * game.map->width + tile_x])) return_failure_int; } - return EXIT_SUCCESS; + return SUCCESS; } #if DEBUG > 1 @@ -531,7 +467,7 @@ int map_display_hitboxes(void) .h = TILE_SIZE }; - if(display_draw_frect(&draw_frect, C_RED, 128)) return_failure_int; + if(display_draw_frect(&draw_frect, COLOR_RED, 128)) return_failure_int; } } diff --git a/src/components/animation_system.c b/src/components/animation_system.c index 5137d79..748bd26 100644 --- a/src/components/animation_system.c +++ b/src/components/animation_system.c @@ -15,14 +15,14 @@ int animation_system_init(component_t *system) if(!system_data->sprite_component) { error_printf("Animation system require a sprite component."); - return EXIT_FAILURE; + return FAILURE; } linked_list_init(&system_data->frames, sizeof(rect_t), NULL, NULL); system_data->frame_timer_ms = 0.0f; - return EXIT_SUCCESS; + return SUCCESS; } int animation_system_update(component_t *system) @@ -77,7 +77,7 @@ int animation_system_update(component_t *system) sprite_component_data->src_rect = *(rect_t *)system_data->current_frame->data; } - return EXIT_SUCCESS; + return SUCCESS; } inline int animation_system_destroy(component_t *system) @@ -88,7 +88,7 @@ inline int animation_system_destroy(component_t *system) free(system_data); - return EXIT_SUCCESS; + return SUCCESS; } inline int animation_system_apply_changes(component_t *system) @@ -119,10 +119,10 @@ inline int animation_system_apply_changes(component_t *system) if(!system_data->current_frame) { error_printf("Current frame number out of range."); - return EXIT_FAILURE; + return FAILURE; } sprite_component_data->src_rect = *(rect_t *)system_data->current_frame->data; - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/door_component.c b/src/components/door_component.c index 97a4304..0f47845 100644 --- a/src/components/door_component.c +++ b/src/components/door_component.c @@ -17,19 +17,19 @@ inline int door_component_init(component_t *component) if(!component_data->animation_system) { error_printf("Door component require an animation system."); - return EXIT_FAILURE; + return FAILURE; } component_data->hitbox_component = entity_get_component(component->entity, HITBOX_COMPONENT); if(!component_data->hitbox_component) { error_printf("Door component require an hitbox component."); - return EXIT_FAILURE; + return FAILURE; } component_data->last_state = false; - return EXIT_SUCCESS; + return SUCCESS; } inline int door_component_update(component_t *component) @@ -48,7 +48,7 @@ inline int door_component_update(component_t *component) component_data->last_state = component_data->state; - return EXIT_SUCCESS; + return SUCCESS; } inline int door_component_destroy(component_t *component) @@ -57,7 +57,7 @@ inline int door_component_destroy(component_t *component) free(component_data); - return EXIT_SUCCESS; + return SUCCESS; } // Event arg : size_t state, Subscription data : entity_t *entity @@ -68,7 +68,7 @@ static inline int door_component_callback(void *event_arg, void *subscription_da component_data->state = *(size_t *)event_arg; - return EXIT_SUCCESS; + return SUCCESS; } inline int door_component_subscribe(component_t *component) @@ -77,5 +77,5 @@ inline int door_component_subscribe(component_t *component) if(event_bus_subscribe(component_data->topic_id, door_component_callback, component)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/hitbox_component.c b/src/components/hitbox_component.c index 27ecc7f..078cca5 100644 --- a/src/components/hitbox_component.c +++ b/src/components/hitbox_component.c @@ -14,7 +14,7 @@ inline int hitbox_component_init(component_t *component) if(!component_data->transform_component) { error_printf("Hitbox component require a transform component."); - return EXIT_FAILURE; + return FAILURE; } component_data->position = (fvector2d_t) { @@ -29,7 +29,7 @@ inline int hitbox_component_init(component_t *component) component_data->activated = true; - return EXIT_SUCCESS; + return SUCCESS; } inline int hitbox_component_update(component_t *component) @@ -42,7 +42,7 @@ inline int hitbox_component_update(component_t *component) component_data->bounds.w = transform_component_data->bounds.w * component_data->scale.x; component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y; - return EXIT_SUCCESS; + return SUCCESS; } #if DEBUG >= 2 @@ -53,9 +53,9 @@ inline int hitbox_component_draw(const component_t *component) const hitbox_component_data_t *component_data = component->data; if(component_data->activated) - if(display_draw_frect(&component_data->bounds, C_RED, 128)) return_failure_int; + if(display_draw_frect(&component_data->bounds, COLOR_RED, 128)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } #endif // DEBUG >= 2 @@ -65,5 +65,5 @@ inline int hitbox_component_destroy(component_t *component) free(component_data); - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/interact_component.c b/src/components/interact_component.c index 1802c29..bbbf828 100644 --- a/src/components/interact_component.c +++ b/src/components/interact_component.c @@ -15,7 +15,7 @@ inline int interact_component_init(component_t *component) if(!component_data->transform_component) { error_printf("Hitbox component require a transform component."); - return EXIT_FAILURE; + return FAILURE; } component_data->position = (fvector2d_t) { @@ -30,7 +30,7 @@ inline int interact_component_init(component_t *component) component_data->topic_id = 0; component_data->activated = true; - return EXIT_SUCCESS; + return SUCCESS; } inline int interact_component_update(component_t *component) @@ -43,7 +43,7 @@ inline int interact_component_update(component_t *component) component_data->bounds.w = transform_component_data->bounds.w * component_data->scale.x; component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y; - return EXIT_SUCCESS; + return SUCCESS; } #if DEBUG >= 2 @@ -53,7 +53,7 @@ inline int interact_component_draw(const component_t *component) { const interact_component_data_t *component_data = component->data; - if(display_draw_frect(&component_data->bounds, C_GREEN, 128)) return_failure_int; + if(display_draw_frect(&component_data->bounds, COLOR_GREEN, 128)) return_failure_int; return EXIT_SUCCESS; } @@ -65,7 +65,7 @@ inline int interact_component_destroy(component_t *component) free(component_data); - return EXIT_SUCCESS; + return SUCCESS; } inline int interact_component_interact(component_t *component) @@ -80,10 +80,10 @@ inline int interact_component_interact(component_t *component) default: error_printf("Interaction type not valid : %d", component_data->type); - return EXIT_FAILURE; + return FAILURE; } if(event_bus_publish(component_data->topic_id, &component_data->state)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/lever_component.c b/src/components/lever_component.c index f2c17a8..4260c5a 100644 --- a/src/components/lever_component.c +++ b/src/components/lever_component.c @@ -17,12 +17,12 @@ inline int lever_component_init(component_t *component) if(!component_data->animation_system) { error_printf("Lever component require an animation system."); - return EXIT_FAILURE; + return FAILURE; } component_data->last_state = false; - return EXIT_SUCCESS; + return SUCCESS; } inline int lever_component_update(component_t *component) @@ -38,7 +38,7 @@ inline int lever_component_update(component_t *component) component_data->last_state = component_data->state; - return EXIT_SUCCESS; + return SUCCESS; } inline int lever_component_destroy(component_t *component) @@ -47,7 +47,7 @@ inline int lever_component_destroy(component_t *component) free(component_data); - return EXIT_SUCCESS; + return SUCCESS; } // Event arg : size_t state, Subscription data : component_t *component @@ -58,7 +58,7 @@ static inline int lever_component_callback(void *event_arg, void *subscription_d component_data->state = *(size_t *)event_arg; - return EXIT_SUCCESS; + return SUCCESS; } inline int lever_component_subscribe(component_t *component) @@ -67,5 +67,5 @@ inline int lever_component_subscribe(component_t *component) if(event_bus_subscribe(component_data->topic_id, lever_component_callback, component)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/lua_component.c b/src/components/lua_component.c index c04666b..e939865 100644 --- a/src/components/lua_component.c +++ b/src/components/lua_component.c @@ -18,7 +18,7 @@ inline int lua_component_init(component_t *component) luaL_dostring(component_data->lua_state, "function f (a) print(a) end"); - return EXIT_SUCCESS; + return SUCCESS; } inline int lua_component_update(component_t *component __attribute__((unused))) @@ -27,7 +27,7 @@ inline int lua_component_update(component_t *component __attribute__((unused))) luaL_dostring(component_data->lua_state, "f(\"prout\")"); - return EXIT_SUCCESS; + return SUCCESS; } #if DEBUG >= 2 @@ -47,5 +47,5 @@ inline int lua_component_destroy(component_t *component) free(component_data); - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/physics_system.c b/src/components/physics_system.c index 18885ec..7f9f98e 100644 --- a/src/components/physics_system.c +++ b/src/components/physics_system.c @@ -16,14 +16,14 @@ inline int physics_system_init(component_t *component) if(!system_data->transform_component) { error_printf("Physics system require a transform component."); - return EXIT_FAILURE; + return FAILURE; } system_data->hitbox_component = entity_get_component(component->entity, HITBOX_COMPONENT); if(!system_data->hitbox_component) { error_printf("Physics system require an hitbox component."); - return EXIT_FAILURE; + return FAILURE; } system_data->target_velocity = (fvector2d_t) { @@ -40,7 +40,7 @@ inline int physics_system_init(component_t *component) system_data->smoothing_factor = 1.0f; - return EXIT_SUCCESS; + return SUCCESS; } // Args : component_t *component @@ -58,7 +58,7 @@ int physics_system_update(component_t *component) if(hitbox_component_data->activated) { // Resolve entities collisions. - 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 FAILURE; // Resolve map collisions if(game.map) @@ -136,7 +136,7 @@ int physics_system_update(component_t *component) transform_component_data->bounds.x += system_data->velocity.x * (float)game.delta_time_ms / 1000.0f; transform_component_data->bounds.y += system_data->velocity.y * (float)game.delta_time_ms / 1000.0f; - return EXIT_SUCCESS; + return SUCCESS; } inline int physics_system_destroy(component_t *component) @@ -145,7 +145,7 @@ inline int physics_system_destroy(component_t *component) free(system_data); - return EXIT_SUCCESS; + return SUCCESS; } static inline int action_detect_solve_entities_collision(elem_t *elem, void *arg) @@ -160,15 +160,15 @@ static inline int action_detect_solve_entities_collision(elem_t *elem, void *arg if(target_entity->id == physics_system->entity->id) { - return EXIT_SUCCESS; + return SUCCESS; } const component_t *target_hitbox_component = entity_get_component(target_entity, HITBOX_COMPONENT); - if(!target_hitbox_component) return EXIT_SUCCESS; + if(!target_hitbox_component) return SUCCESS; const hitbox_component_data_t *target_hitbox_component_data = target_hitbox_component->data; - if(!target_hitbox_component_data->activated) return EXIT_SUCCESS; + if(!target_hitbox_component_data->activated) return SUCCESS; const frect_t *hitbox_bounds = &hitbox_component_data->bounds; const frect_t *target_hitbox_bounds = &target_hitbox_component_data->bounds; @@ -210,5 +210,5 @@ static inline int action_detect_solve_entities_collision(elem_t *elem, void *arg } } - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/player_system.c b/src/components/player_system.c index ff76485..b3da758 100644 --- a/src/components/player_system.c +++ b/src/components/player_system.c @@ -18,26 +18,26 @@ inline int player_system_init(component_t *component) if(!system_data->sprite_component) { error_printf("Player system require a sprite component."); - return EXIT_FAILURE; + return FAILURE; } system_data->animation_system = entity_get_component(component->entity, ANIMATION_SYSTEM); if(!system_data->animation_system) { error_printf("Player system require an animation system."); - return EXIT_FAILURE; + return FAILURE; } system_data->physics_system = entity_get_component(component->entity, PHYSICS_SYSTEM); if(!system_data->physics_system) { error_printf("Player system require a physics system."); - return EXIT_FAILURE; + return FAILURE; } system_data->state = PLAYER_MOVING; - return EXIT_SUCCESS; + return SUCCESS; } static void player_system_get_inputs(player_system_data_t *system_data); @@ -54,7 +54,7 @@ inline int player_system_update(component_t *component) if(player_system_set_animation_and_speed(system_data)) return_failure_int; if(player_system_process_interaction(system_data)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } inline int player_system_destroy(component_t *component) @@ -63,7 +63,7 @@ inline int player_system_destroy(component_t *component) free(component_data); - return EXIT_SUCCESS; + return SUCCESS; } static inline void player_system_get_inputs(player_system_data_t *system_data) @@ -169,7 +169,7 @@ static inline int player_system_set_animation_and_speed(player_system_data_t *sy if(animation_system_apply_changes(system_data->animation_system)) return_failure_int; } - return EXIT_SUCCESS; + return SUCCESS; } // Args : player_system_data_t *system_data @@ -181,13 +181,13 @@ static inline int action_detect_activate_interact(elem_t *elem, void *arg) physics_system_data_t *physics_system_data = system_data->physics_system->data; component_t *target_interact_component = entity_get_component(target_entity, INTERACT_COMPONENT); - if(!target_interact_component) return EXIT_SUCCESS; + if(!target_interact_component) return SUCCESS; interact_component_data_t *target_interact_component_data = target_interact_component->data; if(!target_interact_component_data->activated) { - return EXIT_SUCCESS; + return SUCCESS; } const frect_t *hitbox_bounds = &((hitbox_component_data_t *)physics_system_data->hitbox_component->data)->bounds; @@ -198,7 +198,7 @@ static inline int action_detect_activate_interact(elem_t *elem, void *arg) if(interact_component_interact(target_interact_component)) return_failure_int; } - return EXIT_SUCCESS; + return SUCCESS; } static inline int player_system_process_interaction(player_system_data_t *system_data) @@ -212,5 +212,5 @@ static inline int player_system_process_interaction(player_system_data_t *system last_state = game.inputs.keys[KEY_INTERACT]; - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/sprite_component.c b/src/components/sprite_component.c index 5b6e168..183c793 100644 --- a/src/components/sprite_component.c +++ b/src/components/sprite_component.c @@ -15,7 +15,7 @@ inline int sprite_component_init(component_t *component) if(!component_data->transform_component) { error_printf("Sprite component require a transform component."); - return EXIT_FAILURE; + return FAILURE; } transform_component_data_t *transform_component_data = component_data->transform_component->data; @@ -26,7 +26,7 @@ inline int sprite_component_init(component_t *component) component_data->flip = false; - return EXIT_SUCCESS; + return SUCCESS; } inline int sprite_component_update(component_t *component) @@ -37,7 +37,7 @@ inline int sprite_component_update(component_t *component) frect_to_rect(&transform_component_data->bounds, &component_data->dst_rect); component->entity->draw_priority = transform_component_data->bounds.y + transform_component_data->bounds.h; - return EXIT_SUCCESS; + return SUCCESS; } inline int sprite_component_draw(const component_t *component) @@ -46,7 +46,7 @@ inline int sprite_component_draw(const component_t *component) if(display_draw_texture(component_data->texture, &component_data->src_rect, &component_data->dst_rect, component_data->flip)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } inline int sprite_component_destroy(component_t *component) @@ -55,5 +55,5 @@ inline int sprite_component_destroy(component_t *component) free(component_data); - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/components/transform_component.c b/src/components/transform_component.c index 21cf731..063f6e0 100644 --- a/src/components/transform_component.c +++ b/src/components/transform_component.c @@ -16,12 +16,12 @@ inline int transform_component_init(component_t *component) .h = 32.0f }; - return EXIT_SUCCESS; + return SUCCESS; } inline int transform_component_update(component_t *component __attribute__((unused))) { - return EXIT_SUCCESS; + return SUCCESS; } #if DEBUG >= 2 @@ -31,9 +31,9 @@ inline int transform_component_draw(const component_t *component) { const transform_component_data_t *component_data = component->data; - if(display_draw_frect(&component_data->bounds, C_BLUE, 128)) return_failure_int; + if(display_draw_frect(&component_data->bounds, COLOR_BLUE, 128)) return_failure_int; - return EXIT_SUCCESS; + return SUCCESS; } #endif // DEBUG >= 2 @@ -43,5 +43,5 @@ inline int transform_component_destroy(component_t *component) free(component_data); - return EXIT_SUCCESS; + return SUCCESS; } diff --git a/src/game.c b/src/game.c index 10fd381..56fd7ee 100644 --- a/src/game.c +++ b/src/game.c @@ -129,7 +129,7 @@ int game_init(void) */ game.is_running = true; - return EXIT_SUCCESS; + return SUCCESS; } void game_handle_event(void) @@ -158,7 +158,7 @@ int game_update(void) camera_update(); - return EXIT_SUCCESS; + return SUCCESS; } int game_render(void) @@ -200,7 +200,7 @@ int game_render(void) return_failure_int; } - return EXIT_SUCCESS; + return SUCCESS; } int game_exit(void) @@ -210,5 +210,5 @@ int game_exit(void) if(event_bus_exit()) return_failure_int; display_exit(); - return EXIT_SUCCESS; + return SUCCESS; }