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,
@ -20,8 +19,7 @@ typedef enum ComponentEnum
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;
@ -29,8 +27,7 @@ typedef struct _CBBox_t
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;
@ -38,8 +35,7 @@ typedef struct _CTransform_t
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;
@ -47,14 +43,12 @@ typedef struct _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;
@ -63,21 +57,18 @@ typedef struct _CJump_t
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,
@ -87,35 +78,30 @@ typedef enum ContainerItem
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;
@ -127,8 +113,7 @@ typedef struct Sprite
} 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;

View File

@ -14,7 +14,7 @@ void init_entity_manager(EntityManager_t *p_manager)
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
@ -46,7 +46,6 @@ 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)

View File

@ -4,8 +4,7 @@
#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}]

View File

@ -4,16 +4,14 @@
#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;

View File

@ -16,8 +16,7 @@ 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;
@ -26,8 +25,7 @@ typedef struct MemPool
} 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,7 +37,13 @@ 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)
@ -91,11 +95,11 @@ 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);
@ -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,7 +6,7 @@
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);

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);

View File

@ -17,10 +17,10 @@ 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);

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

@ -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,
@ -167,9 +166,11 @@ 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;
@ -183,10 +184,11 @@ static void spawn_crate(Scene_t *scene, unsigned int tile_idx, bool 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;
@ -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,

View File

@ -6,6 +6,7 @@ void change_scene(GameEngine_t *engine, unsigned int idx)
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);

View File

@ -7,8 +7,7 @@
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;
@ -16,14 +15,12 @@ typedef struct GameEngine
} 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,
@ -33,8 +30,7 @@ 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;

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,
@ -27,16 +26,14 @@ 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;
@ -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
) )
@ -268,8 +276,6 @@ 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);
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); 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);
@ -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,8 +350,6 @@ 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;
} }
@ -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);
@ -441,13 +448,17 @@ void tile_collision_system(Scene_t *scene)
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
@ -594,6 +628,7 @@ void global_external_forces_system(Scene_t *scene)
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;
@ -720,7 +761,10 @@ void state_transition_update_system(Scene_t *scene)
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))
{ {
@ -809,7 +853,7 @@ void hitbox_update_system(Scene_t *scene)
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);
@ -848,7 +892,12 @@ void hitbox_update_system(Scene_t *scene)
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)
@ -871,7 +920,10 @@ 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(
&scene->ent_manager, p_other_ent, CTILECOORD_COMP_T
);
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

View File

@ -3,8 +3,7 @@
#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;

View File

@ -110,7 +110,6 @@ static void gui_loop(Scene_t* scene)
} }
} }
} }
} }
} }
@ -121,21 +120,18 @@ void init_menu_scene(MenuScene_t *scene)
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

View File

@ -7,29 +7,25 @@
#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;
@ -38,22 +34,19 @@ 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;

3
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