Created
August 23, 2013 10:22
-
-
Save vittorioromeo/6317778 to your computer and use it in GitHub Desktop.
preallocation fails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2013 Vittorio Romeo | |
// License: Academic Free License ("AFL") v. 3.0 | |
// AFL License page: http://opensource.org/licenses/AFL-3.0 | |
#ifndef SSES_ENTITY | |
#define SSES_ENTITY | |
#include "SSVEntitySystem/Core/Manager.h" | |
#include "SSVEntitySystem/Core/Component.h" | |
#include "SSVEntitySystem/Global/Typedefs.h" | |
#include <functional> | |
namespace sses | |
{ | |
class Entity : public ssvu::MemoryManageable | |
{ | |
private: | |
Manager& manager; | |
Bitset groups; | |
std::vector<Component*> components; | |
int drawPriority{0}; | |
public: | |
Entity(Manager& mManager) : manager(mManager) { } | |
Entity(const Entity&) = delete; // non construction-copyable | |
Entity& operator=(const Entity&) = delete; // non copyable | |
~Entity() { for(const auto& c : components) manager.preAllocator.destroy(c, c->mysize); } | |
inline void addGroup(Group mGroup) { groups.set(mGroup); manager.addToGroup(this, mGroup); } | |
inline void delGroup(Group mGroup) { groups.set(mGroup, false); manager.delFromGroup(this, mGroup); } | |
inline bool hasGroup(Group mGroup) const { return groups.test(mGroup); } | |
inline bool hasAnyGroup(const Bitset& mGroups) const { return (groups & mGroups).any(); } | |
inline void clearGroups() { for(Group i{0}; i < groups.size(); ++i) if(groups.test(i)) manager.delFromGroup(this, i); groups.reset(); } | |
inline const Bitset& getGroups() const { return groups; } | |
inline void update(float mFrameTime) { for(const auto& c : components) c->update(mFrameTime); } | |
inline void draw() { for(const auto& c : components) c->draw(); } | |
inline void destroy() { manager.del(*this); } | |
inline void setDrawPriority(int mDrawPriority) { drawPriority = mDrawPriority; } | |
inline Manager& getManager() const { return manager; } | |
inline int getDrawPriority() const { return drawPriority; } | |
inline decltype(components)& getComponents() { return components; } | |
template<typename T> inline T* getComponentSafe() const { for(const auto& c : components) if(getTypeId<T>() == c->getId()) return static_cast<T*>(c); return nullptr; } | |
template<typename T> inline T& getComponent() const { return *getComponentSafe<T>(); } | |
template<typename T> inline std::size_t getComponentCount() const { std::size_t result{0}; for(const auto& c : components) if(getTypeId<T>() == c->getId()) ++result; return result; } | |
template<typename T> inline bool hasComponent() const { for(const auto& c : components) if(getTypeId<T>() == c->getId()) return true; return false; } | |
template<typename T, typename... TArgs> inline T& createComponent(TArgs&&... mArgs) | |
{ | |
//auto result(new T{std::forward<TArgs>(mArgs)...}); | |
auto result(manager.preAllocator.create<T>(std::forward<TArgs>(mArgs)...)); | |
result->entity = this; | |
result->id = getTypeId<T>(); | |
result->mysize = sizeof(T); | |
result->init(); | |
components.push_back(result); | |
return *result; | |
} | |
}; | |
// These definitions are in Entity.h because they require Entity's definition | |
inline Manager& Component::getManager() const { return entity->getManager(); } | |
inline void Manager::update(float mFrameTime) | |
{ | |
for(auto& p : groupedEntities) ssvu::eraseRemoveIf(groupedEntities[p.first], &entities.isDead<Entity*>); | |
entities.refresh(); | |
for(const auto& e : entities) e->update(mFrameTime); | |
} | |
inline void Manager::draw() | |
{ | |
toSort.clear(); | |
for(const auto& e : entities) toSort.push_back(e.get()); | |
sort(toSort, [](const Entity* mA, const Entity* mB){ return mA->getDrawPriority() > mB->getDrawPriority(); }); | |
for(const auto& e : toSort) e->draw(); | |
} | |
} | |
#endif | |
/* | |
// Copyright (c) 2013 Vittorio Romeo | |
// License: Academic Free License ("AFL") v. 3.0 | |
// AFL License page: http://opensource.org/licenses/AFL-3.0 | |
#ifndef SSES_ENTITY | |
#define SSES_ENTITY | |
#include "SSVEntitySystem/Core/Manager.h" | |
#include "SSVEntitySystem/Core/Component.h" | |
#include "SSVEntitySystem/Global/Typedefs.h" | |
#include <functional> | |
namespace sses | |
{ | |
class Entity : public ssvu::MemoryManageable | |
{ | |
private: | |
Manager& manager; | |
Bitset groups; | |
using UptrDeleter = std::function<void(Component*)>; | |
UptrDeleter uptrDeleter; | |
std::vector<Uptr<Component, UptrDeleter>> components; | |
int drawPriority{0}; | |
public: | |
Entity(Manager& mManager) : manager(mManager), uptrDeleter{[&](Component* mPtr) | |
{ | |
manager.preAllocator.destroy(mPtr, mPtr->mysize); | |
}} { } | |
Entity(const Entity&) = delete; // non construction-copyable | |
Entity& operator=(const Entity&) = delete; // non copyable | |
inline void addGroup(Group mGroup) { groups.set(mGroup); manager.addToGroup(this, mGroup); } | |
inline void delGroup(Group mGroup) { groups.set(mGroup, false); manager.delFromGroup(this, mGroup); } | |
inline bool hasGroup(Group mGroup) const { return groups.test(mGroup); } | |
inline bool hasAnyGroup(const Bitset& mGroups) const { return (groups & mGroups).any(); } | |
inline void clearGroups() { for(Group i{0}; i < groups.size(); ++i) if(groups.test(i)) manager.delFromGroup(this, i); groups.reset(); } | |
inline const Bitset& getGroups() const { return groups; } | |
inline void update(float mFrameTime) { for(const auto& c : components) c->update(mFrameTime); } | |
inline void draw() { for(const auto& c : components) c->draw(); } | |
inline void destroy() { manager.del(*this); } | |
inline void setDrawPriority(int mDrawPriority) { drawPriority = mDrawPriority; } | |
inline Manager& getManager() const { return manager; } | |
inline int getDrawPriority() const { return drawPriority; } | |
inline decltype(components)& getComponents() { return components; } | |
template<typename T> inline T* getComponentSafe() const { for(const auto& c : components) if(getTypeId<T>() == c->getId()) return static_cast<T*>(c.get()); return nullptr; } | |
template<typename T> inline T& getComponent() const { return *getComponentSafe<T>(); } | |
template<typename T> inline std::size_t getComponentCount() const { std::size_t result{0}; for(const auto& c : components) if(getTypeId<T>() == c->getId()) ++result; return result; } | |
template<typename T> inline bool hasComponent() const { for(const auto& c : components) if(getTypeId<T>() == c->getId()) return true; return false; } | |
template<typename T, typename... TArgs> inline T& createComponent(TArgs&&... mArgs) | |
{ | |
//auto result(new T{std::forward<TArgs>(mArgs)...}); | |
auto result(manager.preAllocator.create<T>(std::forward<TArgs>(mArgs)...)); | |
result->entity = this; | |
result->id = getTypeId<T>(); | |
result->mysize = sizeof(T); | |
result->init(); | |
components.emplace_back(result, uptrDeleter); | |
return *result; | |
} | |
}; | |
// These definitions are in Entity.h because they require Entity's definition | |
inline Manager& Component::getManager() const { return entity->getManager(); } | |
inline void Manager::update(float mFrameTime) | |
{ | |
for(auto& p : groupedEntities) ssvu::eraseRemoveIf(groupedEntities[p.first], &entities.isDead<Entity*>); | |
entities.refresh(); | |
for(const auto& e : entities) e->update(mFrameTime); | |
} | |
inline void Manager::draw() | |
{ | |
toSort.clear(); | |
for(const auto& e : entities) toSort.push_back(e.get()); | |
sort(toSort, [](const Entity* mA, const Entity* mB){ return mA->getDrawPriority() > mB->getDrawPriority(); }); | |
for(const auto& e : toSort) e->draw(); | |
} | |
} | |
#endif | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment