Attempt to apply code style consitency

Squashed commit of the following:

commit 249fb96c63f68913ef632b8d1006e8a2d1c1b6d3
Author: En Yi <en.yi@tutanota.com>
Date:   Sat Apr 15 22:51:08 2023 +0800

    Ensure 120 column width limit

    Changelog:
    - this is just to avoid overly long line of code

commit 4b1ffa8fc89f17418de15f5fc3f0e73040f1d40c
Author: En Yi <en.yi@tutanota.com>
Date:   Sat Apr 15 20:05:07 2023 +0800

    Ensure style consistency in For loop

commit dcb41960407224592f78278313e97c2cc4bf66a5
Author: En Yi <en.yi@tutanota.com>
Date:   Sat Apr 15 17:26:20 2023 +0800

    Ensure consistency in code style

    Internal Changelog:
    - Use char* var instead of char *var. Preference
scene_man
En Yi 2023-04-15 22:52:38 +08:00
parent 82b8a3b988
commit dc760bf8db
25 changed files with 405 additions and 370 deletions

View File

@ -2,8 +2,21 @@ Trivial Variables: i,n,c,etc... (Only one letter. If one letter isn't clear, the
Local Variables: snake_case Local Variables: snake_case
Global Variables: g_snake_case Global Variables: g_snake_case
Const Variables: ALL_CAPS 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 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 Structs: PascalCase
Enums: PascalCase Enums: PascalCase

View File

@ -1,6 +1,6 @@
#include "AABB.h" #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 // No Overlap
if (l1.y < l2.x || l2.y < l1.x) return false; 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; 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 // Note that we include one extra pixel for checking
// This avoid overlapping on the border // This avoid overlapping on the border

View File

@ -2,6 +2,6 @@
#define __AABB_H #define __AABB_H
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* 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 * const overlap); bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2* overlap);
#endif // __AABB_H #endif // __AABB_H

View File

@ -6,8 +6,7 @@
// TODO: Look at sc to use macros to auto generate functions // TODO: Look at sc to use macros to auto generate functions
#define N_COMPONENTS 10 #define N_COMPONENTS 10
typedef enum ComponentEnum typedef enum ComponentEnum {
{
CBBOX_COMP_T, CBBOX_COMP_T,
CTRANSFORM_COMP_T, CTRANSFORM_COMP_T,
CTILECOORD_COMP_T, CTILECOORD_COMP_T,
@ -18,66 +17,58 @@ typedef enum ComponentEnum
CHITBOXES_T, CHITBOXES_T,
CHURTBOX_T, CHURTBOX_T,
CSPRITE_T, CSPRITE_T,
}ComponentEnum_t; } ComponentEnum_t;
typedef struct _CBBox_t typedef struct _CBBox_t {
{
Vector2 size; Vector2 size;
Vector2 offset; Vector2 offset;
Vector2 half_size; Vector2 half_size;
bool solid; bool solid;
bool fragile; bool fragile;
}CBBox_t; } CBBox_t;
typedef struct _CTransform_t typedef struct _CTransform_t {
{
Vector2 prev_position; Vector2 prev_position;
Vector2 position; Vector2 position;
Vector2 velocity; Vector2 velocity;
Vector2 accel; Vector2 accel;
Vector2 fric_coeff; Vector2 fric_coeff;
}CTransform_t; } CTransform_t;
typedef struct _CMovementState_t typedef struct _CMovementState_t {
{
uint8_t ground_state; uint8_t ground_state;
uint8_t water_state; uint8_t water_state;
}CMovementState_t; } CMovementState_t;
// This is to store the occupying tiles // This is to store the occupying tiles
// Limits to store 4 tiles at a tile, // Limits to store 4 tiles at a tile,
// Thus the size of the entity cannot be larger than the 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 tiles[8];
unsigned int n_tiles; unsigned int n_tiles;
}CTileCoord_t; } CTileCoord_t;
typedef struct _CJump_t typedef struct _CJump_t {
{
unsigned int jumps; unsigned int jumps;
unsigned int max_jumps; unsigned int max_jumps;
int jump_speed; int jump_speed;
bool jumped; bool jumped;
bool jump_ready; bool jump_ready;
bool short_hop; bool short_hop;
}CJump_t; } CJump_t;
typedef enum PlayerState typedef enum PlayerState {
{
GROUNDED, GROUNDED,
AIR, AIR,
}PlayerState_t; } PlayerState_t;
typedef struct _CPlayerState_t typedef struct _CPlayerState_t {
{
Vector2 player_dir; Vector2 player_dir;
uint8_t jump_pressed; uint8_t jump_pressed;
uint8_t is_crouch; uint8_t is_crouch;
}CPlayerState_t; } CPlayerState_t;
typedef enum ContainerItem typedef enum ContainerItem {
{
CONTAINER_EMPTY, CONTAINER_EMPTY,
CONTAINER_LEFT_ARROW, CONTAINER_LEFT_ARROW,
CONTAINER_RIGHT_ARROW, CONTAINER_RIGHT_ARROW,
@ -85,37 +76,32 @@ typedef enum ContainerItem
CONTAINER_DOWN_ARROW, CONTAINER_DOWN_ARROW,
CONTAINER_COIN, CONTAINER_COIN,
CONTAINER_BOMB, CONTAINER_BOMB,
}ContainerItem_t; } ContainerItem_t;
typedef enum ContainerMaterial typedef enum ContainerMaterial {
{
WOODEN_CONTAINER, WOODEN_CONTAINER,
METAL_CONTAINER, METAL_CONTAINER,
}ContainerMaterial_t; } ContainerMaterial_t;
typedef struct _CContainer_t typedef struct _CContainer_t {
{
ContainerMaterial_t material; ContainerMaterial_t material;
ContainerItem_t item; ContainerItem_t item;
}CContainer_t; } CContainer_t;
typedef struct _CHitBoxes_t typedef struct _CHitBoxes_t {
{
Rectangle boxes[2]; Rectangle boxes[2];
uint8_t n_boxes; uint8_t n_boxes;
bool strong; bool strong;
}CHitBoxes_t; } CHitBoxes_t;
typedef struct _CHurtbox_t typedef struct _CHurtbox_t {
{
Vector2 offset; Vector2 offset;
Vector2 size; Vector2 size;
bool fragile; bool fragile;
}CHurtbox_t; } CHurtbox_t;
// Credits to bedroomcoders.co.uk for this // Credits to bedroomcoders.co.uk for this
typedef struct Sprite typedef struct Sprite {
{
Texture2D* texture; Texture2D* texture;
Vector2 frame_size; Vector2 frame_size;
Vector2 origin; Vector2 origin;
@ -124,17 +110,16 @@ typedef struct Sprite
int elapsed; int elapsed;
int speed; int speed;
char* name; char* name;
}Sprite_t; } Sprite_t;
typedef unsigned int (*sprite_transition_func_t)(Entity_t *ent); // Transition requires knowledge of the entity 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 const char * const *sprites_map; //Array of all sprite names associated
Sprite_t* sprite; Sprite_t* sprite;
sprite_transition_func_t transition_func; sprite_transition_func_t transition_func;
Vector2 offset; Vector2 offset;
unsigned int current_idx; unsigned int current_idx;
}CSprite_t; } CSprite_t;
static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y) static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
{ {

View File

@ -1,21 +1,21 @@
#include "entManager.h" #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); sc_map_init_64v(&p_manager->entities, MAX_COMP_POOL_SIZE, 0);
for (size_t i=0; i<N_COMPONENTS; ++i) for (size_t i = 0; i < N_COMPONENTS; ++i)
{ {
sc_map_init_64v(p_manager->component_map + i, MAX_COMP_POOL_SIZE, 0); sc_map_init_64v(p_manager->component_map + i, MAX_COMP_POOL_SIZE, 0);
} }
for (size_t i=0; i<N_TAGS; ++i) for (size_t i = 0; i < N_TAGS; ++i)
{ {
sc_map_init_64v(p_manager->entities_map + i, MAX_COMP_POOL_SIZE, 0); sc_map_init_64v(p_manager->entities_map + i, MAX_COMP_POOL_SIZE, 0);
} }
sc_queue_init(&p_manager->to_add); sc_queue_init(&p_manager->to_add);
sc_queue_init(&p_manager->to_remove); 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 // This will only update the entity map of the manager
// It does not make new entities, but will free entity // 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_map_put_64v(&p_manager->entities_map[p_entity->m_tag], e_idx, (void *)p_entity);
} }
sc_queue_clear(&p_manager->to_add); 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; unsigned long e_id;
Entity_t *p_ent; Entity_t* p_ent;
sc_map_foreach (&p_manager->entities, e_id, p_ent) sc_map_foreach (&p_manager->entities, e_id, p_ent)
{ {
remove_entity(p_manager, e_id); remove_entity(p_manager, e_id);
@ -60,15 +59,15 @@ void clear_entity_manager(EntityManager_t *p_manager)
update_entity_manager(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); clear_entity_manager(p_manager);
sc_map_term_64v(&p_manager->entities); sc_map_term_64v(&p_manager->entities);
for (size_t i=0; i<N_COMPONENTS; ++i) for (size_t i = 0; i < N_COMPONENTS; ++i)
{ {
sc_map_term_64v(p_manager->component_map + i); sc_map_term_64v(p_manager->component_map + i);
} }
for (size_t i=0; i<N_TAGS; ++i) for (size_t i = 0; i < N_TAGS; ++i)
{ {
sc_map_term_64v(p_manager->entities_map + i); sc_map_term_64v(p_manager->entities_map + i);
} }
@ -76,10 +75,10 @@ void free_entity_manager(EntityManager_t *p_manager)
sc_queue_term(&p_manager->to_remove); 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; 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; p_ent->m_tag = tag;
if (p_ent) if (p_ent)
{ {
@ -88,9 +87,9 @@ Entity_t *add_entity(EntityManager_t *p_manager, EntityTag_t tag)
return p_ent; 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; if(!sc_map_found(&p_manager->entities)) return;
// This only marks the entity for deletion // This only marks the entity for deletion
// Does not free entity. This is done during the update // 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; if(!sc_map_found(&p_manager->entities)) return NULL;
return p_entity; return p_entity;
} }
// Components are not expected to be removed // Components are not expected to be removed
// So, no need to extra steps to deal with iterator invalidation // 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_type_idx = (unsigned long)comp_type;
unsigned long comp_idx = 0; 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) if (p_comp)
{ {
sc_map_put_64(&p_entity->components, comp_type_idx, comp_idx); 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; 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; 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); void * p_comp = sc_map_get_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id);

View File

@ -4,27 +4,26 @@
#include "sc/map/sc_map.h" #include "sc/map/sc_map.h"
#include "mempool.h" // includes entity and components #include "mempool.h" // includes entity and components
typedef struct EntityManager typedef struct EntityManager {
{
// All fields are Read-Only // All fields are Read-Only
struct sc_map_64v entities; // ent id : entity struct sc_map_64v entities; // ent id : entity
struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}] struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}]
struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...] struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...]
struct sc_queue_uint to_add; struct sc_queue_uint to_add;
struct sc_queue_uint to_remove; struct sc_queue_uint to_remove;
}EntityManager_t; } EntityManager_t;
void init_entity_manager(EntityManager_t *p_manager); void init_entity_manager(EntityManager_t* p_manager);
void update_entity_manager(EntityManager_t *p_manager); void update_entity_manager(EntityManager_t* p_manager);
void clear_entity_manager(EntityManager_t *p_manager); void clear_entity_manager(EntityManager_t* p_manager);
void free_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); Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag);
void remove_entity(EntityManager_t *p_manager, unsigned long id); 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);
void *add_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* 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 remove_component(EntityManager_t* p_manager, Entity_t* entity, ComponentEnum_t comp_type);
#endif // __ENTITY_MANAGER_H #endif // __ENTITY_MANAGER_H

View File

@ -4,19 +4,17 @@
#include "sc/map/sc_map.h" #include "sc/map/sc_map.h"
#define N_TAGS 4 #define N_TAGS 4
enum EntityTag typedef enum EntityTag {
{
NO_ENT_TAG, NO_ENT_TAG,
PLAYER_ENT_TAG, PLAYER_ENT_TAG,
ENEMY_ENT_TAG, ENEMY_ENT_TAG,
CRATES_ENT_TAG, CRATES_ENT_TAG,
}; } EntityTag_t;
typedef enum EntityTag EntityTag_t;
typedef struct Entity typedef struct Entity {
{
unsigned long m_id; unsigned long m_id;
EntityTag_t m_tag; EntityTag_t m_tag;
bool m_alive; bool m_alive;
struct sc_map_64 components; struct sc_map_64 components;
}Entity_t; } Entity_t;
#endif // __ENTITY_H #endif // __ENTITY_H

View File

@ -16,18 +16,16 @@ static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE];
static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE]; static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE];
static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE]; static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE];
typedef struct MemPool typedef struct MemPool {
{
void * const buffer; void * const buffer;
const unsigned long max_size; const unsigned long max_size;
const unsigned long elem_size; const unsigned long elem_size;
bool *use_list; bool *use_list;
struct sc_queue_uint free_list; struct sc_queue_uint free_list;
}MemPool_t; } MemPool_t;
// Static allocate mempools // 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}}, {bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), NULL, {0}},
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_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}}, {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}}, {churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}},
{csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_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; static bool pool_inited = false;
void init_memory_pools(void) void init_memory_pools(void)
{ {
if (!pool_inited) if (!pool_inited)
{ {
for (size_t i=0; i<N_COMPONENTS; ++i) for (size_t i = 0; i < N_COMPONENTS; ++i)
{ {
memset(comp_mempools[i].buffer, 0, comp_mempools[i].elem_size*comp_mempools[i].max_size); memset(comp_mempools[i].buffer, 0, comp_mempools[i].elem_size * comp_mempools[i].max_size);
comp_mempools[i].use_list = (bool *)calloc(comp_mempools[i].max_size, sizeof(bool)); comp_mempools[i].use_list = (bool*)calloc(comp_mempools[i].max_size, sizeof(bool));
assert(comp_mempools[i].use_list != NULL); assert(comp_mempools[i].use_list != NULL);
sc_queue_init(&comp_mempools[i].free_list); sc_queue_init(&comp_mempools[i].free_list);
for (size_t j=0;j<comp_mempools[i].max_size;++j) for (size_t j = 0; j < comp_mempools[i].max_size; ++j)
{ {
sc_queue_add_last(&comp_mempools[i].free_list, j); sc_queue_add_last(&comp_mempools[i].free_list, j);
} }
@ -61,7 +65,7 @@ void init_memory_pools(void)
memset(ent_mempool.buffer, 0, ent_mempool.elem_size*ent_mempool.max_size); memset(ent_mempool.buffer, 0, ent_mempool.elem_size*ent_mempool.max_size);
sc_queue_init(&ent_mempool.free_list); sc_queue_init(&ent_mempool.free_list);
ent_mempool.use_list = (bool *)calloc(ent_mempool.max_size, sizeof(bool)); ent_mempool.use_list = (bool *)calloc(ent_mempool.max_size, sizeof(bool));
for (size_t i=0;i<ent_mempool.max_size;++i) for (size_t i = 0; i < ent_mempool.max_size; ++i)
{ {
entity_buffer[i].m_id = i; entity_buffer[i].m_id = i;
sc_map_init_64(&entity_buffer[i].components, 16 ,0); sc_map_init_64(&entity_buffer[i].components, 16 ,0);
@ -75,7 +79,7 @@ void free_memory_pools(void)
{ {
if (pool_inited) if (pool_inited)
{ {
for (size_t i=0; i<N_COMPONENTS; ++i) for (size_t i = 0; i < N_COMPONENTS; ++i)
{ {
free(comp_mempools[i].use_list); free(comp_mempools[i].use_list);
sc_queue_term(&comp_mempools[i].free_list); sc_queue_term(&comp_mempools[i].free_list);
@ -83,7 +87,7 @@ void free_memory_pools(void)
free(ent_mempool.use_list); free(ent_mempool.use_list);
sc_queue_term(&ent_mempool.free_list); sc_queue_term(&ent_mempool.free_list);
for (int i=0;i<MAX_COMP_POOL_SIZE;++i) for (int i = 0; i < MAX_COMP_POOL_SIZE; ++i)
{ {
sc_map_term_64(&entity_buffer[i].components); sc_map_term_64(&entity_buffer[i].components);
} }
@ -91,20 +95,20 @@ void free_memory_pools(void)
} }
} }
Entity_t* new_entity_from_mempool(unsigned long *p_e_idx) Entity_t* new_entity_from_mempool(unsigned long* e_idx_ptr)
{ {
if(sc_queue_empty(&ent_mempool.free_list)) return NULL; if(sc_queue_empty(&ent_mempool.free_list)) return NULL;
unsigned long e_idx = sc_queue_del_first(&ent_mempool.free_list); unsigned long e_idx = sc_queue_del_first(&ent_mempool.free_list);
*p_e_idx = e_idx; *e_idx_ptr = e_idx;
ent_mempool.use_list[e_idx] = true; ent_mempool.use_list[e_idx] = true;
Entity_t * ent = entity_buffer + e_idx; Entity_t* ent = entity_buffer + e_idx;
sc_map_clear_64(&ent->components); sc_map_clear_64(&ent->components);
ent->m_alive = true; ent->m_alive = true;
ent->m_tag = NO_ENT_TAG; ent->m_tag = NO_ENT_TAG;
return ent; 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; if (!ent_mempool.use_list[idx]) return NULL;
return entity_buffer + idx; 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); assert(comp_type < N_COMPONENTS);
if(!sc_queue_empty(&comp_mempools[comp_type].free_list)) 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) void print_mempool_stats(char* buffer)
{ {
int written = 0; for (size_t i = 0; i < N_COMPONENTS; ++i)
for (size_t i=0; i<N_COMPONENTS; ++i)
{ {
written += sprintf(buffer+written, "%lu: %lu/%lu\n", i, sc_queue_size(&comp_mempools[i].free_list), comp_mempools[i].max_size); buffer += sprintf(
buffer, "%lu: %lu/%lu\n",
i, sc_queue_size(&comp_mempools[i].free_list), comp_mempools[i].max_size
);
} }
} }

View File

@ -6,11 +6,11 @@
void init_memory_pools(void); void init_memory_pools(void);
void free_memory_pools(void); void free_memory_pools(void);
Entity_t* new_entity_from_mempool(unsigned long *p_idx); Entity_t* new_entity_from_mempool(unsigned long* e_idx_ptr);
Entity_t* get_entity_wtih_id(unsigned long idx); Entity_t* get_entity_wtih_id(unsigned long idx);
void free_entity_to_mempool(unsigned long idx); 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* get_component_wtih_id(ComponentEnum_t comp_type, unsigned long idx); void* get_component_wtih_id(ComponentEnum_t comp_type, unsigned long idx);
void free_component_to_mempool(ComponentEnum_t comp_type, unsigned long idx); void free_component_to_mempool(ComponentEnum_t comp_type, unsigned long idx);

View File

@ -14,7 +14,7 @@ static Sound sfx[MAX_SOUNDS];
static Sprite_t sprites[MAX_SPRITES]; static Sprite_t sprites[MAX_SPRITES];
// Maybe need a circular buffer?? // Maybe need a circular buffer??
Texture2D* add_texture(Assets_t *assets, char *name, char *path) Texture2D* add_texture(Assets_t* assets, const char* name, const char* path)
{ {
uint8_t tex_idx = free_idx[0]; uint8_t tex_idx = free_idx[0];
assert(tex_idx < MAX_TEXTURES); assert(tex_idx < MAX_TEXTURES);
@ -24,7 +24,7 @@ Texture2D* add_texture(Assets_t *assets, char *name, char *path)
return textures + tex_idx; return textures + tex_idx;
} }
Sprite_t* add_sprite(Assets_t *assets, char *name, Texture2D* texture) Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture)
{ {
uint8_t spr_idx = free_idx[1]; uint8_t spr_idx = free_idx[1];
assert(spr_idx < MAX_SPRITES); assert(spr_idx < MAX_SPRITES);
@ -35,7 +35,7 @@ Sprite_t* add_sprite(Assets_t *assets, char *name, Texture2D* texture)
return sprites + spr_idx; return sprites + spr_idx;
} }
Sound* add_sound(Assets_t *assets, char *name, char *path) Sound* add_sound(Assets_t* assets, const char* name, const char* path)
{ {
uint8_t snd_idx = free_idx[2]; uint8_t snd_idx = free_idx[2];
assert(snd_idx < MAX_SOUNDS); assert(snd_idx < MAX_SOUNDS);
@ -45,7 +45,7 @@ Sound* add_sound(Assets_t *assets, char *name, char *path)
return sfx + snd_idx; return sfx + snd_idx;
} }
Font* add_font(Assets_t *assets, char *name, char *path) Font* add_font(Assets_t* assets, const char* name, const char* path)
{ {
uint8_t fnt_idx = free_idx[3]; uint8_t fnt_idx = free_idx[3];
assert(fnt_idx < MAX_FONTS); assert(fnt_idx < MAX_FONTS);
@ -55,7 +55,7 @@ Font* add_font(Assets_t *assets, char *name, char *path)
return fonts + fnt_idx; return fonts + fnt_idx;
} }
void init_assets(Assets_t *assets) void init_assets(Assets_t* assets)
{ {
sc_map_init_s64(&assets->m_fonts, MAX_FONTS, 0); sc_map_init_s64(&assets->m_fonts, MAX_FONTS, 0);
sc_map_init_s64(&assets->m_sprites, MAX_SPRITES, 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); 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_textures);
sc_map_clear_s64(&assets->m_fonts); 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)); memset(free_idx, 0, sizeof(free_idx));
} }
void term_assets(Assets_t *assets) void term_assets(Assets_t* assets)
{ {
free_all_assets(assets); free_all_assets(assets);
sc_map_term_s64(&assets->m_textures); sc_map_term_s64(&assets->m_textures);
@ -81,7 +81,7 @@ void term_assets(Assets_t *assets)
sc_map_term_s64(&assets->m_sprites); 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); uint8_t tex_idx = sc_map_get_s64(&assets->m_textures, name);
if (sc_map_found(&assets->m_textures)) if (sc_map_found(&assets->m_textures))
@ -91,7 +91,7 @@ Texture2D* get_texture(Assets_t *assets, const char *name)
return NULL; 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); uint8_t spr_idx = sc_map_get_s64(&assets->m_sprites, name);
if (sc_map_found(&assets->m_sprites)) if (sc_map_found(&assets->m_sprites))
@ -101,7 +101,7 @@ Sprite_t* get_sprite(Assets_t *assets, const char *name)
return NULL; 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); uint8_t snd_idx = sc_map_get_s64(&assets->m_sounds, name);
if (sc_map_found(&assets->m_sounds)) if (sc_map_found(&assets->m_sounds))
@ -111,7 +111,7 @@ Sound* get_sound(Assets_t *assets, const char *name)
return NULL; 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); uint8_t fnt_idx = sc_map_get_s64(&assets->m_fonts, name);
if (sc_map_found(&assets->m_fonts)) if (sc_map_found(&assets->m_fonts))
@ -121,7 +121,7 @@ Font* get_font(Assets_t *assets, const char *name)
return NULL; return NULL;
} }
void draw_sprite(Sprite_t *spr, Vector2 pos) void draw_sprite(Sprite_t* spr, Vector2 pos)
{ {
Rectangle rec = { Rectangle rec = {
spr->origin.x + spr->frame_size.x * spr->current_frame, spr->origin.x + spr->frame_size.x * spr->current_frame,

View File

@ -13,20 +13,20 @@ typedef struct Assets
}Assets_t; }Assets_t;
void init_assets(Assets_t *assets); void init_assets(Assets_t* assets);
void free_all_assets(Assets_t *assets); void free_all_assets(Assets_t* assets);
void term_assets(Assets_t *assets); void term_assets(Assets_t* assets);
Texture2D* add_texture(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, char *name, Texture2D* texture); Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture);
Sound* add_sound(Assets_t *assets, char *name, char *path); Sound* add_sound(Assets_t * assets, const char* name, const char* path);
Font* add_font(Assets_t *assets, char *name, char *path); Font* add_font(Assets_t* assets, const char* name, const char* path);
Texture2D* get_texture(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); Sprite_t* get_sprite(Assets_t* assets, const char* name);
Sound* get_sound(Assets_t *assets, const char *name); Sound* get_sound(Assets_t* assets, const char* name);
Font* get_font(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 #endif // __ASSETS_H

View File

@ -1,8 +1,7 @@
#include "assets_maps.h" #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_stand",
"plr_run", "plr_run",
}; };

View File

@ -8,6 +8,6 @@ enum PlayerSpriteEnum
SPR_PLAYER_RUN 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 #endif // __ASSETS_MAPS_H

View File

@ -9,8 +9,7 @@
#define MAX_N_TILES 4096 #define MAX_N_TILES 4096
static Tile_t all_tiles[MAX_N_TILES] = {0}; static Tile_t all_tiles[MAX_N_TILES] = {0};
enum EntitySpawnSelection enum EntitySpawnSelection {
{
TOGGLE_TILE = 0, TOGGLE_TILE = 0,
SPAWN_CRATE, SPAWN_CRATE,
SPAWN_METAL_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) 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; TileGrid_t tilemap = data->tilemap;
Entity_t *p_ent; Entity_t* p_ent;
for (size_t i=0; i<tilemap.n_tiles;++i) for (size_t i = 0; i < tilemap.n_tiles; ++i)
{ {
char buffer[6] = {0}; char buffer[6] = {0};
int x = (i % tilemap.width) * TILE_SIZE; int x = (i % tilemap.width) * TILE_SIZE;
@ -72,7 +71,7 @@ static void level_scene_render_func(Scene_t* scene)
CHitBoxes_t* p_hitbox = get_component(&scene->ent_manager, p_ent, CHITBOXES_T); CHitBoxes_t* p_hitbox = get_component(&scene->ent_manager, p_ent, CHITBOXES_T);
if (p_hitbox != NULL) if (p_hitbox != NULL)
{ {
for (uint8_t i=0;i<p_hitbox->n_boxes;++i) for (uint8_t i = 0;i < p_hitbox->n_boxes; ++i)
{ {
Rectangle rec = { Rectangle rec = {
.x = p_ct->position.x + p_hitbox->boxes[i].x, .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; i<tilemap.n_tiles;++i) for (size_t i = 0; i < tilemap.n_tiles; ++i)
{ {
int x = (i % tilemap.width) * TILE_SIZE; int x = (i % tilemap.width) * TILE_SIZE;
int y = (i / tilemap.width) * TILE_SIZE; int y = (i / tilemap.width) * TILE_SIZE;
@ -119,12 +118,12 @@ static void level_scene_render_func(Scene_t* scene)
} }
// Draw tile grid // Draw tile grid
for (size_t i=0; i<tilemap.width;++i) for (size_t i = 0; i < tilemap.width; ++i)
{ {
int x = (i+1)*TILE_SIZE; int x = (i+1)*TILE_SIZE;
DrawLine(x, 0, x, tilemap.height * TILE_SIZE, BLACK); DrawLine(x, 0, x, tilemap.height * TILE_SIZE, BLACK);
} }
for (size_t i=0; i<tilemap.height;++i) for (size_t i = 0; i < tilemap.height;++i)
{ {
int y = (i+1)*TILE_SIZE; int y = (i+1)*TILE_SIZE;
DrawLine(0, y, tilemap.width * TILE_SIZE, y, BLACK); DrawLine(0, y, tilemap.width * TILE_SIZE, y, BLACK);
@ -162,15 +161,17 @@ static void level_scene_render_func(Scene_t* scene)
DrawText(mempool_stats, tilemap.width * TILE_SIZE + 1, 350, 12, BLACK); DrawText(mempool_stats, tilemap.width * TILE_SIZE + 1, 350, 12, BLACK);
} }
static void spawn_crate(Scene_t *scene, unsigned int tile_idx, bool metal) static void spawn_crate(Scene_t* scene, unsigned int tile_idx, bool metal)
{ {
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; LevelSceneData_t* data = (LevelSceneData_t*)scene->scene_data;
Entity_t *p_crate = add_entity(&scene->ent_manager, CRATES_ENT_TAG); 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); CBBox_t* p_bbox = add_component(&scene->ent_manager, p_crate, CBBOX_COMP_T);
set_bbox(p_bbox, TILE_SIZE, TILE_SIZE); set_bbox(p_bbox, TILE_SIZE, TILE_SIZE);
p_bbox->solid = true; p_bbox->solid = true;
p_bbox->fragile = !metal; 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.x = (tile_idx % data->tilemap.width) * TILE_SIZE;
p_ctransform->position.y = (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); 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; 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); set_bbox(p_bbox, PLAYER_WIDTH, PLAYER_HEIGHT);
add_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); 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->jump_speed = 680;
p_cjump->jumps = 1; p_cjump->jumps = 1;
p_cjump->max_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); add_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T);
CHitBoxes_t* p_hitbox = add_component(&scene->ent_manager, p_ent, CHITBOXES_T); CHitBoxes_t* p_hitbox = add_component(&scene->ent_manager, p_ent, CHITBOXES_T);
p_hitbox->n_boxes = 2; p_hitbox->n_boxes = 2;
p_hitbox->boxes[0] = (Rectangle) p_hitbox->boxes[0] = (Rectangle) {
{
.x = 0, .x = 0,
.y = -1, .y = -1,
.width = p_bbox->size.x - 1, .width = p_bbox->size.x - 1,
.height = p_bbox->size.y + 2, .height = p_bbox->size.y + 2,
}; };
p_hitbox->boxes[1] = (Rectangle) p_hitbox->boxes[1] = (Rectangle) {
{
.x = -1, .x = -1,
.y = 0, .y = 0,
.width = p_bbox->size.x + 2, .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; 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 // 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; 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) sc_map_foreach_value(&scene->ent_manager.component_map[CPLAYERSTATE_T], p_playerstate)
{ {
switch(action) 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); init_scene(&scene->scene, LEVEL_SCENE, &level_scene_render_func, &level_do_action);
scene->scene.scene_data = &scene->data; 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; scene->data.tilemap.n_tiles = scene->data.tilemap.width * scene->data.tilemap.height;
assert(scene->data.tilemap.n_tiles <= MAX_N_TILES); assert(scene->data.tilemap.n_tiles <= MAX_N_TILES);
scene->data.tilemap.tiles = all_tiles; scene->data.tilemap.tiles = all_tiles;
for (size_t i=0; i<MAX_N_TILES;i++) for (size_t i = 0; i < MAX_N_TILES;i++)
{ {
all_tiles[i].solid = 0; all_tiles[i].solid = 0;
sc_map_init_64(&all_tiles[i].entities_set, 16, 0); sc_map_init_64(&all_tiles[i].entities_set, 16, 0);
} }
for (size_t i=0; i<scene->data.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; unsigned int tile_idx = (scene->data.tilemap.height - 1) * scene->data.tilemap.width + i;
all_tiles[tile_idx].solid = true; // for testing 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); 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); free_scene(&scene->scene);
for (size_t i=0; i<MAX_N_TILES;i++) for (size_t i = 0; i < MAX_N_TILES;i++)
{ {
all_tiles[i].solid = 0; all_tiles[i].solid = 0;
sc_map_term_64(&all_tiles[i].entities_set); sc_map_term_64(&all_tiles[i].entities_set);
@ -385,6 +385,6 @@ void free_level_scene(LevelScene_t *scene)
term_level_scene_data(&scene->data); term_level_scene_data(&scene->data);
} }
void reload_level_scene(LevelScene_t *scene) void reload_level_scene(LevelScene_t* scene)
{ {
} }

View File

@ -1,12 +1,13 @@
#include "engine.h" #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->scenes[engine->curr_scene]->state = SCENE_ENDED;
engine->curr_scene = idx; engine->curr_scene = idx;
engine->scenes[engine->curr_scene]->state = SCENE_PLAYING; 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_map_init_64(&scene->action_map, 32, 0);
sc_array_init(&scene->systems); 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; scene->state = SCENE_ENDED;
} }
void free_scene(Scene_t *scene) void free_scene(Scene_t* scene)
{ {
sc_map_term_64(&scene->action_map); sc_map_term_64(&scene->action_map);
sc_array_term(&scene->systems); sc_array_term(&scene->systems);
free_entity_manager(&scene->ent_manager); free_entity_manager(&scene->ent_manager);
} }
inline void update_scene(Scene_t *scene) inline void update_scene(Scene_t* scene)
{ {
system_func_t sys; system_func_t sys;
sc_array_foreach(&scene->systems, 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); 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); scene->action_function(scene, action, pressed);
} }

View File

@ -7,34 +7,30 @@
typedef struct Scene Scene_t; typedef struct Scene Scene_t;
typedef struct GameEngine typedef struct GameEngine {
{
Scene_t **scenes; Scene_t **scenes;
unsigned int max_scenes; unsigned int max_scenes;
unsigned int curr_scene; unsigned int curr_scene;
Assets_t assets; Assets_t assets;
}GameEngine_t; } GameEngine_t;
void change_scene(GameEngine_t *engine, unsigned int idx); void change_scene(GameEngine_t* engine, unsigned int idx);
typedef enum SceneType typedef enum SceneType {
{
LEVEL_SCENE = 0, LEVEL_SCENE = 0,
MENU_SCENE, MENU_SCENE,
}SceneType_t; }SceneType_t;
typedef enum SceneState typedef enum SceneState {
{
SCENE_PLAYING = 0, SCENE_PLAYING = 0,
SCENE_SUSPENDED, SCENE_SUSPENDED,
SCENE_ENDED, SCENE_ENDED,
}SceneState_t; }SceneState_t;
typedef void(*system_func_t)(Scene_t *); typedef void(*system_func_t)(Scene_t*);
typedef void(*action_func_t)(Scene_t *, ActionType_t, bool); typedef void(*action_func_t)(Scene_t*, ActionType_t, bool);
sc_array_def(system_func_t, systems); sc_array_def(system_func_t, systems);
struct Scene struct Scene {
{
struct sc_map_64 action_map; // key -> actions struct sc_map_64 action_map; // key -> actions
struct sc_array_systems systems; struct sc_array_systems systems;
system_func_t render_function; system_func_t render_function;
@ -47,11 +43,11 @@ struct Scene
}; };
// Inline functions, for convenience // Inline functions, for convenience
extern void update_scene(Scene_t *scene); extern void update_scene(Scene_t* scene);
extern void render_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 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 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 free_scene(Scene_t* scene);
#endif // __ENGINE_H #endif // __ENGINE_H

View File

@ -7,8 +7,7 @@
static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE}; static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE};
static const Vector2 GRAVITY = {0, GRAV_ACCEL}; static const Vector2 GRAVITY = {0, GRAV_ACCEL};
static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 1.1}; static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 1.1};
typedef enum AnchorPoint typedef enum AnchorPoint {
{
AP_TOP_LEFT, AP_TOP_LEFT,
AP_TOP_CENTER, AP_TOP_CENTER,
AP_TOP_RIGHT, AP_TOP_RIGHT,
@ -18,7 +17,7 @@ typedef enum AnchorPoint
AP_BOT_LEFT, AP_BOT_LEFT,
AP_BOT_CENTER, AP_BOT_CENTER,
AP_BOT_RIGHT, AP_BOT_RIGHT,
}AnchorPoint_t; } AnchorPoint_t;
static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width) 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; return tile_y * tilemap_width + tile_x;
} }
typedef struct TileArea typedef struct TileArea {
{
unsigned int tile_x1; unsigned int tile_x1;
unsigned int tile_y1; unsigned int tile_y1;
unsigned int tile_x2; unsigned int tile_x2;
unsigned int tile_y2; unsigned int tile_y2;
}TileArea_t; } TileArea_t;
typedef struct CollideEntity typedef struct CollideEntity {
{
unsigned int idx; unsigned int idx;
Rectangle bbox; Rectangle bbox;
TileArea_t area; TileArea_t area;
}CollideEntity_t; } CollideEntity_t;
// ------------------------- Collision functions ------------------------------------ // ------------------------- 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++) 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 && !p_bbox->fragile)
if (p_bbox->solid) 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; return true;
} }
@ -72,14 +75,15 @@ static bool check_collision(const CollideEntity_t *ent, TileGrid_t* grid, Entity
} }
} }
return false; 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); Vector2 new_pos = Vector2Add(pos, point);
CollideEntity_t ent = CollideEntity_t ent = {
{
.idx = ent_idx, .idx = ent_idx,
.bbox = (Rectangle){new_pos.x, new_pos.y, bbox_sz.x, bbox_sz.y}, .bbox = (Rectangle){new_pos.x, new_pos.y, bbox_sz.x, bbox_sz.y},
.area = (TileArea_t){ .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); 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); 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, static bool check_collision_and_move(
unsigned int ent_idx, CTransform_t *const p_ct, Vector2 sz, 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, Vector2 other_pos, Vector2 other_sz, bool other_solid,
uint32_t* collision_value 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) 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; TileGrid_t tilemap = data->tilemap;
// Deal with player acceleration/velocity via inputs first // Deal with player acceleration/velocity via inputs first
@ -268,9 +276,7 @@ void player_movement_input_system(Scene_t* scene)
unsigned int ent_idx; unsigned int ent_idx;
sc_map_foreach(&scene->ent_manager.component_map[CPLAYERSTATE_T], ent_idx, p_pstate) sc_map_foreach(&scene->ent_manager.component_map[CPLAYERSTATE_T], ent_idx, p_pstate)
{ {
float mag = Vector2Length(p_pstate->player_dir); Entity_t* p_player = get_entity(&scene->ent_manager, ent_idx);
mag = (mag == 0)? 1 : mag;
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); 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); 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); CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T);
@ -308,8 +314,6 @@ void player_movement_input_system(Scene_t* scene)
} }
} }
//else
{
bool hit = false; bool hit = false;
unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE; 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_x2 = (p_ctransform->position.x + p_bbox->size.x - 1) / TILE_SIZE;
@ -346,19 +350,17 @@ void player_movement_input_system(Scene_t* scene)
p_cjump->jump_ready = false; p_cjump->jump_ready = false;
} }
}
p_pstate->player_dir.x = 0; p_pstate->player_dir.x = 0;
p_pstate->player_dir.y = 0; p_pstate->player_dir.y = 0;
} }
} }
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; 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) 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); 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); set_bbox(p_bbox, new_bbox.x, new_bbox.y);
p_ctransform->position = Vector2Add(p_ctransform->position, offset); 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}; 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; 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_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) 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); CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
// Get the occupied tiles // Get the occupied tiles
// For each tile, loop through the entities, check collision and move // 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; unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE;
uint32_t collision_value; 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; unsigned int tile_idx = tile_y * tilemap.width + tile_x;
Vector2 other;
if(tilemap.tiles[tile_idx].solid) if(tilemap.tiles[tile_idx].solid)
{ {
Vector2 other;
other.x = (tile_idx % tilemap.width) * TILE_SIZE; other.x = (tile_idx % tilemap.width) * TILE_SIZE;
other.y = (tile_idx / tilemap.width) * TILE_SIZE; // Precision loss is intentional 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 else
@ -467,7 +478,13 @@ void tile_collision_system(Scene_t *scene)
if (p_other_bbox == NULL) continue; if (p_other_bbox == 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);
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); uint32_t collision_key = ((ent_idx << 16) | other_ent_idx);
sc_map_put_32(&data->collision_events, collision_key, collision_value); 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 // 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 (edges & (1<<3))
{ {
if (p_ctransform->velocity.x < 0) p_ctransform->velocity.x = 0; 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) 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 < 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; 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) 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 < 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; p_ctransform->velocity.y *= -1;
} }
// Deal with float precision, by rounding when it is near to an integer enough by 2 dp // 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; CTransform_t* p_ct;
unsigned long ent_idx; unsigned long ent_idx;
sc_map_foreach(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], ent_idx, p_ct) 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); 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; CMovementState_t* p_mstate;
unsigned long ent_idx; unsigned long ent_idx;
sc_map_foreach(&scene->ent_manager.component_map[CMOVEMENTSTATE_T], ent_idx, p_mstate) 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); 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); 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); CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
if (!(p_mstate->ground_state & 1)) if (!(p_mstate->ground_state & 1))
{ {
// Only apply upthrust if center is in water // 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 // 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 (edges & (1<<3))
{ {
if (p_ctransform->accel.x < 0) p_ctransform->accel.x = 0; 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); 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 // 3 dp precision
if (fabs(p_ctransform->velocity.x) < 1e-3) p_ctransform->velocity.x = 0; 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) 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) 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); 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; //Entity_t* p_ent;
CMovementState_t* p_mstate; CMovementState_t* p_mstate;
unsigned long ent_idx; unsigned long ent_idx;
sc_map_foreach(&scene->ent_manager.component_map[CMOVEMENTSTATE_T], ent_idx, p_mstate) 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); 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); 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); CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
if (p_ctransform == NULL || p_bbox == NULL) continue; 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; bool in_water = false;
if (!(p_mstate->water_state & 1)) 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_y1 = (p_ctransform->position.y) / TILE_SIZE;
unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x) / 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; 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; unsigned int tile_idx = tile_y * data->tilemap.width + tile_x;
in_water |= data->tilemap.tiles[tile_idx].water_level > 0; 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; TileGrid_t tilemap = data->tilemap;
CTileCoord_t *p_tilecoord; CTileCoord_t* p_tilecoord;
unsigned long ent_idx; unsigned long ent_idx;
sc_map_foreach(&scene->ent_manager.component_map[CTILECOORD_COMP_T], ent_idx, p_tilecoord) 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); 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); CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
if (p_ctransform == NULL) continue; 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; if (p_bbox == NULL) continue;
// Update tilemap position // Update tilemap position
for (size_t i=0;i<p_tilecoord->n_tiles;++i) for (size_t i = 0;i < p_tilecoord->n_tiles; ++i)
{ {
// Use previously store tile position // Use previously store tile position
// Clear from those positions // 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_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; 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; unsigned int tile_idx = tile_y * tilemap.width + tile_x;
p_tilecoord->tiles[p_tilecoord->n_tiles++] = tile_idx; 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}; 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; TileGrid_t tilemap = data->tilemap;
unsigned int ent_idx; unsigned int ent_idx;
CHitBoxes_t* p_hitbox; 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) 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); 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); CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
for (uint8_t i=0;i<p_hitbox->n_boxes;++i) for (uint8_t i = 0; i < p_hitbox->n_boxes; ++i)
{ {
Vector2 hitbox_pos = { Vector2 hitbox_pos = {
.x = p_ctransform->position.x + p_hitbox->boxes[i].x, .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_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; 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 tile_idx = tile_y * tilemap.width + tile_x;
unsigned int other_ent_idx; unsigned int other_ent_idx;
@ -838,24 +882,29 @@ void hitbox_update_system(Scene_t *scene)
if (other_ent_idx == ent_idx) continue; if (other_ent_idx == ent_idx) continue;
if (checked_entities[other_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_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 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; 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 hurtbox_pos = Vector2Add(p_other_ct->position, p_other_hurtbox->offset);
Vector2 overlap; 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_hurtbox->fragile) continue;
if (p_other_ent->m_tag == CRATES_ENT_TAG) if (p_other_ent->m_tag == CRATES_ENT_TAG)
{ {
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);
CPlayerState_t * p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T); CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
if ( if (
// TODO: Check Material of the crates // TODO: Check Material of the crates
p_ctransform->position.y + p_bbox->size.y <= p_other_ct->position.y 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); CTileCoord_t* p_tilecoord = get_component(
for (size_t i=0;i<p_tilecoord->n_tiles;++i) &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 // Use previously store tile position
// Clear from those positions // 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; unsigned int ent_idx;
CSprite_t* p_cspr; CSprite_t* p_cspr;
sc_map_foreach(&scene->ent_manager.component_map[CSPRITE_T], ent_idx, 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 // Update animation state
if (p_cspr->transition_func != NULL) 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); 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); sc_map_term_32(&data->collision_events);
} }

View File

@ -1,20 +1,20 @@
#ifndef __GAME_SYSTEMS_H #ifndef __GAME_SYSTEMS_H
#define __GAME_SYSTEMS_H #define __GAME_SYSTEMS_H
#include "scene_impl.h" #include "scene_impl.h"
void init_level_scene_data(LevelSceneData_t *data); void init_level_scene_data(LevelSceneData_t* data);
void term_level_scene_data(LevelSceneData_t *data); void term_level_scene_data(LevelSceneData_t* data);
void player_movement_input_system(Scene_t* scene); void player_movement_input_system(Scene_t* scene);
void player_bbox_update_system(Scene_t *scene); void player_bbox_update_system(Scene_t* scene);
void tile_collision_system(Scene_t *scene); void tile_collision_system(Scene_t* scene);
void friction_coefficient_update_system(Scene_t *scene); void friction_coefficient_update_system(Scene_t* scene);
void global_external_forces_system(Scene_t *scene); void global_external_forces_system(Scene_t* scene);
void movement_update_system(Scene_t* scene); void movement_update_system(Scene_t* scene);
void player_ground_air_transition_system(Scene_t* scene); 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);
void update_tilemap_system(Scene_t *scene); void update_tilemap_system(Scene_t* scene);
void hitbox_update_system(Scene_t *scene); void hitbox_update_system(Scene_t* scene);
void sprite_animation_system(Scene_t *scene); void sprite_animation_system(Scene_t* scene);
unsigned int player_sprite_transition_func(Entity_t* ent); unsigned int player_sprite_transition_func(Entity_t* ent);
#endif // __GAME_SYSTEMS_H #endif // __GAME_SYSTEMS_H

View File

@ -119,7 +119,7 @@ int GuiGetStyle(int control, int property)
return guiStyle[control*(RAYGUI_MAX_PROPS_BASE + RAYGUI_MAX_PROPS_EXTENDED) + 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) #if !defined(ICON_TEXT_PADDING)
#define ICON_TEXT_PADDING 4 #define ICON_TEXT_PADDING 4
@ -178,7 +178,7 @@ static int GetTextWidth(const char *text)
return (int)textSize.x; 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 #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 #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---------// //------- 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)); 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)); GuiDrawText(text, GetTextBounds(BUTTON, comp->bbox), GuiGetStyle(BUTTON, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(BUTTON, TEXT + (comp->state*3))), comp->alpha));

View File

@ -3,14 +3,13 @@
#include "raylib.h" #include "raylib.h"
#include "raygui.h" #include "raygui.h"
typedef struct UIComp typedef struct UIComp {
{
Rectangle bbox; Rectangle bbox;
GuiState state; GuiState state;
float alpha; float alpha;
bool pressed; bool pressed;
}UIComp_t; } UIComp_t;
void init_UI(void); 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 #endif

View File

@ -3,18 +3,18 @@
#include <stdio.h> #include <stdio.h>
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); DrawText("This is a game", 25, 220, 12, BLACK);
UI_button(data->buttons, "Start"); UI_button(data->buttons, "Start");
UI_button(data->buttons + 1, "Continue"); UI_button(data->buttons + 1, "Continue");
UI_button(data->buttons + 2, "Exit"); 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; int new_selection = data->selected_comp;
if (!pressed) 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) 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) if (Vector2LengthSqr(GetMouseDelta()) > 1)
{ {
data->mode = MOUSE_MODE; data->mode = MOUSE_MODE;
@ -74,7 +74,7 @@ static void gui_loop(Scene_t* scene)
else else
{ {
data->buttons[data->selected_comp].state = STATE_NORMAL; data->buttons[data->selected_comp].state = STATE_NORMAL;
for (size_t i=0;i<data->max_comp;i++) for (size_t i = 0;i < data->max_comp; i++)
{ {
if ((data->buttons[i].state != STATE_DISABLED)) 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); init_scene(&scene->scene, MENU_SCENE, &menu_scene_render_func, &menu_do_action);
scene->scene.scene_data = &scene->data; scene->scene.scene_data = &scene->data;
sc_array_add(&scene->scene.systems, &gui_loop); 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}, .bbox = {25,255,125,30},
.state = STATE_NORMAL, .state = STATE_NORMAL,
.alpha = 1.0 .alpha = 1.0
}; };
scene->data.buttons[1] = (UIComp_t) scene->data.buttons[1] = (UIComp_t) {
{
.bbox = {25,300,125,30}, .bbox = {25,300,125,30},
.state = STATE_NORMAL, .state = STATE_NORMAL,
.alpha = 1.0 .alpha = 1.0
}; };
scene->data.buttons[2] = (UIComp_t) scene->data.buttons[2] = (UIComp_t) {
{
.bbox = {25,345,125,30}, .bbox = {25,345,125,30},
.state = STATE_NORMAL, .state = STATE_NORMAL,
.alpha = 1.0 .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); 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); free_scene(&scene->scene);
} }

View File

@ -7,57 +7,50 @@
#include "engine.h" #include "engine.h"
#include "gui.h" #include "gui.h"
typedef struct Tile typedef struct Tile {
{
bool solid; bool solid;
unsigned int water_level; unsigned int water_level;
struct sc_map_64 entities_set; struct sc_map_64 entities_set;
}Tile_t; }Tile_t;
typedef struct TileGrid typedef struct TileGrid {
{
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
unsigned int n_tiles; unsigned int n_tiles;
Tile_t * tiles; Tile_t * tiles;
}TileGrid_t; }TileGrid_t;
typedef struct LevelSceneData typedef struct LevelSceneData {
{
TileGrid_t tilemap; TileGrid_t tilemap;
struct sc_map_32 collision_events; struct sc_map_32 collision_events;
}LevelSceneData_t; }LevelSceneData_t;
typedef struct LevelScene typedef struct LevelScene {
{
Scene_t scene; Scene_t scene;
LevelSceneData_t data; LevelSceneData_t data;
}LevelScene_t; }LevelScene_t;
void init_level_scene(LevelScene_t *scene); void init_level_scene(LevelScene_t* scene);
void free_level_scene(LevelScene_t *scene); void free_level_scene(LevelScene_t* scene);
void reload_level_scene(LevelScene_t *scene); void reload_level_scene(LevelScene_t* scene);
typedef enum GuiMode typedef enum GuiMode {
{
KEYBOARD_MODE, KEYBOARD_MODE,
MOUSE_MODE MOUSE_MODE
}GuiMode_t; } GuiMode_t;
typedef struct MenuSceneData typedef struct MenuSceneData {
{
UIComp_t buttons[3]; UIComp_t buttons[3];
int selected_comp; int selected_comp;
int max_comp; int max_comp;
GuiMode_t mode; GuiMode_t mode;
}MenuSceneData_t; } MenuSceneData_t;
typedef struct MenuScene typedef struct MenuScene {
{
Scene_t scene; Scene_t scene;
MenuSceneData_t data; MenuSceneData_t data;
}MenuScene_t; } MenuScene_t;
void init_menu_scene(MenuScene_t *scene); void init_menu_scene(MenuScene_t* scene);
void free_menu_scene(MenuScene_t *scene); void free_menu_scene(MenuScene_t* scene);
#endif // __SCENE_IMPL_H #endif // __SCENE_IMPL_H

7
main.c
View File

@ -4,8 +4,7 @@
#define N_SCENES 3 #define N_SCENES 3
Scene_t *scenes[N_SCENES]; Scene_t *scenes[N_SCENES];
static GameEngine_t engine = static GameEngine_t engine = {
{
.scenes = scenes, .scenes = scenes,
.max_scenes = 2, .max_scenes = 2,
.curr_scene = 0 .curr_scene = 0
@ -47,11 +46,11 @@ int main(void)
{ {
// This entire key processing relies on the assumption that a pressed key will // This entire key processing relies on the assumption that a pressed key will
// appear in the polling of raylib // 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); unsigned int sz = sc_queue_size(&key_buffer);
// Process any existing pressed key // Process any existing pressed key
for (size_t i=0; i<sz; i++) for (size_t i = 0; i < sz; i++)
{ {
int button = sc_queue_del_first(&key_buffer); int button = sc_queue_del_first(&key_buffer);
ActionType_t action = sc_map_get_64(&curr_scene->action_map, button); ActionType_t action = sc_map_get_64(&curr_scene->action_map, button);

View File

@ -22,7 +22,7 @@ int main(void)
unsigned int sz = sc_queue_size(&key_buffer); unsigned int sz = sc_queue_size(&key_buffer);
// Process any existing pressed key // Process any existing pressed key
for (size_t i=0; i<sz; i++) for (size_t i = 0; i < sz; i++)
{ {
int button = sc_queue_del_first(&key_buffer); int button = sc_queue_del_first(&key_buffer);
ActionType_t action = sc_map_get_64(&scene.scene.action_map, button); ActionType_t action = sc_map_get_64(&scene.scene.action_map, button);

View File

@ -6,7 +6,7 @@
// Maintain own queue to handle key presses // Maintain own queue to handle key presses
struct sc_queue_32 key_buffer; struct sc_queue_32 key_buffer;
Scene_t *scenes[1]; Scene_t* scenes[1];
static GameEngine_t engine = static GameEngine_t engine =
{ {
.scenes = scenes, .scenes = scenes,
@ -48,7 +48,7 @@ int main(void)
unsigned int sz = sc_queue_size(&key_buffer); unsigned int sz = sc_queue_size(&key_buffer);
// Process any existing pressed key // Process any existing pressed key
for (size_t i=0; i<sz; i++) for (size_t i = 0; i < sz; i++)
{ {
int button = sc_queue_del_first(&key_buffer); int button = sc_queue_del_first(&key_buffer);
ActionType_t action = sc_map_get_64(&scene.scene.action_map, button); ActionType_t action = sc_map_get_64(&scene.scene.action_map, button);