diff --git a/Conventions.txt b/Conventions.txt index bc550ab..3fefbe4 100644 --- a/Conventions.txt +++ b/Conventions.txt @@ -2,8 +2,21 @@ Trivial Variables: i,n,c,etc... (Only one letter. If one letter isn't clear, the Local Variables: snake_case Global Variables: g_snake_case Const Variables: ALL_CAPS -Pointer Variables: add a p_ to the prefix. For global variables it would be gp_var, for local variables p_var, for const variables p_VAR. If far pointers are used then use an fp_ instead of p_. Typedef Values: suffix _t +Pointer type: do type* var not type *var or type * var. + +Braces for statements: +func(type arg) +{ +... +} + +Braces for struct Variable definition +struct var = { + .var1 = val1, + .var2 = val2 +} +Single line is fine, though not recommended for struct with more than 4 fields. Structs: PascalCase Enums: PascalCase diff --git a/engine/AABB.c b/engine/AABB.c index f713a15..3302efc 100644 --- a/engine/AABB.c +++ b/engine/AABB.c @@ -1,6 +1,6 @@ #include "AABB.h" -bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap) +bool find_1D_overlap(Vector2 l1, Vector2 l2, float* overlap) { // No Overlap if (l1.y < l2.x || l2.y < l1.x) return false; @@ -24,7 +24,7 @@ bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap) return true; } -bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2 * const overlap) +bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2* overlap) { // Note that we include one extra pixel for checking // This avoid overlapping on the border diff --git a/engine/AABB.h b/engine/AABB.h index 085cdf4..1ab47a5 100644 --- a/engine/AABB.h +++ b/engine/AABB.h @@ -2,6 +2,6 @@ #define __AABB_H #include "raylib.h" #include "raymath.h" -bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap); -bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2 * const overlap); +bool find_1D_overlap(Vector2 l1, Vector2 l2, float* overlap); +bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2* overlap); #endif // __AABB_H diff --git a/engine/EC/components.h b/engine/EC/components.h index 5041eeb..998e818 100644 --- a/engine/EC/components.h +++ b/engine/EC/components.h @@ -6,8 +6,7 @@ // TODO: Look at sc to use macros to auto generate functions #define N_COMPONENTS 10 -typedef enum ComponentEnum -{ +typedef enum ComponentEnum { CBBOX_COMP_T, CTRANSFORM_COMP_T, CTILECOORD_COMP_T, @@ -18,66 +17,58 @@ typedef enum ComponentEnum CHITBOXES_T, CHURTBOX_T, CSPRITE_T, -}ComponentEnum_t; +} ComponentEnum_t; -typedef struct _CBBox_t -{ +typedef struct _CBBox_t { Vector2 size; Vector2 offset; Vector2 half_size; bool solid; bool fragile; -}CBBox_t; +} CBBox_t; -typedef struct _CTransform_t -{ +typedef struct _CTransform_t { Vector2 prev_position; Vector2 position; Vector2 velocity; Vector2 accel; Vector2 fric_coeff; -}CTransform_t; +} CTransform_t; -typedef struct _CMovementState_t -{ +typedef struct _CMovementState_t { uint8_t ground_state; uint8_t water_state; -}CMovementState_t; +} CMovementState_t; // This is to store the occupying tiles // Limits to store 4 tiles at a tile, // Thus the size of the entity cannot be larger than the tile -typedef struct _CTileCoord_t -{ +typedef struct _CTileCoord_t { unsigned int tiles[8]; unsigned int n_tiles; -}CTileCoord_t; +} CTileCoord_t; -typedef struct _CJump_t -{ +typedef struct _CJump_t { unsigned int jumps; unsigned int max_jumps; int jump_speed; bool jumped; bool jump_ready; bool short_hop; -}CJump_t; +} CJump_t; -typedef enum PlayerState -{ +typedef enum PlayerState { GROUNDED, AIR, -}PlayerState_t; +} PlayerState_t; -typedef struct _CPlayerState_t -{ +typedef struct _CPlayerState_t { Vector2 player_dir; uint8_t jump_pressed; uint8_t is_crouch; -}CPlayerState_t; +} CPlayerState_t; -typedef enum ContainerItem -{ +typedef enum ContainerItem { CONTAINER_EMPTY, CONTAINER_LEFT_ARROW, CONTAINER_RIGHT_ARROW, @@ -85,37 +76,32 @@ typedef enum ContainerItem CONTAINER_DOWN_ARROW, CONTAINER_COIN, CONTAINER_BOMB, -}ContainerItem_t; +} ContainerItem_t; -typedef enum ContainerMaterial -{ +typedef enum ContainerMaterial { WOODEN_CONTAINER, METAL_CONTAINER, -}ContainerMaterial_t; +} ContainerMaterial_t; -typedef struct _CContainer_t -{ +typedef struct _CContainer_t { ContainerMaterial_t material; ContainerItem_t item; -}CContainer_t; +} CContainer_t; -typedef struct _CHitBoxes_t -{ +typedef struct _CHitBoxes_t { Rectangle boxes[2]; uint8_t n_boxes; bool strong; -}CHitBoxes_t; +} CHitBoxes_t; -typedef struct _CHurtbox_t -{ +typedef struct _CHurtbox_t { Vector2 offset; Vector2 size; bool fragile; -}CHurtbox_t; +} CHurtbox_t; // Credits to bedroomcoders.co.uk for this -typedef struct Sprite -{ +typedef struct Sprite { Texture2D* texture; Vector2 frame_size; Vector2 origin; @@ -124,17 +110,16 @@ typedef struct Sprite int elapsed; int speed; char* name; -}Sprite_t; +} Sprite_t; typedef unsigned int (*sprite_transition_func_t)(Entity_t *ent); // Transition requires knowledge of the entity -typedef struct _CSprite_t -{ +typedef struct _CSprite_t { const char * const *sprites_map; //Array of all sprite names associated Sprite_t* sprite; sprite_transition_func_t transition_func; Vector2 offset; unsigned int current_idx; -}CSprite_t; +} CSprite_t; static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y) { diff --git a/engine/EC/entManager.c b/engine/EC/entManager.c index ea25654..fc2dae1 100644 --- a/engine/EC/entManager.c +++ b/engine/EC/entManager.c @@ -1,21 +1,21 @@ #include "entManager.h" -void init_entity_manager(EntityManager_t *p_manager) +void init_entity_manager(EntityManager_t* p_manager) { sc_map_init_64v(&p_manager->entities, MAX_COMP_POOL_SIZE, 0); - for (size_t i=0; icomponent_map + i, MAX_COMP_POOL_SIZE, 0); } - for (size_t i=0; ientities_map + i, MAX_COMP_POOL_SIZE, 0); } sc_queue_init(&p_manager->to_add); sc_queue_init(&p_manager->to_remove); } -; -void update_entity_manager(EntityManager_t *p_manager) + +void update_entity_manager(EntityManager_t* p_manager) { // This will only update the entity map of the manager // It does not make new entities, but will free entity @@ -46,13 +46,12 @@ void update_entity_manager(EntityManager_t *p_manager) sc_map_put_64v(&p_manager->entities_map[p_entity->m_tag], e_idx, (void *)p_entity); } sc_queue_clear(&p_manager->to_add); - } -void clear_entity_manager(EntityManager_t *p_manager) +void clear_entity_manager(EntityManager_t* p_manager) { unsigned long e_id; - Entity_t *p_ent; + Entity_t* p_ent; sc_map_foreach (&p_manager->entities, e_id, p_ent) { remove_entity(p_manager, e_id); @@ -60,15 +59,15 @@ void clear_entity_manager(EntityManager_t *p_manager) update_entity_manager(p_manager); } -void free_entity_manager(EntityManager_t *p_manager) +void free_entity_manager(EntityManager_t* p_manager) { clear_entity_manager(p_manager); sc_map_term_64v(&p_manager->entities); - for (size_t i=0; icomponent_map + i); } - for (size_t i=0; ientities_map + i); } @@ -76,10 +75,10 @@ void free_entity_manager(EntityManager_t *p_manager) sc_queue_term(&p_manager->to_remove); } -Entity_t *add_entity(EntityManager_t *p_manager, EntityTag_t tag) +Entity_t *add_entity(EntityManager_t* p_manager, EntityTag_t tag) { unsigned long e_idx = 0; - Entity_t * p_ent = new_entity_from_mempool(&e_idx); + Entity_t* p_ent = new_entity_from_mempool(&e_idx); p_ent->m_tag = tag; if (p_ent) { @@ -88,9 +87,9 @@ Entity_t *add_entity(EntityManager_t *p_manager, EntityTag_t tag) return p_ent; } -void remove_entity(EntityManager_t *p_manager, unsigned long id) +void remove_entity(EntityManager_t* p_manager, unsigned long id) { - Entity_t *p_entity = sc_map_get_64v(&p_manager->entities, id); + Entity_t* p_entity = sc_map_get_64v(&p_manager->entities, id); if(!sc_map_found(&p_manager->entities)) return; // This only marks the entity for deletion // Does not free entity. This is done during the update @@ -101,20 +100,20 @@ void remove_entity(EntityManager_t *p_manager, unsigned long id) } } -Entity_t *get_entity(EntityManager_t *p_manager, unsigned long id) +Entity_t* get_entity(EntityManager_t* p_manager, unsigned long id) { - Entity_t *p_entity = sc_map_get_64v(&p_manager->entities, id); + Entity_t* p_entity = sc_map_get_64v(&p_manager->entities, id); if(!sc_map_found(&p_manager->entities)) return NULL; return p_entity; } // Components are not expected to be removed // So, no need to extra steps to deal with iterator invalidation -void *add_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type) +void* add_component(EntityManager_t* p_manager, Entity_t* p_entity, ComponentEnum_t comp_type) { unsigned long comp_type_idx = (unsigned long)comp_type; unsigned long comp_idx = 0; - void * p_comp = new_component_from_mempool(comp_type, &comp_idx); + void* p_comp = new_component_from_mempool(comp_type, &comp_idx); if (p_comp) { sc_map_put_64(&p_entity->components, comp_type_idx, comp_idx); @@ -123,7 +122,7 @@ void *add_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnu return p_comp; } -void *get_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type) +void* get_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type) { unsigned long comp_type_idx = (unsigned long)comp_type; void * p_comp = sc_map_get_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id); diff --git a/engine/EC/entManager.h b/engine/EC/entManager.h index 92c3604..c3bdf87 100644 --- a/engine/EC/entManager.h +++ b/engine/EC/entManager.h @@ -4,27 +4,26 @@ #include "sc/map/sc_map.h" #include "mempool.h" // includes entity and components -typedef struct EntityManager -{ +typedef struct EntityManager { // All fields are Read-Only struct sc_map_64v entities; // ent id : entity struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}] struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...] struct sc_queue_uint to_add; struct sc_queue_uint to_remove; -}EntityManager_t; +} EntityManager_t; -void init_entity_manager(EntityManager_t *p_manager); -void update_entity_manager(EntityManager_t *p_manager); -void clear_entity_manager(EntityManager_t *p_manager); -void free_entity_manager(EntityManager_t *p_manager); +void init_entity_manager(EntityManager_t* p_manager); +void update_entity_manager(EntityManager_t* p_manager); +void clear_entity_manager(EntityManager_t* p_manager); +void free_entity_manager(EntityManager_t* p_manager); -Entity_t *add_entity(EntityManager_t *p_manager, EntityTag_t tag); -void remove_entity(EntityManager_t *p_manager, unsigned long id); -Entity_t *get_entity(EntityManager_t *p_manager, unsigned long id); +Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag); +void remove_entity(EntityManager_t* p_manager, unsigned long id); +Entity_t *get_entity(EntityManager_t* p_manager, unsigned long id); -void *add_component(EntityManager_t *p_manager, Entity_t *entity, ComponentEnum_t comp_type); -void *get_component(EntityManager_t *p_manager, Entity_t *entity, ComponentEnum_t comp_type); -void remove_component(EntityManager_t *p_manager, Entity_t *entity, ComponentEnum_t comp_type); +void* add_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type); +void* get_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type); +void remove_component(EntityManager_t* p_manager, Entity_t* entity, ComponentEnum_t comp_type); #endif // __ENTITY_MANAGER_H diff --git a/engine/EC/entity.h b/engine/EC/entity.h index c1ec0cb..2d94629 100644 --- a/engine/EC/entity.h +++ b/engine/EC/entity.h @@ -4,19 +4,17 @@ #include "sc/map/sc_map.h" #define N_TAGS 4 -enum EntityTag -{ +typedef enum EntityTag { NO_ENT_TAG, PLAYER_ENT_TAG, ENEMY_ENT_TAG, CRATES_ENT_TAG, -}; -typedef enum EntityTag EntityTag_t; -typedef struct Entity -{ +} EntityTag_t; + +typedef struct Entity { unsigned long m_id; EntityTag_t m_tag; bool m_alive; struct sc_map_64 components; -}Entity_t; +} Entity_t; #endif // __ENTITY_H diff --git a/engine/EC/mempool.c b/engine/EC/mempool.c index 87d8a35..e77c731 100644 --- a/engine/EC/mempool.c +++ b/engine/EC/mempool.c @@ -16,18 +16,16 @@ static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE]; static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE]; static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE]; -typedef struct MemPool -{ +typedef struct MemPool { void * const buffer; const unsigned long max_size; const unsigned long elem_size; bool *use_list; struct sc_queue_uint free_list; -}MemPool_t; +} MemPool_t; // Static allocate mempools -static MemPool_t comp_mempools[N_COMPONENTS] = -{ +static MemPool_t comp_mempools[N_COMPONENTS] = { {bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), NULL, {0}}, {ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), NULL, {0}}, {ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), NULL, {0}}, @@ -39,20 +37,26 @@ static MemPool_t comp_mempools[N_COMPONENTS] = {churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}}, {csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}}, }; -static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), NULL, {0}}; +static MemPool_t ent_mempool = { + .buffer = entity_buffer, + .max_size = MAX_COMP_POOL_SIZE, + .elem_size = sizeof(Entity_t), + .use_list = NULL, + .free_list = {0} +}; static bool pool_inited = false; void init_memory_pools(void) { if (!pool_inited) { - for (size_t i=0; icomponents); ent->m_alive = true; ent->m_tag = NO_ENT_TAG; return ent; } -Entity_t * get_entity_wtih_id(unsigned long idx) +Entity_t* get_entity_wtih_id(unsigned long idx) { if (!ent_mempool.use_list[idx]) return NULL; return entity_buffer + idx; @@ -119,9 +123,9 @@ void free_entity_to_mempool(unsigned long idx) } } -void* new_component_from_mempool(ComponentEnum_t comp_type, unsigned long *idx) +void* new_component_from_mempool(ComponentEnum_t comp_type, unsigned long* idx) { - void * comp = NULL; + void* comp = NULL; assert(comp_type < N_COMPONENTS); if(!sc_queue_empty(&comp_mempools[comp_type].free_list)) { @@ -157,9 +161,11 @@ void free_component_to_mempool(ComponentEnum_t comp_type, unsigned long idx) void print_mempool_stats(char* buffer) { - int written = 0; - for (size_t i=0; im_fonts, MAX_FONTS, 0); sc_map_init_s64(&assets->m_sprites, MAX_SPRITES, 0); @@ -63,7 +63,7 @@ void init_assets(Assets_t *assets) sc_map_init_s64(&assets->m_sounds, MAX_SOUNDS, 0); } -void free_all_assets(Assets_t *assets) +void free_all_assets(Assets_t* assets) { sc_map_clear_s64(&assets->m_textures); sc_map_clear_s64(&assets->m_fonts); @@ -72,7 +72,7 @@ void free_all_assets(Assets_t *assets) memset(free_idx, 0, sizeof(free_idx)); } -void term_assets(Assets_t *assets) +void term_assets(Assets_t* assets) { free_all_assets(assets); sc_map_term_s64(&assets->m_textures); @@ -81,7 +81,7 @@ void term_assets(Assets_t *assets) sc_map_term_s64(&assets->m_sprites); } -Texture2D* get_texture(Assets_t *assets, const char *name) +Texture2D* get_texture(Assets_t* assets, const char* name) { uint8_t tex_idx = sc_map_get_s64(&assets->m_textures, name); if (sc_map_found(&assets->m_textures)) @@ -91,7 +91,7 @@ Texture2D* get_texture(Assets_t *assets, const char *name) return NULL; } -Sprite_t* get_sprite(Assets_t *assets, const char *name) +Sprite_t* get_sprite(Assets_t* assets, const char* name) { uint8_t spr_idx = sc_map_get_s64(&assets->m_sprites, name); if (sc_map_found(&assets->m_sprites)) @@ -101,7 +101,7 @@ Sprite_t* get_sprite(Assets_t *assets, const char *name) return NULL; } -Sound* get_sound(Assets_t *assets, const char *name) +Sound* get_sound(Assets_t* assets, const char* name) { uint8_t snd_idx = sc_map_get_s64(&assets->m_sounds, name); if (sc_map_found(&assets->m_sounds)) @@ -111,7 +111,7 @@ Sound* get_sound(Assets_t *assets, const char *name) return NULL; } -Font* get_font(Assets_t *assets, const char *name) +Font* get_font(Assets_t* assets, const char* name) { uint8_t fnt_idx = sc_map_get_s64(&assets->m_fonts, name); if (sc_map_found(&assets->m_fonts)) @@ -121,7 +121,7 @@ Font* get_font(Assets_t *assets, const char *name) return NULL; } -void draw_sprite(Sprite_t *spr, Vector2 pos) +void draw_sprite(Sprite_t* spr, Vector2 pos) { Rectangle rec = { spr->origin.x + spr->frame_size.x * spr->current_frame, diff --git a/engine/assets.h b/engine/assets.h index f016e87..cab9919 100644 --- a/engine/assets.h +++ b/engine/assets.h @@ -13,20 +13,20 @@ typedef struct Assets }Assets_t; -void init_assets(Assets_t *assets); -void free_all_assets(Assets_t *assets); -void term_assets(Assets_t *assets); +void init_assets(Assets_t* assets); +void free_all_assets(Assets_t* assets); +void term_assets(Assets_t* assets); -Texture2D* add_texture(Assets_t *assets, char *name, char *path); -Sprite_t* add_sprite(Assets_t *assets, char *name, Texture2D* texture); -Sound* add_sound(Assets_t *assets, char *name, char *path); -Font* add_font(Assets_t *assets, char *name, char *path); +Texture2D* add_texture(Assets_t* assets, const char* name, const char* path); +Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture); +Sound* add_sound(Assets_t * assets, const char* name, const char* path); +Font* add_font(Assets_t* assets, const char* name, const char* path); -Texture2D* get_texture(Assets_t *assets, const char *name); -Sprite_t* get_sprite(Assets_t *assets, const char *name); -Sound* get_sound(Assets_t *assets, const char *name); -Font* get_font(Assets_t *assets, const char *name); +Texture2D* get_texture(Assets_t* assets, const char* name); +Sprite_t* get_sprite(Assets_t* assets, const char* name); +Sound* get_sound(Assets_t* assets, const char* name); +Font* get_font(Assets_t* assets, const char* name); -void draw_sprite(Sprite_t *spr, Vector2 pos); +void draw_sprite(Sprite_t* spr, Vector2 pos); #endif // __ASSETS_H diff --git a/engine/assets_maps.c b/engine/assets_maps.c index ecd5099..459d0fa 100644 --- a/engine/assets_maps.c +++ b/engine/assets_maps.c @@ -1,8 +1,7 @@ #include "assets_maps.h" -const char * const player_sprite_map[N_PLAYER_SPRITES] = -{ +const char* const player_sprite_map[N_PLAYER_SPRITES] = { "plr_stand", "plr_run", }; diff --git a/engine/assets_maps.h b/engine/assets_maps.h index 7accbbd..59fa3ff 100644 --- a/engine/assets_maps.h +++ b/engine/assets_maps.h @@ -8,6 +8,6 @@ enum PlayerSpriteEnum SPR_PLAYER_RUN }; -extern const char * const player_sprite_map[N_PLAYER_SPRITES]; +extern const char* const player_sprite_map[N_PLAYER_SPRITES]; #endif // __ASSETS_MAPS_H diff --git a/engine/editor_scene.c b/engine/editor_scene.c index be44a0c..5b9b673 100644 --- a/engine/editor_scene.c +++ b/engine/editor_scene.c @@ -9,8 +9,7 @@ #define MAX_N_TILES 4096 static Tile_t all_tiles[MAX_N_TILES] = {0}; -enum EntitySpawnSelection -{ +enum EntitySpawnSelection { TOGGLE_TILE = 0, SPAWN_CRATE, SPAWN_METAL_CRATE, @@ -27,12 +26,12 @@ static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width static void level_scene_render_func(Scene_t* scene) { - LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + LevelSceneData_t* data = (LevelSceneData_t *)scene->scene_data; TileGrid_t tilemap = data->tilemap; - Entity_t *p_ent; + Entity_t* p_ent; - for (size_t i=0; ient_manager, p_ent, CHITBOXES_T); if (p_hitbox != NULL) { - for (uint8_t i=0;in_boxes;++i) + for (uint8_t i = 0;i < p_hitbox->n_boxes; ++i) { Rectangle rec = { .x = p_ct->position.x + p_hitbox->boxes[i].x, @@ -101,7 +100,7 @@ static void level_scene_render_func(Scene_t* scene) } } - for (size_t i=0; iscene_data; - Entity_t *p_crate = add_entity(&scene->ent_manager, CRATES_ENT_TAG); - CBBox_t *p_bbox = add_component(&scene->ent_manager, p_crate, CBBOX_COMP_T); + LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data; + Entity_t* p_crate = add_entity(&scene->ent_manager, CRATES_ENT_TAG); + CBBox_t* p_bbox = add_component(&scene->ent_manager, p_crate, CBBOX_COMP_T); + set_bbox(p_bbox, TILE_SIZE, TILE_SIZE); p_bbox->solid = true; p_bbox->fragile = !metal; - CTransform_t *p_ctransform = add_component(&scene->ent_manager, p_crate, CTRANSFORM_COMP_T); + + CTransform_t* p_ctransform = add_component(&scene->ent_manager, p_crate, CTRANSFORM_COMP_T); p_ctransform->position.x = (tile_idx % data->tilemap.width) * TILE_SIZE; p_ctransform->position.y = (tile_idx / data->tilemap.width) * TILE_SIZE; add_component(&scene->ent_manager, p_crate, CMOVEMENTSTATE_T); @@ -180,14 +181,15 @@ static void spawn_crate(Scene_t *scene, unsigned int tile_idx, bool metal) p_hurtbox->fragile = !metal; } -static void spawn_player(Scene_t *scene) +static void spawn_player(Scene_t* scene) { - Entity_t *p_ent = add_entity(&scene->ent_manager, PLAYER_ENT_TAG); + Entity_t* p_ent = add_entity(&scene->ent_manager, PLAYER_ENT_TAG); + CBBox_t* p_bbox = add_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); - CBBox_t *p_bbox = add_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); set_bbox(p_bbox, PLAYER_WIDTH, PLAYER_HEIGHT); add_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); - CJump_t *p_cjump = add_component(&scene->ent_manager, p_ent, CJUMP_COMP_T); + + CJump_t* p_cjump = add_component(&scene->ent_manager, p_ent, CJUMP_COMP_T); p_cjump->jump_speed = 680; p_cjump->jumps = 1; p_cjump->max_jumps = 1; @@ -197,15 +199,13 @@ static void spawn_player(Scene_t *scene) add_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T); CHitBoxes_t* p_hitbox = add_component(&scene->ent_manager, p_ent, CHITBOXES_T); p_hitbox->n_boxes = 2; - p_hitbox->boxes[0] = (Rectangle) - { + p_hitbox->boxes[0] = (Rectangle) { .x = 0, .y = -1, .width = p_bbox->size.x - 1, .height = p_bbox->size.y + 2, }; - p_hitbox->boxes[1] = (Rectangle) - { + p_hitbox->boxes[1] = (Rectangle) { .x = -1, .y = 0, .width = p_bbox->size.x + 2, @@ -217,7 +217,7 @@ static void spawn_player(Scene_t *scene) p_cspr->transition_func = &player_sprite_transition_func; } -static void toggle_block_system(Scene_t *scene) +static void toggle_block_system(Scene_t* scene) { // TODO: This system is not good as the interface between raw input and actions is broken static unsigned int last_tile_idx = MAX_N_TILES; @@ -269,9 +269,9 @@ static void toggle_block_system(Scene_t *scene) } } -void level_do_action(Scene_t *scene, ActionType_t action, bool pressed) +void level_do_action(Scene_t* scene, ActionType_t action, bool pressed) { - CPlayerState_t *p_playerstate; + CPlayerState_t* p_playerstate; sc_map_foreach_value(&scene->ent_manager.component_map[CPLAYERSTATE_T], p_playerstate) { switch(action) @@ -321,7 +321,7 @@ void level_do_action(Scene_t *scene, ActionType_t action, bool pressed) } } -void init_level_scene(LevelScene_t *scene) +void init_level_scene(LevelScene_t* scene) { init_scene(&scene->scene, LEVEL_SCENE, &level_scene_render_func, &level_do_action); scene->scene.scene_data = &scene->data; @@ -359,12 +359,12 @@ void init_level_scene(LevelScene_t *scene) scene->data.tilemap.n_tiles = scene->data.tilemap.width * scene->data.tilemap.height; assert(scene->data.tilemap.n_tiles <= MAX_N_TILES); scene->data.tilemap.tiles = all_tiles; - for (size_t i=0; idata.tilemap.width; ++i) + for (size_t i = 0; i < scene->data.tilemap.width; ++i) { unsigned int tile_idx = (scene->data.tilemap.height - 1) * scene->data.tilemap.width + i; all_tiles[tile_idx].solid = true; // for testing @@ -374,10 +374,10 @@ void init_level_scene(LevelScene_t *scene) update_entity_manager(&scene->scene.ent_manager); } -void free_level_scene(LevelScene_t *scene) +void free_level_scene(LevelScene_t* scene) { free_scene(&scene->scene); - for (size_t i=0; idata); } -void reload_level_scene(LevelScene_t *scene) +void reload_level_scene(LevelScene_t* scene) { } diff --git a/engine/engine.c b/engine/engine.c index 1f09c43..d7e859a 100644 --- a/engine/engine.c +++ b/engine/engine.c @@ -1,12 +1,13 @@ #include "engine.h" -void change_scene(GameEngine_t *engine, unsigned int idx) +void change_scene(GameEngine_t* engine, unsigned int idx) { engine->scenes[engine->curr_scene]->state = SCENE_ENDED; engine->curr_scene = idx; engine->scenes[engine->curr_scene]->state = SCENE_PLAYING; } -void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func) + +void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func) { sc_map_init_64(&scene->action_map, 32, 0); sc_array_init(&scene->systems); @@ -18,14 +19,14 @@ void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_fun scene->state = SCENE_ENDED; } -void free_scene(Scene_t *scene) +void free_scene(Scene_t* scene) { sc_map_term_64(&scene->action_map); sc_array_term(&scene->systems); free_entity_manager(&scene->ent_manager); } -inline void update_scene(Scene_t *scene) +inline void update_scene(Scene_t* scene) { system_func_t sys; sc_array_foreach(&scene->systems, sys) @@ -34,12 +35,12 @@ inline void update_scene(Scene_t *scene) } } -inline void render_scene(Scene_t *scene) +inline void render_scene(Scene_t* scene) { scene->render_function(scene); } -inline void do_action(Scene_t *scene, ActionType_t action, bool pressed) +inline void do_action(Scene_t* scene, ActionType_t action, bool pressed) { scene->action_function(scene, action, pressed); } diff --git a/engine/engine.h b/engine/engine.h index 054fd13..b1104da 100644 --- a/engine/engine.h +++ b/engine/engine.h @@ -7,34 +7,30 @@ typedef struct Scene Scene_t; -typedef struct GameEngine -{ +typedef struct GameEngine { Scene_t **scenes; unsigned int max_scenes; unsigned int curr_scene; Assets_t assets; -}GameEngine_t; -void change_scene(GameEngine_t *engine, unsigned int idx); +} GameEngine_t; +void change_scene(GameEngine_t* engine, unsigned int idx); -typedef enum SceneType -{ +typedef enum SceneType { LEVEL_SCENE = 0, MENU_SCENE, }SceneType_t; -typedef enum SceneState -{ +typedef enum SceneState { SCENE_PLAYING = 0, SCENE_SUSPENDED, SCENE_ENDED, }SceneState_t; -typedef void(*system_func_t)(Scene_t *); -typedef void(*action_func_t)(Scene_t *, ActionType_t, bool); +typedef void(*system_func_t)(Scene_t*); +typedef void(*action_func_t)(Scene_t*, ActionType_t, bool); sc_array_def(system_func_t, systems); -struct Scene -{ +struct Scene { struct sc_map_64 action_map; // key -> actions struct sc_array_systems systems; system_func_t render_function; @@ -47,11 +43,11 @@ struct Scene }; // Inline functions, for convenience -extern void update_scene(Scene_t *scene); -extern void render_scene(Scene_t *scene); -extern void do_action(Scene_t *scene, ActionType_t action, bool pressed); +extern void update_scene(Scene_t* scene); +extern void render_scene(Scene_t* scene); +extern void do_action(Scene_t* scene, ActionType_t action, bool pressed); -void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func); -void free_scene(Scene_t *scene); +void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func); +void free_scene(Scene_t* scene); #endif // __ENGINE_H diff --git a/engine/game_systems.c b/engine/game_systems.c index e7ab286..adec22f 100644 --- a/engine/game_systems.c +++ b/engine/game_systems.c @@ -7,8 +7,7 @@ static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE}; static const Vector2 GRAVITY = {0, GRAV_ACCEL}; static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 1.1}; -typedef enum AnchorPoint -{ +typedef enum AnchorPoint { AP_TOP_LEFT, AP_TOP_CENTER, AP_TOP_RIGHT, @@ -18,7 +17,7 @@ typedef enum AnchorPoint AP_BOT_LEFT, AP_BOT_CENTER, AP_BOT_RIGHT, -}AnchorPoint_t; +} AnchorPoint_t; static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width) { @@ -27,23 +26,21 @@ static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width return tile_y * tilemap_width + tile_x; } -typedef struct TileArea -{ +typedef struct TileArea { unsigned int tile_x1; unsigned int tile_y1; unsigned int tile_x2; unsigned int tile_y2; -}TileArea_t; +} TileArea_t; -typedef struct CollideEntity -{ +typedef struct CollideEntity { unsigned int idx; Rectangle bbox; TileArea_t area; -}CollideEntity_t; +} CollideEntity_t; // ------------------------- Collision functions ------------------------------------ -static bool check_collision(const CollideEntity_t *ent, TileGrid_t* grid, EntityManager_t* p_manager) +static bool check_collision(const CollideEntity_t* ent, TileGrid_t* grid, EntityManager_t* p_manager) { for(unsigned int tile_y = ent->area.tile_y1; tile_y <= ent->area.tile_y2; tile_y++) { @@ -63,7 +60,13 @@ static bool check_collision(const CollideEntity_t *ent, TileGrid_t* grid, Entity //if (p_bbox->solid && !p_bbox->fragile) if (p_bbox->solid) { - if (find_AABB_overlap((Vector2){ent->bbox.x, ent->bbox.y}, (Vector2){ent->bbox.width, ent->bbox.height}, p_ctransform->position, p_bbox->size, &overlap)) + if ( + find_AABB_overlap( + (Vector2){ent->bbox.x, ent->bbox.y}, + (Vector2){ent->bbox.width, ent->bbox.height}, + p_ctransform->position, p_bbox->size, &overlap + ) + ) { return true; } @@ -72,14 +75,15 @@ static bool check_collision(const CollideEntity_t *ent, TileGrid_t* grid, Entity } } return false; - } -static bool check_collision_at(unsigned int ent_idx, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 point, EntityManager_t* p_manager) +static bool check_collision_at( + unsigned int ent_idx, Vector2 pos, Vector2 bbox_sz, + TileGrid_t* grid, Vector2 point, EntityManager_t* p_manager +) { Vector2 new_pos = Vector2Add(pos, point); - CollideEntity_t ent = - { + CollideEntity_t ent = { .idx = ent_idx, .bbox = (Rectangle){new_pos.x, new_pos.y, bbox_sz.x, bbox_sz.y}, .area = (TileArea_t){ @@ -93,13 +97,17 @@ static bool check_collision_at(unsigned int ent_idx, Vector2 pos, Vector2 bbox_s return check_collision(&ent, grid, p_manager); } -static inline bool check_on_ground(unsigned int ent_idx, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, EntityManager_t* p_manager) +static inline bool check_on_ground( + unsigned int ent_idx, Vector2 pos, Vector2 bbox_sz, + TileGrid_t* grid, EntityManager_t* p_manager +) { return check_collision_at(ent_idx, pos, bbox_sz, grid, (Vector2){0, 1}, p_manager); } -static bool check_collision_and_move(EntityManager_t* p_manager, TileGrid_t* tilemap, - unsigned int ent_idx, CTransform_t *const p_ct, Vector2 sz, +static bool check_collision_and_move( + EntityManager_t* p_manager, TileGrid_t* tilemap, + unsigned int ent_idx, CTransform_t* p_ct, Vector2 sz, Vector2 other_pos, Vector2 other_sz, bool other_solid, uint32_t* collision_value ) @@ -260,7 +268,7 @@ static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor) void player_movement_input_system(Scene_t* scene) { - LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data; TileGrid_t tilemap = data->tilemap; // Deal with player acceleration/velocity via inputs first @@ -268,9 +276,7 @@ void player_movement_input_system(Scene_t* scene) unsigned int ent_idx; sc_map_foreach(&scene->ent_manager.component_map[CPLAYERSTATE_T], ent_idx, p_pstate) { - float mag = Vector2Length(p_pstate->player_dir); - mag = (mag == 0)? 1 : mag; - Entity_t * p_player = get_entity(&scene->ent_manager, ent_idx); + Entity_t* p_player = get_entity(&scene->ent_manager, ent_idx); CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T); CBBox_t* p_bbox = get_component(&scene->ent_manager, p_player, CBBOX_COMP_T); CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T); @@ -308,44 +314,40 @@ void player_movement_input_system(Scene_t* scene) } } - //else + bool hit = false; + unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE; + unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x - 1) / TILE_SIZE; + unsigned int tile_y = (p_ctransform->position.y) / TILE_SIZE; + if (p_bbox->size.y < TILE_SIZE && tile_y > 0) tile_y--; // hack to detect small bbox state + + for(unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) { - bool hit = false; - unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE; - unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x - 1) / TILE_SIZE; - unsigned int tile_y = (p_ctransform->position.y) / TILE_SIZE; - if (p_bbox->size.y < TILE_SIZE && tile_y > 0) tile_y--; // hack to detect small bbox state + hit |= tilemap.tiles[tile_y * tilemap.width + tile_x].solid; + } + if (hit) + { + p_pstate->is_crouch |= 0b10; + } + if (!(p_mstate->ground_state & 1)) p_pstate->is_crouch &= 0b01; + p_pstate->is_crouch >>= 1; - for(unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) + // Jumps is possible as long as you have a jump + + // Check if possible to jump when jump is pressed + if (p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready) + { + p_cjump->jumps--; + if (!in_water) { - hit |= tilemap.tiles[tile_y * tilemap.width + tile_x].solid; + p_ctransform->velocity.y = -p_cjump->jump_speed; } - if (hit) + else { - p_pstate->is_crouch |= 0b10; - } - if (!(p_mstate->ground_state & 1)) p_pstate->is_crouch &= 0b01; - p_pstate->is_crouch >>= 1; - - // Jumps is possible as long as you have a jump - - // Check if possible to jump when jump is pressed - if (p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready) - { - p_cjump->jumps--; - if (!in_water) - { - p_ctransform->velocity.y = -p_cjump->jump_speed; - } - else - { - p_ctransform->velocity.y = -p_cjump->jump_speed / 1.75; - } - - p_cjump->jumped = true; - p_cjump->jump_ready = false; + p_ctransform->velocity.y = -p_cjump->jump_speed / 1.75; } + p_cjump->jumped = true; + p_cjump->jump_ready = false; } p_pstate->player_dir.x = 0; @@ -353,12 +355,12 @@ void player_movement_input_system(Scene_t* scene) } } -void player_bbox_update_system(Scene_t *scene) +void player_bbox_update_system(Scene_t* scene) { - LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data; TileGrid_t tilemap = data->tilemap; - Entity_t *p_player; + Entity_t* p_player; sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player) { CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T); @@ -399,7 +401,12 @@ void player_bbox_update_system(Scene_t *scene) } } - if (!check_collision_at(p_player->m_id, p_ctransform->position, new_bbox, &tilemap, offset, &scene->ent_manager)) + if ( + !check_collision_at( + p_player->m_id, p_ctransform->position, new_bbox, + &tilemap, offset, &scene->ent_manager + ) + ) { set_bbox(p_bbox, new_bbox.x, new_bbox.y); p_ctransform->position = Vector2Add(p_ctransform->position, offset); @@ -412,11 +419,11 @@ void player_bbox_update_system(Scene_t *scene) } } -void tile_collision_system(Scene_t *scene) +void tile_collision_system(Scene_t* scene) { static bool checked_entities[MAX_COMP_POOL_SIZE] = {0}; - LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data; TileGrid_t tilemap = data->tilemap; @@ -425,7 +432,7 @@ void tile_collision_system(Scene_t *scene) //sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player) sc_map_foreach(&scene->ent_manager.component_map[CBBOX_COMP_T], ent_idx, p_bbox) { - Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); + Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); // Get the occupied tiles // For each tile, loop through the entities, check collision and move @@ -436,18 +443,22 @@ void tile_collision_system(Scene_t *scene) unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE; uint32_t collision_value; - for (unsigned int tile_y=tile_y1; tile_y <= tile_y2; tile_y++) + for (unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++) { - for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++) + for (unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) { unsigned int tile_idx = tile_y * tilemap.width + tile_x; - Vector2 other; if(tilemap.tiles[tile_idx].solid) { + Vector2 other; other.x = (tile_idx % tilemap.width) * TILE_SIZE; other.y = (tile_idx / tilemap.width) * TILE_SIZE; // Precision loss is intentional - check_collision_and_move(&scene->ent_manager, &tilemap, ent_idx, p_ctransform, p_bbox->size, other, TILE_SZ, true, &collision_value); + check_collision_and_move( + &scene->ent_manager, &tilemap, ent_idx, + p_ctransform, p_bbox->size, other, + TILE_SZ, true, &collision_value + ); } else @@ -467,7 +478,13 @@ void tile_collision_system(Scene_t *scene) if (p_other_bbox == NULL) continue; CTransform_t *p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T); - if (check_collision_and_move(&scene->ent_manager, &tilemap, ent_idx, p_ctransform, p_bbox->size, p_other_ct->position, p_other_bbox->size, p_other_bbox->solid, &collision_value)) + if ( + check_collision_and_move( + &scene->ent_manager, &tilemap, ent_idx, + p_ctransform, p_bbox->size, p_other_ct->position, + p_other_bbox->size, p_other_bbox->solid, &collision_value + ) + ) { uint32_t collision_key = ((ent_idx << 16) | other_ent_idx); sc_map_put_32(&data->collision_events, collision_key, collision_value); @@ -478,7 +495,10 @@ void tile_collision_system(Scene_t *scene) } // Post movement edge check to zero out velocity - uint8_t edges = check_bbox_edges(&scene->ent_manager, &data->tilemap, ent_idx, p_ctransform->position, p_bbox->size); + uint8_t edges = check_bbox_edges( + &scene->ent_manager, &data->tilemap, ent_idx, + p_ctransform->position, p_bbox->size + ); if (edges & (1<<3)) { if (p_ctransform->velocity.x < 0) p_ctransform->velocity.x = 0; @@ -513,7 +533,14 @@ void tile_collision_system(Scene_t *scene) if(p_ctransform->position.x < 0 || p_ctransform->position.x + p_bbox->size.x > level_width) { p_ctransform->position.x = (p_ctransform->position.x < 0) ? 0 : p_ctransform->position.x; - p_ctransform->position.x = (p_ctransform->position.x + p_bbox->size.x > level_width) ? level_width - p_bbox->size.x : p_ctransform->position.x; + if (p_ctransform->position.x + p_bbox->size.x > level_width) + { + p_ctransform->position.x = level_width - p_bbox->size.x; + } + else + { + p_ctransform->position.x = p_ctransform->position.x; + } p_ctransform->velocity.x *= -1; } @@ -521,7 +548,14 @@ void tile_collision_system(Scene_t *scene) if(p_ctransform->position.y < 0 || p_ctransform->position.y + p_bbox->size.y > level_height) { p_ctransform->position.y = (p_ctransform->position.y < 0) ? 0 : p_ctransform->position.y; - p_ctransform->position.y = (p_ctransform->position.y + p_bbox->size.y > level_height) ? level_height - p_bbox->size.y : p_ctransform->position.y; + if (p_ctransform->position.y + p_bbox->size.y > level_height) + { + p_ctransform->position.y = level_height - p_bbox->size.y; + } + else + { + p_ctransform->position.y = p_ctransform->position.y; + } p_ctransform->velocity.y *= -1; } // Deal with float precision, by rounding when it is near to an integer enough by 2 dp @@ -550,13 +584,13 @@ void tile_collision_system(Scene_t *scene) } } -void friction_coefficient_update_system(Scene_t *scene) +void friction_coefficient_update_system(Scene_t* scene) { CTransform_t* p_ct; unsigned long ent_idx; sc_map_foreach(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], ent_idx, p_ct) { - Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); + Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T); @@ -584,16 +618,17 @@ void friction_coefficient_update_system(Scene_t *scene) } } -void global_external_forces_system(Scene_t *scene) +void global_external_forces_system(Scene_t* scene) { - LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data; CMovementState_t* p_mstate; unsigned long ent_idx; sc_map_foreach(&scene->ent_manager.component_map[CMOVEMENTSTATE_T], ent_idx, p_mstate) { - Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); - CTransform_t * p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); - CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); + Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); + CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); + CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); + if (!(p_mstate->ground_state & 1)) { // Only apply upthrust if center is in water @@ -622,7 +657,10 @@ void global_external_forces_system(Scene_t *scene) // Zero out acceleration for contacts with sturdy entites and tiles - uint8_t edges = check_bbox_edges(&scene->ent_manager, &data->tilemap, ent_idx, p_ctransform->position, p_bbox->size); + uint8_t edges = check_bbox_edges( + &scene->ent_manager, &data->tilemap, ent_idx, + p_ctransform->position, p_bbox->size + ); if (edges & (1<<3)) { if (p_ctransform->accel.x < 0) p_ctransform->accel.x = 0; @@ -657,7 +695,10 @@ void movement_update_system(Scene_t* scene) ); float mag = Vector2Length(p_ctransform->velocity); - p_ctransform->velocity = Vector2Scale(Vector2Normalize(p_ctransform->velocity), (mag > PLAYER_MAX_SPEED)? PLAYER_MAX_SPEED:mag); + p_ctransform->velocity = Vector2Scale( + Vector2Normalize(p_ctransform->velocity), + (mag > PLAYER_MAX_SPEED)? PLAYER_MAX_SPEED:mag + ); // 3 dp precision if (fabs(p_ctransform->velocity.x) < 1e-3) p_ctransform->velocity.x = 0; @@ -677,7 +718,7 @@ void movement_update_system(Scene_t* scene) void player_ground_air_transition_system(Scene_t* scene) { - Entity_t *p_player; + Entity_t* p_player; sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player) { CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T); @@ -706,21 +747,24 @@ void player_ground_air_transition_system(Scene_t* scene) } } -void state_transition_update_system(Scene_t *scene) +void state_transition_update_system(Scene_t* scene) { - LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data; //Entity_t* p_ent; CMovementState_t* p_mstate; unsigned long ent_idx; sc_map_foreach(&scene->ent_manager.component_map[CMOVEMENTSTATE_T], ent_idx, p_mstate) { - Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); - CTransform_t *p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); - CBBox_t *p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); + Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); + CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); + CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); if (p_ctransform == NULL || p_bbox == NULL) continue; - bool on_ground = check_on_ground(ent_idx, p_ctransform->position, p_bbox->size, &data->tilemap, &scene->ent_manager); + bool on_ground = check_on_ground( + ent_idx, p_ctransform->position, p_bbox->size, + &data->tilemap, &scene->ent_manager + ); bool in_water = false; if (!(p_mstate->water_state & 1)) { @@ -738,9 +782,9 @@ void state_transition_update_system(Scene_t *scene) unsigned int tile_y1 = (p_ctransform->position.y) / TILE_SIZE; unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x) / TILE_SIZE; unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE; - for (unsigned int tile_y=tile_y1; tile_y <= tile_y2; tile_y++) + for (unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++) { - for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++) + for (unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) { unsigned int tile_idx = tile_y * data->tilemap.width + tile_x; in_water |= data->tilemap.tiles[tile_idx].water_level > 0; @@ -756,23 +800,23 @@ void state_transition_update_system(Scene_t *scene) } } -void update_tilemap_system(Scene_t *scene) +void update_tilemap_system(Scene_t* scene) { - LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data; TileGrid_t tilemap = data->tilemap; - CTileCoord_t *p_tilecoord; + CTileCoord_t* p_tilecoord; unsigned long ent_idx; sc_map_foreach(&scene->ent_manager.component_map[CTILECOORD_COMP_T], ent_idx, p_tilecoord) { - Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); - CTransform_t * p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); + Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); + CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); if (p_ctransform == NULL) continue; - CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); + CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); if (p_bbox == NULL) continue; // Update tilemap position - for (size_t i=0;in_tiles;++i) + for (size_t i = 0;i < p_tilecoord->n_tiles; ++i) { // Use previously store tile position // Clear from those positions @@ -788,9 +832,9 @@ void update_tilemap_system(Scene_t *scene) unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x - 1) / TILE_SIZE; unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y - 1) / TILE_SIZE; - for (unsigned int tile_y=tile_y1; tile_y <= tile_y2; tile_y++) + for (unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++) { - for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++) + for (unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) { unsigned int tile_idx = tile_y * tilemap.width + tile_x; p_tilecoord->tiles[p_tilecoord->n_tiles++] = tile_idx; @@ -800,21 +844,21 @@ void update_tilemap_system(Scene_t *scene) } } -void hitbox_update_system(Scene_t *scene) +void hitbox_update_system(Scene_t* scene) { static bool checked_entities[MAX_COMP_POOL_SIZE] = {0}; - LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data; TileGrid_t tilemap = data->tilemap; unsigned int ent_idx; CHitBoxes_t* p_hitbox; - //sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player) + sc_map_foreach(&scene->ent_manager.component_map[CHITBOXES_T], ent_idx, p_hitbox) { Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); - for (uint8_t i=0;in_boxes;++i) + for (uint8_t i = 0; i < p_hitbox->n_boxes; ++i) { Vector2 hitbox_pos = { .x = p_ctransform->position.x + p_hitbox->boxes[i].x, @@ -826,9 +870,9 @@ void hitbox_update_system(Scene_t *scene) unsigned int tile_x2 = (hitbox_pos.x + p_hitbox->boxes[i].width - 1) / TILE_SIZE; unsigned int tile_y2 = (hitbox_pos.y + p_hitbox->boxes[i].height - 1) / TILE_SIZE; - for (unsigned int tile_y=tile_y1; tile_y <= tile_y2; tile_y++) + for (unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++) { - for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++) + for (unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) { unsigned int tile_idx = tile_y * tilemap.width + tile_x; unsigned int other_ent_idx; @@ -838,24 +882,29 @@ void hitbox_update_system(Scene_t *scene) if (other_ent_idx == ent_idx) continue; if (checked_entities[other_ent_idx]) continue; - Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx); + Entity_t* p_other_ent = get_entity(&scene->ent_manager, other_ent_idx); if (!p_other_ent->m_alive) continue; // To only allow one way collision check if (p_other_ent->m_tag < p_ent->m_tag) continue; // To only allow one way collision check - CHurtbox_t *p_other_hurtbox = get_component(&scene->ent_manager, p_other_ent, CHURTBOX_T); + CHurtbox_t* p_other_hurtbox = get_component(&scene->ent_manager, p_other_ent, CHURTBOX_T); if (p_other_hurtbox == NULL) continue; - CTransform_t *p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T); + CTransform_t* p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T); Vector2 hurtbox_pos = Vector2Add(p_other_ct->position, p_other_hurtbox->offset); Vector2 overlap; - if (find_AABB_overlap(hitbox_pos, (Vector2){p_hitbox->boxes[i].width, p_hitbox->boxes[i].height}, hurtbox_pos, p_other_hurtbox->size, &overlap)) + if ( + find_AABB_overlap( + hitbox_pos, (Vector2){p_hitbox->boxes[i].width, p_hitbox->boxes[i].height}, + hurtbox_pos, p_other_hurtbox->size, &overlap + ) + ) { if (!p_other_hurtbox->fragile) continue; if (p_other_ent->m_tag == CRATES_ENT_TAG) { - CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); - CPlayerState_t * p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T); + CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); + CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T); if ( // TODO: Check Material of the crates p_ctransform->position.y + p_bbox->size.y <= p_other_ct->position.y @@ -871,8 +920,11 @@ void hitbox_update_system(Scene_t *scene) } } - CTileCoord_t *p_tilecoord = get_component(&scene->ent_manager, p_other_ent, CTILECOORD_COMP_T); - for (size_t i=0;in_tiles;++i) + CTileCoord_t* p_tilecoord = get_component( + &scene->ent_manager, p_other_ent, CTILECOORD_COMP_T + ); + + for (size_t i = 0;i < p_tilecoord->n_tiles; ++i) { // Use previously store tile position // Clear from those positions @@ -889,13 +941,13 @@ void hitbox_update_system(Scene_t *scene) } } -void sprite_animation_system(Scene_t *scene) +void sprite_animation_system(Scene_t* scene) { unsigned int ent_idx; CSprite_t* p_cspr; sc_map_foreach(&scene->ent_manager.component_map[CSPRITE_T], ent_idx, p_cspr) { - Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); + Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); // Update animation state if (p_cspr->transition_func != NULL) { @@ -922,12 +974,12 @@ void sprite_animation_system(Scene_t *scene) } } -void init_level_scene_data(LevelSceneData_t *data) +void init_level_scene_data(LevelSceneData_t* data) { sc_map_init_32(&data->collision_events, 128, 0); } -void term_level_scene_data(LevelSceneData_t *data) +void term_level_scene_data(LevelSceneData_t* data) { sc_map_term_32(&data->collision_events); } diff --git a/engine/game_systems.h b/engine/game_systems.h index 038ec7d..0931ece 100644 --- a/engine/game_systems.h +++ b/engine/game_systems.h @@ -1,20 +1,20 @@ #ifndef __GAME_SYSTEMS_H #define __GAME_SYSTEMS_H #include "scene_impl.h" -void init_level_scene_data(LevelSceneData_t *data); -void term_level_scene_data(LevelSceneData_t *data); +void init_level_scene_data(LevelSceneData_t* data); +void term_level_scene_data(LevelSceneData_t* data); void player_movement_input_system(Scene_t* scene); -void player_bbox_update_system(Scene_t *scene); -void tile_collision_system(Scene_t *scene); -void friction_coefficient_update_system(Scene_t *scene); -void global_external_forces_system(Scene_t *scene); +void player_bbox_update_system(Scene_t* scene); +void tile_collision_system(Scene_t* scene); +void friction_coefficient_update_system(Scene_t* scene); +void global_external_forces_system(Scene_t* scene); void movement_update_system(Scene_t* scene); void player_ground_air_transition_system(Scene_t* scene); -void state_transition_update_system(Scene_t *scene); -void update_tilemap_system(Scene_t *scene); -void hitbox_update_system(Scene_t *scene); -void sprite_animation_system(Scene_t *scene); +void state_transition_update_system(Scene_t* scene); +void update_tilemap_system(Scene_t* scene); +void hitbox_update_system(Scene_t* scene); +void sprite_animation_system(Scene_t* scene); unsigned int player_sprite_transition_func(Entity_t* ent); #endif // __GAME_SYSTEMS_H diff --git a/engine/gui.c b/engine/gui.c index c57227c..103b27c 100644 --- a/engine/gui.c +++ b/engine/gui.c @@ -119,7 +119,7 @@ int GuiGetStyle(int control, int property) return guiStyle[control*(RAYGUI_MAX_PROPS_BASE + RAYGUI_MAX_PROPS_EXTENDED) + property]; } -static int GetTextWidth(const char *text) +static int GetTextWidth(const char* text) { #if !defined(ICON_TEXT_PADDING) #define ICON_TEXT_PADDING 4 @@ -178,7 +178,7 @@ static int GetTextWidth(const char *text) return (int)textSize.x; } -const char **GetTextLines(const char *text, int *count) +const char** GetTextLines(const char* text, int* count) { #define RAYGUI_MAX_TEXT_LINES 128 @@ -228,7 +228,7 @@ static void GuiDrawRectangle(Rectangle rec, int borderWidth, Color borderColor, } } -static void GuiDrawText(const char *text, Rectangle bounds, int alignment, Color tint) +static void GuiDrawText(const char* text, Rectangle bounds, int alignment, Color tint) { #define TEXT_VALIGN_PIXEL_OFFSET(h) ((int)h%2) // Vertical alignment for pixel perfect @@ -362,7 +362,7 @@ static Rectangle GetTextBounds(int control, Rectangle bounds) //------- End of raygui section---------// -void UI_button(const UIComp_t *comp, const char *text) +void UI_button(const UIComp_t* comp, const char* text) { GuiDrawRectangle(comp->bbox, GuiGetStyle(BUTTON, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(BUTTON, BORDER + (comp->state*3))), comp->alpha), Fade(GetColor(GuiGetStyle(BUTTON, BASE + (comp->state*3))), comp->alpha)); GuiDrawText(text, GetTextBounds(BUTTON, comp->bbox), GuiGetStyle(BUTTON, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(BUTTON, TEXT + (comp->state*3))), comp->alpha)); diff --git a/engine/gui.h b/engine/gui.h index 0706538..8484a39 100644 --- a/engine/gui.h +++ b/engine/gui.h @@ -3,14 +3,13 @@ #include "raylib.h" #include "raygui.h" -typedef struct UIComp -{ +typedef struct UIComp { Rectangle bbox; GuiState state; float alpha; bool pressed; -}UIComp_t; +} UIComp_t; void init_UI(void); -void UI_button(const UIComp_t *bbox, const char *text); +void UI_button(const UIComp_t* bbox, const char* text); #endif diff --git a/engine/menu_scene.c b/engine/menu_scene.c index d983494..653bbc0 100644 --- a/engine/menu_scene.c +++ b/engine/menu_scene.c @@ -3,18 +3,18 @@ #include -static void menu_scene_render_func(Scene_t *scene) +static void menu_scene_render_func(Scene_t* scene) { - MenuSceneData_t *data = (MenuSceneData_t *)scene->scene_data; + MenuSceneData_t* data = (MenuSceneData_t*)scene->scene_data; DrawText("This is a game", 25, 220, 12, BLACK); UI_button(data->buttons, "Start"); UI_button(data->buttons + 1, "Continue"); UI_button(data->buttons + 2, "Exit"); } -static void menu_do_action(Scene_t *scene, ActionType_t action, bool pressed) +static void menu_do_action(Scene_t* scene, ActionType_t action, bool pressed) { - MenuSceneData_t *data = (MenuSceneData_t *)scene->scene_data; + MenuSceneData_t* data = (MenuSceneData_t*)scene->scene_data; int new_selection = data->selected_comp; if (!pressed) { @@ -63,7 +63,7 @@ static void menu_do_action(Scene_t *scene, ActionType_t action, bool pressed) static void gui_loop(Scene_t* scene) { - MenuSceneData_t *data = (MenuSceneData_t *)scene->scene_data; + MenuSceneData_t* data = (MenuSceneData_t*)scene->scene_data; if (Vector2LengthSqr(GetMouseDelta()) > 1) { data->mode = MOUSE_MODE; @@ -74,7 +74,7 @@ static void gui_loop(Scene_t* scene) else { data->buttons[data->selected_comp].state = STATE_NORMAL; - for (size_t i=0;imax_comp;i++) + for (size_t i = 0;i < data->max_comp; i++) { if ((data->buttons[i].state != STATE_DISABLED)) { @@ -110,32 +110,28 @@ static void gui_loop(Scene_t* scene) } } } - } } -void init_menu_scene(MenuScene_t *scene) +void init_menu_scene(MenuScene_t* scene) { init_scene(&scene->scene, MENU_SCENE, &menu_scene_render_func, &menu_do_action); scene->scene.scene_data = &scene->data; sc_array_add(&scene->scene.systems, &gui_loop); - scene->data.buttons[0] = (UIComp_t) - { + scene->data.buttons[0] = (UIComp_t) { .bbox = {25,255,125,30}, .state = STATE_NORMAL, .alpha = 1.0 }; - scene->data.buttons[1] = (UIComp_t) - { + scene->data.buttons[1] = (UIComp_t) { .bbox = {25,300,125,30}, .state = STATE_NORMAL, .alpha = 1.0 }; - scene->data.buttons[2] = (UIComp_t) - { + scene->data.buttons[2] = (UIComp_t) { .bbox = {25,345,125,30}, .state = STATE_NORMAL, .alpha = 1.0 @@ -151,7 +147,7 @@ void init_menu_scene(MenuScene_t *scene) sc_map_put_64(&scene->scene.action_map, KEY_ENTER, ACTION_CONFIRM); } -void free_menu_scene(MenuScene_t *scene) +void free_menu_scene(MenuScene_t* scene) { free_scene(&scene->scene); } diff --git a/engine/scene_impl.h b/engine/scene_impl.h index f820229..85ce540 100644 --- a/engine/scene_impl.h +++ b/engine/scene_impl.h @@ -7,57 +7,50 @@ #include "engine.h" #include "gui.h" -typedef struct Tile -{ +typedef struct Tile { bool solid; unsigned int water_level; struct sc_map_64 entities_set; }Tile_t; -typedef struct TileGrid -{ +typedef struct TileGrid { unsigned int width; unsigned int height; unsigned int n_tiles; Tile_t * tiles; }TileGrid_t; -typedef struct LevelSceneData -{ +typedef struct LevelSceneData { TileGrid_t tilemap; struct sc_map_32 collision_events; }LevelSceneData_t; -typedef struct LevelScene -{ +typedef struct LevelScene { Scene_t scene; LevelSceneData_t data; }LevelScene_t; -void init_level_scene(LevelScene_t *scene); -void free_level_scene(LevelScene_t *scene); -void reload_level_scene(LevelScene_t *scene); +void init_level_scene(LevelScene_t* scene); +void free_level_scene(LevelScene_t* scene); +void reload_level_scene(LevelScene_t* scene); -typedef enum GuiMode -{ +typedef enum GuiMode { KEYBOARD_MODE, MOUSE_MODE -}GuiMode_t; +} GuiMode_t; -typedef struct MenuSceneData -{ +typedef struct MenuSceneData { UIComp_t buttons[3]; int selected_comp; int max_comp; GuiMode_t mode; -}MenuSceneData_t; +} MenuSceneData_t; -typedef struct MenuScene -{ +typedef struct MenuScene { Scene_t scene; MenuSceneData_t data; -}MenuScene_t; +} MenuScene_t; -void init_menu_scene(MenuScene_t *scene); -void free_menu_scene(MenuScene_t *scene); +void init_menu_scene(MenuScene_t* scene); +void free_menu_scene(MenuScene_t* scene); #endif // __SCENE_IMPL_H diff --git a/main.c b/main.c index ecd3f02..a0b2305 100644 --- a/main.c +++ b/main.c @@ -4,8 +4,7 @@ #define N_SCENES 3 Scene_t *scenes[N_SCENES]; -static GameEngine_t engine = -{ +static GameEngine_t engine = { .scenes = scenes, .max_scenes = 2, .curr_scene = 0 @@ -47,11 +46,11 @@ int main(void) { // This entire key processing relies on the assumption that a pressed key will // appear in the polling of raylib - Scene_t * curr_scene = engine.scenes[engine.curr_scene]; + Scene_t* curr_scene = engine.scenes[engine.curr_scene]; unsigned int sz = sc_queue_size(&key_buffer); // Process any existing pressed key - for (size_t i=0; iaction_map, button); diff --git a/menu_test.c b/menu_test.c index addfe4b..8d3faa7 100644 --- a/menu_test.c +++ b/menu_test.c @@ -22,7 +22,7 @@ int main(void) unsigned int sz = sc_queue_size(&key_buffer); // Process any existing pressed key - for (size_t i=0; i