Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <typeinfo>
- //#include <map>
- //#include <unordered_map>
- #include "map.h"
- namespace world
- {
- struct EntityHandle
- {
- size_t handle;
- };
- struct ComponentHandle
- {
- size_t handle;
- };
- struct ComponentHandleData
- {
- size_t type_hash;
- ComponentHandle handle;
- };
- struct EntityInfo
- {
- Map handle_map;
- };
- struct ComponentStorage
- {
- void *data;
- size_t count;
- };
- struct World
- {
- EntityInfo *entity_infos;
- size_t entity_count;
- size_t entity_max;
- Map storages;
- //std::map<size_t, ComponentStorage> storages;
- };
- World create_world(size_t entity_max)
- {
- World world = {};
- world.entity_count = 0;
- world.entity_infos = (EntityInfo*)calloc(entity_max, sizeof(EntityInfo));
- world.entity_max = entity_max;
- map_init(&world.storages, int_hash, ptr_cmp);
- return world;
- }
- EntityHandle create_entity(World *world)
- {
- EntityInfo info = {};
- map_init(&info.handle_map, int_hash, ptr_cmp);
- world->entity_infos[world->entity_count++] = info;
- return { world->entity_count };
- }
- template <typename T>
- void register_component(World *world)
- {
- const std::type_info &type_info = typeid(T);
- size_t hash = type_info.hash_code();
- ComponentStorage *storage = (ComponentStorage*)malloc(sizeof(ComponentStorage));
- storage->count = 0;
- storage->data = malloc(sizeof(T) * world->entity_max);
- map_put(&world->storages, hash, storage);
- }
- template <typename T>
- void add_component(World *world, EntityHandle entity, T value)
- {
- const std::type_info &type_info = typeid(T);
- size_t hash = type_info.hash_code();
- if(auto *storage = (ComponentStorage*)map_get(&world->storages, hash))
- {
- T* data = (T*)storage->data;
- data[storage->count++] = value;
- EntityInfo *entity_info = &world->entity_infos[entity.handle - 1];
- ComponentHandleData *handle_data = (ComponentHandleData*)malloc(sizeof(ComponentHandleData));
- handle_data->type_hash = hash;
- handle_data->handle = {storage->count};
- map_put(&entity_info->handle_map, hash, handle_data);
- }
- }
- template <typename T>
- T * get_component(World *world, EntityHandle entity)
- {
- const std::type_info &type_info = typeid(T);
- size_t hash = type_info.hash_code();
- EntityInfo &info = world->entity_infos[entity.handle - 1];
- if(auto *handle_data = (ComponentHandleData*)map_get(&info.handle_map, hash))
- {
- ComponentHandle component_handle = handle_data->handle;
- ComponentStorage *storage = (ComponentStorage*)map_get(&world->storages, hash);
- T* data = (T*)storage->data;
- return &data[component_handle.handle - 1];
- }
- return nullptr;
- }
- struct Vector3
- {
- float x;
- float y;
- float z;
- };
- struct Quaternion
- {
- float x;
- float y;
- float z;
- float w;
- };
- struct Transform
- {
- Vector3 position;
- Vector3 scale;
- Quaternion orientation;
- };
- struct Renderer
- {};
- void run_test()
- {
- World world = create_world(1024);
- //register_type(Transform, (&world));
- register_component<Transform>(&world);
- register_component<Renderer>(&world);
- EntityHandle entity = create_entity((&world));
- Transform transform;
- transform.position = Vector3 { 10.0f, 0.0f, 0.0f };
- add_component(&world, entity, transform);
- Transform *the_returned_transform = get_component<Transform>(&world, entity);
- printf("Position x: %f y: %f z: %f\n", the_returned_transform->position.x, the_returned_transform->position.y, the_returned_transform->position.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement