Compare commits
No commits in common. "b2eabff7b13618d6e7872e3e0867bfb7f07bf8a6" and "ad5c3a8d4405e31ed691e3a24877ddefc8aee6fa" have entirely different histories.
b2eabff7b1
...
ad5c3a8d44
|
|
@ -1,8 +1,8 @@
|
|||
[](https://www.gnu.org/licenses/gpl-3.0)
|
||||
# 2D_Engine_C_SDL3_Core
|
||||
# 2D_Engine_C_SDL2_Core
|
||||
|
||||
## Description
|
||||
The core made on SDL3 for the multi-platform 2D_Engine_C.
|
||||
The core made on SDL2 for the multi-platform 2D_Engine_C.
|
||||
|
||||
## Licences
|
||||
- This program is open-source : you can redistribute and/or modify it under the term of the **GNU GPLv3**.
|
||||
|
|
|
|||
|
|
@ -49,8 +49,6 @@ void destroy_asset_header_def(asset_header_def_t *asset_header_def)
|
|||
|
||||
int asset_manager_init(asset_manager_t *asset_manager)
|
||||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
linked_list_init(&asset_manager->assets, sizeof(asset_t), (deleter_t)destroy_asset);
|
||||
linked_list_init(&asset_manager->assets_header_defs, sizeof(asset_header_def_t), (deleter_t)destroy_asset_header_def);
|
||||
|
||||
|
|
@ -99,8 +97,6 @@ int asset_manager_init(asset_manager_t *asset_manager)
|
|||
|
||||
int asset_manager_exit(asset_manager_t *asset_manager)
|
||||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
linked_list_clear(&asset_manager->assets_header_defs);
|
||||
linked_list_clear(&asset_manager->assets);
|
||||
|
||||
|
|
@ -123,8 +119,6 @@ static inline bool condition_is_asset_unused(elem_t *elem, va_list args __attrib
|
|||
|
||||
inline void asset_manager_collect_garbage(asset_manager_t *asset_manager)
|
||||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
linked_list_remove_if(&asset_manager->assets, condition_is_asset_unused);
|
||||
}
|
||||
|
||||
|
|
@ -142,9 +136,9 @@ static inline bool condition_is_asset_header_def_name(elem_t *elem, va_list args
|
|||
return !strcmp(((asset_header_def_t *)elem->data)->name, va_arg(args, const char *));
|
||||
}
|
||||
|
||||
static inline int load_texture(asset_t *asset, asset_header_def_t *asset_header_def, FILE *asset_file)
|
||||
static inline int load_texture(asset_manager_t *asset_manager, asset_t *asset, asset_header_def_t *asset_header_def)
|
||||
{
|
||||
if(fseek(asset_file, asset_header_def->start, SEEK_SET))
|
||||
if(fseek(asset_manager->asset_file, asset_header_def->start, SEEK_SET))
|
||||
{
|
||||
error_printf("Failed to seek in asset file : %d", errno);
|
||||
return EXIT_FAILURE;
|
||||
|
|
@ -152,13 +146,13 @@ static inline int load_texture(asset_t *asset, asset_header_def_t *asset_header_
|
|||
|
||||
size_t width, height;
|
||||
|
||||
if(fread(&width, sizeof(width), 1, asset_file) != 1)
|
||||
if(fread(&width, sizeof(width), 1, asset_manager->asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(fread(&height, sizeof(height), 1, asset_file) != 1)
|
||||
if(fread(&height, sizeof(height), 1, asset_manager->asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
return EXIT_FAILURE;
|
||||
|
|
@ -178,7 +172,7 @@ static inline int load_texture(asset_t *asset, asset_header_def_t *asset_header_
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, asset_file) != width * height)
|
||||
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, asset_manager->asset_file) != width * height)
|
||||
{
|
||||
error_printf("Failed to read asset file.");
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
|
|
@ -207,8 +201,6 @@ static inline int load_texture(asset_t *asset, asset_header_def_t *asset_header_
|
|||
|
||||
int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_name, asset_type_t asset_type)
|
||||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
// Check if asset has alread been loaded.
|
||||
asset_t *asset = linked_list_get_if(&asset_manager->assets, condition_is_asset_name, asset_name);
|
||||
if(!asset)
|
||||
|
|
@ -237,7 +229,7 @@ int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_n
|
|||
switch(asset_type)
|
||||
{
|
||||
case ASSET_TEXTURE:
|
||||
if(load_texture(asset, asset_header_def, asset_manager->asset_file)) return EXIT_FAILURE;
|
||||
if(load_texture(asset_manager, asset, asset_header_def)) return EXIT_FAILURE;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
@ -256,8 +248,6 @@ int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_n
|
|||
|
||||
int asset_manager_let_go_asset(asset_manager_t *asset_manager, const char *asset_name)
|
||||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
asset_t *asset = linked_list_get_if(&asset_manager->assets, condition_is_asset_name, asset_name);
|
||||
if(!asset)
|
||||
{
|
||||
|
|
@ -271,8 +261,6 @@ int asset_manager_let_go_asset(asset_manager_t *asset_manager, const char *asset
|
|||
|
||||
texture_t *asset_manager_get_texture(asset_manager_t *asset_manager, const char *asset_name)
|
||||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
asset_t *asset = linked_list_get_if(&asset_manager->assets, condition_is_asset_name, asset_name);
|
||||
if(!asset)
|
||||
{
|
||||
|
|
|
|||
14
src/ecs.c
14
src/ecs.c
|
|
@ -94,7 +94,7 @@ inline component_t *get_component(entity_t *entity, component_type_t component_t
|
|||
component_t *component = linked_list_get_if(&entity->components, condition_is_component_type, component_type);
|
||||
if(!component)
|
||||
{
|
||||
error_printf("Entity with id:%d doesn't have componant of type : %d", entity->id, component_type);
|
||||
error_printf("Entity doesn't have componant of type : %d", component_type);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -138,8 +138,6 @@ inline void destroy_entity(entity_t *entity)
|
|||
|
||||
inline void entity_manager_init(entity_manager_t *entity_manager)
|
||||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
linked_list_init(&entity_manager->entities, sizeof(entity_t), (deleter_t)destroy_entity);
|
||||
|
||||
debug_printf("Initialized entity manager.");
|
||||
|
|
@ -147,8 +145,6 @@ inline void entity_manager_init(entity_manager_t *entity_manager)
|
|||
|
||||
inline void entity_manager_add_entity(entity_manager_t *entity_manager, entity_t *entity)
|
||||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
linked_list_push_back(&entity_manager->entities, entity);
|
||||
|
||||
debug_printf("Added entity %d to entity manager.", entity->id);
|
||||
|
|
@ -164,8 +160,6 @@ static inline int action_update_entity(elem_t *elem, va_list args __attribute__(
|
|||
|
||||
inline void entity_manager_update(entity_manager_t *entity_manager)
|
||||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
linked_list_for_each(&entity_manager->entities, action_update_entity);
|
||||
}
|
||||
|
||||
|
|
@ -178,8 +172,6 @@ static inline int action_draw_entity(elem_t *elem, va_list args __attribute__((u
|
|||
|
||||
inline int entity_manager_draw(entity_manager_t *entity_manager)
|
||||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
return linked_list_for_each(&entity_manager->entities, action_draw_entity);
|
||||
}
|
||||
|
||||
|
|
@ -192,15 +184,11 @@ static inline bool condition_is_entity_name(elem_t *elem, va_list args)
|
|||
|
||||
inline void entity_manager_remove_entity(entity_manager_t *entity_manager, unsigned int id)
|
||||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
linked_list_remove_if(&entity_manager->entities, condition_is_entity_name, id);
|
||||
}
|
||||
|
||||
inline void entity_manager_exit(entity_manager_t *entity_manager)
|
||||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
linked_list_clear(&entity_manager->entities);
|
||||
|
||||
debug_printf("Exited entity manager.");
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef DEBUG
|
||||
#define DEBUG 0
|
||||
|
|
@ -12,7 +12,8 @@
|
|||
#if DEBUG > 0
|
||||
#define debug_printf(...) \
|
||||
do { \
|
||||
fprintf(stderr, "\033[33mINFO\033[m: " __VA_ARGS__); \
|
||||
fprintf(stderr, "\033[33mINFO\033[m: %s:%d:%s(): ", __FILE__, __LINE__, __func__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fputc('\n', stderr); \
|
||||
} while (0)
|
||||
#else
|
||||
|
|
@ -21,7 +22,8 @@
|
|||
|
||||
#define warning_printf(...) \
|
||||
do { \
|
||||
fprintf(stderr, "\033[35mWARNING\033[m: " __VA_ARGS__); \
|
||||
fprintf(stderr, "\033[35mWARNING\033[m: %s:%d:%s(): ", __FILE__, __LINE__, __func__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fputc('\n', stderr); \
|
||||
} while (0)
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@
|
|||
|
||||
elem_t *elem_create(const linked_list_t *linked_list)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *elem;
|
||||
elem = malloc(sizeof(elem_t));
|
||||
if(!elem)
|
||||
|
|
@ -28,7 +26,7 @@ elem_t *elem_create(const linked_list_t *linked_list)
|
|||
|
||||
inline void destroy_elem(elem_t *elem, const deleter_t elem_deleter)
|
||||
{
|
||||
if(!elem) return; // Not fatal
|
||||
if(!elem) return;
|
||||
|
||||
if(elem_deleter)
|
||||
elem_deleter(elem->data);
|
||||
|
|
@ -40,8 +38,6 @@ inline void destroy_elem(elem_t *elem, const deleter_t elem_deleter)
|
|||
|
||||
void linked_list_init(linked_list_t *linked_list, const size_t data_size, const deleter_t elem_deleter)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
linked_list->first = NULL;
|
||||
linked_list->last = NULL;
|
||||
linked_list->size = 0;
|
||||
|
|
@ -52,9 +48,7 @@ void linked_list_init(linked_list_t *linked_list, const size_t data_size, const
|
|||
|
||||
void linked_list_push_back_elem(linked_list_t *linked_list, elem_t *elem)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem) return; // Not fatal
|
||||
if(!elem) return;
|
||||
|
||||
elem->prev = linked_list->last;
|
||||
elem->next = NULL;
|
||||
|
|
@ -71,9 +65,7 @@ void linked_list_push_back_elem(linked_list_t *linked_list, elem_t *elem)
|
|||
|
||||
void linked_list_push_front_elem(linked_list_t *linked_list, elem_t *elem)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem) return; // Not fatal
|
||||
if(!elem) return;
|
||||
|
||||
elem->next = linked_list->first;
|
||||
elem->prev = NULL;
|
||||
|
|
@ -90,8 +82,6 @@ void linked_list_push_front_elem(linked_list_t *linked_list, elem_t *elem)
|
|||
|
||||
void linked_list_push_back(linked_list_t *linked_list, void *data)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *tmp = malloc(sizeof(elem_t));
|
||||
if(!tmp)
|
||||
{
|
||||
|
|
@ -105,8 +95,6 @@ void linked_list_push_back(linked_list_t *linked_list, void *data)
|
|||
|
||||
void linked_list_push_front(linked_list_t *linked_list, void *data)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *tmp = malloc(sizeof(elem_t));
|
||||
if(!tmp)
|
||||
{
|
||||
|
|
@ -120,8 +108,6 @@ void linked_list_push_front(linked_list_t *linked_list, void *data)
|
|||
|
||||
void linked_list_pop_back(linked_list_t *linked_list)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *tmp = linked_list->last;
|
||||
if(!tmp) return;
|
||||
|
||||
|
|
@ -139,8 +125,6 @@ void linked_list_pop_back(linked_list_t *linked_list)
|
|||
|
||||
void linked_list_pop_front(linked_list_t *linked_list)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *tmp = linked_list->first;
|
||||
if(!tmp) return;
|
||||
|
||||
|
|
@ -158,8 +142,6 @@ void linked_list_pop_front(linked_list_t *linked_list)
|
|||
|
||||
void linked_list_clear(linked_list_t *linked_list)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *actual_elem = linked_list->first;
|
||||
elem_t *tmp;
|
||||
|
||||
|
|
@ -180,22 +162,16 @@ void linked_list_clear(linked_list_t *linked_list)
|
|||
|
||||
inline bool linked_list_is_empty(const linked_list_t *linked_list)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
return !linked_list->first;
|
||||
}
|
||||
|
||||
inline bool linked_list_is_in_bound(const linked_list_t *linked_list, size_t index)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
return index < linked_list->size;
|
||||
}
|
||||
|
||||
elem_t *linked_list_get_elem(const linked_list_t *linked_list, size_t index)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!linked_list_is_in_bound(linked_list, index)) return NULL;
|
||||
|
||||
if(index == 0)
|
||||
|
|
@ -216,8 +192,6 @@ elem_t *linked_list_get_elem(const linked_list_t *linked_list, size_t index)
|
|||
|
||||
void *linked_list_get(const linked_list_t *linked_list, size_t index)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *tmp = linked_list_get_elem(linked_list, index);
|
||||
if(!tmp)
|
||||
{
|
||||
|
|
@ -229,9 +203,7 @@ void *linked_list_get(const linked_list_t *linked_list, size_t index)
|
|||
|
||||
void linked_list_insert_elem(linked_list_t *linked_list, elem_t *elem, size_t index)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem) return; // Not fatal
|
||||
if(!elem) return;
|
||||
if(!linked_list_is_in_bound(linked_list, index)) return;
|
||||
|
||||
if(index == 0)
|
||||
|
|
@ -259,8 +231,6 @@ void linked_list_insert_elem(linked_list_t *linked_list, elem_t *elem, size_t in
|
|||
|
||||
void linked_list_insert(linked_list_t *linked_list, void *data, size_t index)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *tmp = malloc(sizeof(elem_t));
|
||||
if(!tmp)
|
||||
{
|
||||
|
|
@ -274,8 +244,6 @@ void linked_list_insert(linked_list_t *linked_list, void *data, size_t index)
|
|||
|
||||
void linked_list_remove_elem(linked_list_t *linked_list, elem_t *elem)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem) return;
|
||||
|
||||
if(elem == linked_list->first)
|
||||
|
|
@ -302,16 +270,13 @@ void linked_list_remove_elem(linked_list_t *linked_list, elem_t *elem)
|
|||
|
||||
void linked_list_remove(linked_list_t *linked_list, size_t index)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
elem_t *tmp = linked_list_get_elem(linked_list, index);
|
||||
if(tmp) linked_list_remove_elem(linked_list, tmp);
|
||||
}
|
||||
|
||||
elem_t *linked_list_get_elem_if(const linked_list_t *linked_list, const condition_t condition, ...)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
assert("Condition cannot be NULL" && condition);
|
||||
if(!condition) return NULL;
|
||||
|
||||
va_list args;
|
||||
va_start(args, condition);
|
||||
|
|
@ -346,8 +311,7 @@ elem_t *linked_list_get_elem_if(const linked_list_t *linked_list, const conditio
|
|||
|
||||
void *linked_list_get_if(const linked_list_t *linked_list, const condition_t condition, ...)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
assert("Condition cannot be NULL" && condition);
|
||||
if(!condition) return NULL;
|
||||
|
||||
va_list args;
|
||||
va_start(args, condition);
|
||||
|
|
@ -382,8 +346,11 @@ void *linked_list_get_if(const linked_list_t *linked_list, const condition_t con
|
|||
|
||||
void linked_list_remove_if(linked_list_t *linked_list, const condition_t condition, ...)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
assert("Condition cannot be NULL" && condition);
|
||||
if(!condition)
|
||||
{
|
||||
error_printf("Condition cannot be NULL.");
|
||||
return;
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start(args, condition);
|
||||
|
|
@ -413,8 +380,11 @@ void linked_list_remove_if(linked_list_t *linked_list, const condition_t conditi
|
|||
|
||||
int linked_list_for_each(const linked_list_t *linked_list, const action_t action, ...)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
assert("Action cannot be NULL" && action);
|
||||
if(!action)
|
||||
{
|
||||
error_printf("Action cannot be NULL.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start(args, action);
|
||||
|
|
|
|||
Loading…
Reference in New Issue