Compare commits
2 Commits
98450d0bba
...
e1a7774bfb
Author | SHA1 | Date |
---|---|---|
|
e1a7774bfb | |
|
b0e1d33ad6 |
|
@ -1,5 +1,6 @@
|
|||
#include "mempool.h"
|
||||
#include "scene_impl.h"
|
||||
#include "ent_impl.h"
|
||||
#include "assets_loader.h"
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
@ -25,6 +26,7 @@ int main(void)
|
|||
|
||||
init_assets(&engine.assets);
|
||||
load_from_infofile("res/assets.info", &engine.assets);
|
||||
init_player_creation("res/player_spr.info", &engine.assets);
|
||||
|
||||
LevelScene_t scene;
|
||||
scene.scene.engine = &engine;
|
||||
|
@ -34,6 +36,7 @@ int main(void)
|
|||
scenes[0] = &scene.scene;
|
||||
change_scene(&engine, 0);
|
||||
|
||||
|
||||
while(true)
|
||||
{
|
||||
|
||||
|
|
|
@ -42,9 +42,8 @@ bool load_from_infofile(const char* file, Assets_t* assets)
|
|||
return false;
|
||||
}
|
||||
|
||||
static char buffer[256];
|
||||
char buffer[256];
|
||||
char* tmp;
|
||||
char tmp2[32];
|
||||
size_t line_num = 0;
|
||||
AssetInfoType_t info_type = INVALID_INFO;
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#include "entity.h"
|
||||
// TODO: Look at sc to use macros to auto generate functions
|
||||
|
||||
#define N_COMPONENTS 10
|
||||
typedef enum ComponentEnum {
|
||||
CBBOX_COMP_T,
|
||||
CTRANSFORM_COMP_T,
|
||||
|
|
|
@ -21,20 +21,19 @@ void update_entity_manager(EntityManager_t* p_manager)
|
|||
// It does not make new entities, but will free entity
|
||||
// New entities are assigned during add_entity
|
||||
unsigned long e_idx;
|
||||
unsigned long comp_type_idx;
|
||||
unsigned long comp_idx;
|
||||
|
||||
sc_queue_foreach (&p_manager->to_remove, e_idx)
|
||||
{
|
||||
Entity_t *p_entity = (Entity_t *)sc_map_get_64v(&p_manager->entities, e_idx);
|
||||
if (!p_entity) continue;
|
||||
sc_map_foreach (&p_entity->components, comp_type_idx, comp_idx)
|
||||
for (size_t i = 0; i < N_COMPONENTS; ++i)
|
||||
{
|
||||
free_component_to_mempool((ComponentEnum_t)comp_type_idx, comp_idx);
|
||||
sc_map_del_64v(&p_manager->component_map[comp_type_idx], e_idx);
|
||||
if (p_entity->components[i] == MAX_COMP_POOL_SIZE) continue;
|
||||
|
||||
free_component_to_mempool((ComponentEnum_t)i, p_entity->components[i]);
|
||||
sc_map_del_64v(&p_manager->component_map[i], e_idx);
|
||||
sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx);
|
||||
}
|
||||
sc_map_clear_64(&p_entity->components);
|
||||
free_entity_to_mempool(e_idx);
|
||||
sc_map_del_64v(&p_manager->entities, e_idx);
|
||||
}
|
||||
|
@ -116,7 +115,7 @@ void* add_component(EntityManager_t* p_manager, Entity_t* p_entity, ComponentEnu
|
|||
void* p_comp = new_component_from_mempool(comp_type, &comp_idx);
|
||||
if (p_comp)
|
||||
{
|
||||
sc_map_put_64(&p_entity->components, comp_type_idx, comp_idx);
|
||||
p_entity->components[comp_type] = comp_idx;
|
||||
sc_map_put_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id, p_comp);
|
||||
}
|
||||
return p_comp;
|
||||
|
@ -126,7 +125,6 @@ void* get_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnu
|
|||
{
|
||||
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);
|
||||
//unsigned long comp_idx = sc_map_get_64(&p_entity->components, comp_type_idx);
|
||||
if (!sc_map_found(&p_manager->component_map[comp_type_idx])) return NULL;
|
||||
return p_comp;
|
||||
}
|
||||
|
@ -134,8 +132,7 @@ void* get_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnu
|
|||
void remove_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type)
|
||||
{
|
||||
unsigned long comp_type_idx = (unsigned long)comp_type;
|
||||
unsigned long comp_idx = sc_map_del_64(&p_entity->components, comp_type_idx);
|
||||
if (!sc_map_found(&p_entity->components)) return;
|
||||
if (p_entity->components[comp_type] == MAX_COMP_POOL_SIZE) return;
|
||||
sc_map_del_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id);
|
||||
free_component_to_mempool(comp_type, comp_idx);
|
||||
free_component_to_mempool(comp_type, p_entity->components[comp_type]);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef __ENTITY_H
|
||||
#define __ENTITY_H
|
||||
#include <stdbool.h>
|
||||
#include "sc/map/sc_map.h"
|
||||
|
||||
#define N_TAGS 4
|
||||
#define N_COMPONENTS 10
|
||||
typedef enum EntityTag {
|
||||
NO_ENT_TAG,
|
||||
PLAYER_ENT_TAG,
|
||||
|
@ -15,6 +15,7 @@ typedef struct Entity {
|
|||
unsigned long m_id;
|
||||
EntityTag_t m_tag;
|
||||
bool m_alive;
|
||||
struct sc_map_64 components;
|
||||
unsigned long components[N_COMPONENTS];
|
||||
|
||||
} Entity_t;
|
||||
#endif // __ENTITY_H
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
// Static allocate buffers
|
||||
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
|
||||
|
@ -118,7 +119,6 @@ void init_memory_pools(void)
|
|||
for (size_t i = 0; i < ent_mempool.max_size; ++i)
|
||||
{
|
||||
entity_buffer[i].m_id = i;
|
||||
sc_map_init_64(&entity_buffer[i].components, 16 ,0);
|
||||
ent_mempool.free_list.buffer[i] = i;
|
||||
}
|
||||
ent_mempool.free_list.count = ent_mempool.max_size;
|
||||
|
@ -138,10 +138,6 @@ void free_memory_pools(void)
|
|||
free(ent_mempool.use_list);
|
||||
cb_free(&ent_mempool.free_list);
|
||||
|
||||
for (int i = 0; i < MAX_COMP_POOL_SIZE; ++i)
|
||||
{
|
||||
sc_map_term_64(&entity_buffer[i].components);
|
||||
}
|
||||
pool_inited = false;
|
||||
}
|
||||
}
|
||||
|
@ -154,7 +150,10 @@ Entity_t* new_entity_from_mempool(unsigned long* e_idx_ptr)
|
|||
*e_idx_ptr = e_idx;
|
||||
ent_mempool.use_list[e_idx] = true;
|
||||
Entity_t* ent = entity_buffer + e_idx;
|
||||
sc_map_clear_64(&ent->components);
|
||||
for (size_t j = 0; j< N_COMPONENTS; j++)
|
||||
{
|
||||
ent->components[j] = MAX_COMP_POOL_SIZE;
|
||||
}
|
||||
ent->m_alive = true;
|
||||
ent->m_tag = NO_ENT_TAG;
|
||||
return ent;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "entManager.h"
|
||||
#include "assets.h"
|
||||
|
||||
bool init_player_creation(const char* info_file, Assets_t* assets);
|
||||
Entity_t* create_player(EntityManager_t* ent_manager, Assets_t* assets);
|
||||
Entity_t* create_crate(EntityManager_t* ent_manager, Assets_t* assets, bool metal);
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "ent_impl.h"
|
||||
#include "constants.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define N_PLAYER_SPRITES 2
|
||||
enum PlayerSpriteEnum
|
||||
|
@ -47,11 +49,58 @@ Entity_t* create_player(EntityManager_t* ent_manager, Assets_t* assets)
|
|||
};
|
||||
CSprite_t* p_cspr = add_component(ent_manager, p_ent, CSPRITE_T);
|
||||
p_cspr->sprites = player_sprite_map;
|
||||
p_cspr->sprites[0].sprite = get_sprite(assets, "plr_stand");
|
||||
p_cspr->sprites[0].offset = (Vector2){0, -20};
|
||||
p_cspr->sprites[1].sprite = get_sprite(assets, "plr_stand");
|
||||
p_cspr->sprites[1].offset = (Vector2){0, -20};
|
||||
p_cspr->transition_func = &player_sprite_transition_func;
|
||||
|
||||
return p_ent;
|
||||
}
|
||||
|
||||
bool init_player_creation(const char* info_file, Assets_t* assets)
|
||||
{
|
||||
static bool already_init = false;
|
||||
|
||||
if (already_init) return false;
|
||||
|
||||
FILE* in_file = fopen(info_file, "r");
|
||||
if (in_file == NULL)
|
||||
{
|
||||
printf("Unable to open file %s\n", info_file);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
char* tmp;
|
||||
size_t line_num = 0;
|
||||
uint8_t i = 0;
|
||||
while (true)
|
||||
{
|
||||
tmp = fgets(buffer, 256, in_file);
|
||||
if (tmp == NULL) break;
|
||||
tmp[strcspn(tmp, "\r\n")] = '\0';
|
||||
|
||||
if (i == N_PLAYER_SPRITES) break;
|
||||
|
||||
char* name = strtok(buffer, ":");
|
||||
char* info_str = strtok(NULL, ":");
|
||||
if (name == NULL || info_str == NULL) return false;
|
||||
|
||||
while(*name == ' ' || *name == '\t') name++;
|
||||
while(*info_str == ' ' || *info_str == '\t') info_str++;
|
||||
|
||||
Vector2 offset;
|
||||
int data_count = sscanf(
|
||||
info_str, "%f,%f",
|
||||
&offset.x, &offset.y
|
||||
);
|
||||
if (data_count !=2)
|
||||
{
|
||||
printf("Unable to parse info for player at line %lu\n", line_num);
|
||||
return false;
|
||||
}
|
||||
Sprite_t* spr = get_sprite(assets, name);
|
||||
player_sprite_map[i].sprite = spr;
|
||||
player_sprite_map[i].offset = offset;
|
||||
i++;
|
||||
}
|
||||
already_init = true;
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue