Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ECS.h"
- ECS::ECS()
- {
- Entities.push_back(0);
- FreeEntitiesList.push_back(0);
- EntitiesCount = 0;
- //-- Set Component list masks --//
- InfoComps.Mask = COMP_INFO;
- PositionComps.Mask = COMP_POSITION;
- SpeedComps.Mask = COMP_SPEED;
- SpriteComps.Mask = COMP_SPRITE;
- //-- Store a pointer to each component list --//
- ComponentLists.push_back((ComponentList<CompGeneric>*)&InfoComps);
- ComponentLists.push_back((ComponentList<CompGeneric>*)&PositionComps);
- ComponentLists.push_back((ComponentList<CompGeneric>*)&SpeedComps);
- ComponentLists.push_back((ComponentList<CompGeneric>*)&SpriteComps);
- };
- ECS::~ECS()
- {
- };
- //----------------------------------------------------
- //----- Return the index of a blank entity -----------
- //----------------------------------------------------
- int ECS::EntityCreate()
- {
- int newEntityId = -1;
- if (FreeEntitiesList.size() == 0)
- {
- newEntityId = EntitiesCount;
- EntitiesCount++;
- }
- else
- {
- newEntityId = FreeEntitiesList[FreeEntitiesList.back()];
- FreeEntitiesList.pop_back();
- }
- if (Entities.size() < EntitiesCount)
- Entities.push_back(0);
- return newEntityId;
- };
- //---------------------------------------------------------
- //----- 'Destroy' an entity by setting the mask to blank --
- //---------------------------------------------------------
- void ECS::EntityDestroy(int EntityId)
- {
- for (unsigned int i = 0; i < ComponentLists.size(); i++) // Free all attached components.
- {
- int mappedIndex = ComponentLists[i]->Map[EntityId];
- if (mappedIndex >= 0)
- ComponentLists[i]->Unused.push_back(mappedIndex);
- }
- Entities[EntityId] = COMP_NONE; // Blank out the entity mask.
- FreeEntitiesList.push_back(EntityId); // Add index to free list.
- }
- //--------------------------------------------------------------------------
- //----- RENDER SYSTEM: Draw entities with position ans sprite components ---
- //--------------------------------------------------------------------------
- void ECS::SystemRender(SDL_Renderer* Renderer)
- {
- int64_t renderSysMask = COMP_SPRITE | COMP_POSITION;
- for (int i = 0; i < Entities.size(); i++)
- {
- if ((Entities[i] & renderSysMask) == renderSysMask) // Check entity can be rendered.
- {
- CompPosition* entityPosition = &PositionComps.List[PositionComps.Map[i]]; // Get needed components.
- CompSprite* entitySprite = &SpriteComps.List[SpriteComps.Map[i]];
- SDL_RenderClear(Renderer);
- SDL_RenderCopy(Renderer, entitySprite->Sprite.Image, NULL, NULL); // Render the entity.
- SDL_RenderPresent(Renderer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement