Add Sprite Component
Changelog: - Move Sprite struct into components - Add asset field in engine - Update scene test code to add engine for assets - This fixes the crash when q is pressed - Add sprite component to player - Update render function to draw the spritescene_man
parent
1ea1db3c60
commit
ac6d93565b
|
@ -4,8 +4,8 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
// 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 9
|
#define N_COMPONENTS 10
|
||||||
enum ComponentEnum
|
typedef enum ComponentEnum
|
||||||
{
|
{
|
||||||
CBBOX_COMP_T,
|
CBBOX_COMP_T,
|
||||||
CTRANSFORM_COMP_T,
|
CTRANSFORM_COMP_T,
|
||||||
|
@ -16,8 +16,8 @@ enum ComponentEnum
|
||||||
CCONTAINER_T,
|
CCONTAINER_T,
|
||||||
CHITBOXES_T,
|
CHITBOXES_T,
|
||||||
CHURTBOX_T,
|
CHURTBOX_T,
|
||||||
};
|
CSPRITE_T,
|
||||||
typedef enum ComponentEnum ComponentEnum_t;
|
}ComponentEnum_t;
|
||||||
|
|
||||||
typedef struct _CBBox_t
|
typedef struct _CBBox_t
|
||||||
{
|
{
|
||||||
|
@ -112,6 +112,25 @@ typedef struct _CHurtbox_t
|
||||||
bool fragile;
|
bool fragile;
|
||||||
}CHurtbox_t;
|
}CHurtbox_t;
|
||||||
|
|
||||||
|
// Credits to bedroomcoders.co.uk for this
|
||||||
|
typedef struct Sprite
|
||||||
|
{
|
||||||
|
Texture2D* texture;
|
||||||
|
Vector2 frame_size;
|
||||||
|
Vector2 origin;
|
||||||
|
int frame_count;
|
||||||
|
int current_frame;
|
||||||
|
int elapsed;
|
||||||
|
int speed;
|
||||||
|
char* name;
|
||||||
|
}Sprite_t;
|
||||||
|
|
||||||
|
typedef struct _CSprite_t
|
||||||
|
{
|
||||||
|
Sprite_t* sprite;
|
||||||
|
Vector2 offset;
|
||||||
|
}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)
|
||||||
{
|
{
|
||||||
p_bbox->size.x = x;
|
p_bbox->size.x = x;
|
||||||
|
|
|
@ -14,13 +14,8 @@ static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have
|
||||||
static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE];
|
static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE];
|
||||||
static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE];
|
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];
|
||||||
|
|
||||||
// Use hashmap as a Set
|
|
||||||
// Use list will be used to check if an object exist
|
|
||||||
// The alternative method to check the free list if idx is not there
|
|
||||||
// requires bound checking
|
|
||||||
// It's just easier on the mind overall
|
|
||||||
// If need to optimise memory, replace free_list with set and remove use_list
|
|
||||||
typedef struct MemPool
|
typedef struct MemPool
|
||||||
{
|
{
|
||||||
void * const buffer;
|
void * const buffer;
|
||||||
|
@ -42,6 +37,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] =
|
||||||
{ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}},
|
{ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}},
|
||||||
{chitboxes_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBoxes_t), NULL, {0}},
|
{chitboxes_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBoxes_t), NULL, {0}},
|
||||||
{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}},
|
||||||
};
|
};
|
||||||
static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), NULL, {0}};
|
static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), NULL, {0}};
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,9 @@
|
||||||
#ifndef __ASSETS_H
|
#ifndef __ASSETS_H
|
||||||
#define __ASSETS_H
|
#define __ASSETS_H
|
||||||
#include "sc/map/sc_map.h"
|
#include "sc/map/sc_map.h"
|
||||||
|
#include "components.h"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
// Credits to bedroomcoders.co.uk for this
|
|
||||||
typedef struct Sprite
|
|
||||||
{
|
|
||||||
Texture2D* texture;
|
|
||||||
Vector2 frame_size;
|
|
||||||
Vector2 origin;
|
|
||||||
int frame_count;
|
|
||||||
int current_frame;
|
|
||||||
int elapsed;
|
|
||||||
int speed;
|
|
||||||
char* name;
|
|
||||||
}Sprite_t;
|
|
||||||
|
|
||||||
typedef struct Assets
|
typedef struct Assets
|
||||||
{
|
{
|
||||||
struct sc_map_s64 m_textures;
|
struct sc_map_s64 m_textures;
|
||||||
|
|
|
@ -92,6 +92,12 @@ static void level_scene_render_func(Scene_t* scene)
|
||||||
};
|
};
|
||||||
DrawRectangleLinesEx(rec, 1.5, PURPLE);
|
DrawRectangleLinesEx(rec, 1.5, PURPLE);
|
||||||
}
|
}
|
||||||
|
CSprite_t* p_cspr = get_component(&scene->ent_manager, p_ent, CSPRITE_T);
|
||||||
|
if (p_cspr != NULL)
|
||||||
|
{
|
||||||
|
Vector2 pos = Vector2Add(p_ct->position, p_cspr->offset);
|
||||||
|
draw_sprite(p_cspr->sprite, pos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i=0; i<tilemap.n_tiles;++i)
|
for (size_t i=0; i<tilemap.n_tiles;++i)
|
||||||
|
@ -204,6 +210,8 @@ static void spawn_player(Scene_t *scene)
|
||||||
.width = p_bbox->size.x + 2,
|
.width = p_bbox->size.x + 2,
|
||||||
.height = p_bbox->size.y - 1,
|
.height = p_bbox->size.y - 1,
|
||||||
};
|
};
|
||||||
|
CSprite_t* p_cspr = add_component(&scene->ent_manager, p_ent, CSPRITE_T);
|
||||||
|
p_cspr->sprite = get_sprite(&scene->engine->assets, "plr_stand");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void toggle_block_system(Scene_t *scene)
|
static void toggle_block_system(Scene_t *scene)
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
#include "entManager.h"
|
#include "entManager.h"
|
||||||
#include "actions.h"
|
#include "actions.h"
|
||||||
#include "sc/array/sc_array.h"
|
#include "sc/array/sc_array.h"
|
||||||
|
#include "assets.h"
|
||||||
|
|
||||||
typedef struct Scene Scene_t;
|
typedef struct Scene Scene_t;
|
||||||
|
|
||||||
typedef struct GameEngine
|
typedef struct GameEngine
|
||||||
|
@ -10,6 +12,7 @@ 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;
|
||||||
}GameEngine_t;
|
}GameEngine_t;
|
||||||
void change_scene(GameEngine_t *engine, unsigned int idx);
|
void change_scene(GameEngine_t *engine, unsigned int idx);
|
||||||
|
|
||||||
|
|
26
scene_test.c
26
scene_test.c
|
@ -6,14 +6,40 @@
|
||||||
// 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];
|
||||||
|
static GameEngine_t engine =
|
||||||
|
{
|
||||||
|
.scenes = scenes,
|
||||||
|
.max_scenes = 1,
|
||||||
|
.curr_scene = 0,
|
||||||
|
.assets = {0}
|
||||||
|
};
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
sc_queue_init(&key_buffer);
|
sc_queue_init(&key_buffer);
|
||||||
InitWindow(1280, 640, "raylib");
|
InitWindow(1280, 640, "raylib");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
init_memory_pools();
|
init_memory_pools();
|
||||||
|
|
||||||
|
init_assets(&engine.assets);
|
||||||
|
Texture2D* tex = add_texture(&engine.assets, "plr_tex", "res/test_tex.png");
|
||||||
|
Sprite_t* spr = add_sprite(&engine.assets, "plr_stand", tex);
|
||||||
|
spr->origin = (Vector2){0, 0};
|
||||||
|
spr->frame_size = (Vector2){32, 32};
|
||||||
|
|
||||||
|
spr = add_sprite(&engine.assets, "plr_run", tex);
|
||||||
|
spr->frame_count = 4;
|
||||||
|
spr->origin = (Vector2){0, 0};
|
||||||
|
spr->frame_size = (Vector2){32, 32};
|
||||||
|
spr->speed = 15;
|
||||||
|
|
||||||
LevelScene_t scene;
|
LevelScene_t scene;
|
||||||
|
scene.scene.engine = &engine;
|
||||||
init_level_scene(&scene);
|
init_level_scene(&scene);
|
||||||
|
scenes[0] = &scene.scene;
|
||||||
|
change_scene(&engine, 0);
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue