Advertisement
Guest User

ECS.cpp

a guest
Dec 11th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include "ECS.h"
  2.  
  3. ECS::ECS()
  4. {
  5.     Entities.push_back(0);
  6.     FreeEntitiesList.push_back(0);
  7.     EntitiesCount = 0;
  8.  
  9.     //-- Set Component list masks --//
  10.     InfoComps.Mask     = COMP_INFO;
  11.     PositionComps.Mask = COMP_POSITION;
  12.     SpeedComps.Mask    = COMP_SPEED;
  13.     SpriteComps.Mask   = COMP_SPRITE;
  14.  
  15.     //-- Store a pointer to each component list --//
  16.     ComponentLists.push_back((ComponentList<CompGeneric>*)&InfoComps);
  17.     ComponentLists.push_back((ComponentList<CompGeneric>*)&PositionComps);
  18.     ComponentLists.push_back((ComponentList<CompGeneric>*)&SpeedComps);
  19.     ComponentLists.push_back((ComponentList<CompGeneric>*)&SpriteComps);
  20. };
  21.  
  22. ECS::~ECS()
  23. {
  24. };
  25.  
  26. //----------------------------------------------------
  27. //----- Return the index of a blank entity -----------
  28. //----------------------------------------------------
  29. int ECS::EntityCreate()
  30. {
  31.     int newEntityId = -1;
  32.  
  33.     if (FreeEntitiesList.size() == 0)
  34.         {
  35.         newEntityId = EntitiesCount;
  36.         EntitiesCount++;
  37.         }
  38.     else
  39.         {
  40.         newEntityId = FreeEntitiesList[FreeEntitiesList.back()];
  41.         FreeEntitiesList.pop_back();
  42.         }
  43.  
  44.     if (Entities.size() < EntitiesCount)
  45.         Entities.push_back(0);
  46.  
  47.     return newEntityId;
  48. };
  49.  
  50. //---------------------------------------------------------
  51. //----- 'Destroy' an entity by setting the mask to blank --
  52. //---------------------------------------------------------
  53. void ECS::EntityDestroy(int EntityId)
  54. {
  55.     for (unsigned int i = 0; i < ComponentLists.size(); i++) // Free all attached components.
  56.         {
  57.         int mappedIndex = ComponentLists[i]->Map[EntityId];
  58.         if (mappedIndex >= 0)
  59.             ComponentLists[i]->Unused.push_back(mappedIndex);
  60.         }
  61.  
  62.     Entities[EntityId] = COMP_NONE; // Blank out the entity mask.
  63.     FreeEntitiesList.push_back(EntityId); // Add index to free list.
  64. }
  65.  
  66. //--------------------------------------------------------------------------
  67. //----- RENDER SYSTEM: Draw entities with position ans sprite components ---
  68. //--------------------------------------------------------------------------
  69. void ECS::SystemRender(SDL_Renderer* Renderer)
  70. {
  71.     int64_t renderSysMask = COMP_SPRITE | COMP_POSITION;
  72.  
  73.     for (int i = 0; i < Entities.size(); i++)
  74.     {
  75.         if ((Entities[i] & renderSysMask) == renderSysMask) // Check entity can be rendered.
  76.         {
  77.             CompPosition* entityPosition = &PositionComps.List[PositionComps.Map[i]]; // Get needed components.
  78.             CompSprite* entitySprite = &SpriteComps.List[SpriteComps.Map[i]];
  79.  
  80.             SDL_RenderClear(Renderer);                                        
  81.             SDL_RenderCopy(Renderer, entitySprite->Sprite.Image, NULL, NULL); // Render the entity.
  82.             SDL_RenderPresent(Renderer);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement