new GameObject[] instead of uninitialized byte buffer
This commit is contained in:
parent
0863408bc5
commit
5d84e744ce
@ -7,23 +7,23 @@ std::ostream& operator<<(std::ostream& s, Transform& t){
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
GameObject::GameObject(Mono::Object managed_obj, GameObject *parent)
|
GameObject::GameObject(Mono::Object managed_obj)
|
||||||
: object_handle(managed_obj), parent(parent)
|
: object_handle(managed_obj)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
GameObject::GameObject(GameObject &&o) :
|
GameObject::GameObject(GameObject &&o) :
|
||||||
transform(o.transform),
|
|
||||||
components(std::move(o.components)),
|
|
||||||
object_handle(std::move(o.object_handle)),
|
object_handle(std::move(o.object_handle)),
|
||||||
parent(o.parent)
|
parent(o.parent),
|
||||||
|
transform(o.transform),
|
||||||
|
components(std::move(o.components))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
GameObject &GameObject::operator=(GameObject &&o){
|
GameObject& GameObject::operator=(GameObject &&o){
|
||||||
transform = o.transform;
|
|
||||||
components = std::move(o.components);
|
|
||||||
object_handle = std::move(o.object_handle);
|
object_handle = std::move(o.object_handle);
|
||||||
parent = o.parent;
|
parent = o.parent;
|
||||||
|
transform = o.transform;
|
||||||
|
components = std::move(o.components);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -26,12 +26,17 @@ std::ostream& operator<<(std::ostream& s, Transform& t);
|
|||||||
|
|
||||||
|
|
||||||
class GameObject {
|
class GameObject {
|
||||||
Transform transform;
|
|
||||||
std::map<std::u16string, Component> components;
|
|
||||||
Mono::ObjectHandle object_handle;
|
Mono::ObjectHandle object_handle;
|
||||||
GameObject* parent;
|
GameObject* parent;
|
||||||
|
Transform transform;
|
||||||
|
std::map<std::u16string, Component> components;
|
||||||
public:
|
public:
|
||||||
GameObject(Mono::Object managed_obj, GameObject* parent);
|
/// @warning Do not use this to create objects.
|
||||||
|
/// This constructor creates null values for GameObject arrays
|
||||||
|
/// GameObject* array = new GameObject[10];
|
||||||
|
/// array[0] = GameObject(initialized_mono_object_ptr)
|
||||||
|
GameObject() = default;
|
||||||
|
GameObject(Mono::Object managed_obj);
|
||||||
GameObject(const GameObject& o) = delete;
|
GameObject(const GameObject& o) = delete;
|
||||||
GameObject(GameObject&& o);
|
GameObject(GameObject&& o);
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ GameObjectPool::GameObjectPool(u32 size)
|
|||||||
useful_assert(size % 64 == 0, "size of GameObjectPool must be a multiple of 64");
|
useful_assert(size % 64 == 0, "size of GameObjectPool must be a multiple of 64");
|
||||||
this->size = size;
|
this->size = size;
|
||||||
first_unused_index = 0;
|
first_unused_index = 0;
|
||||||
buffer = new char[size*sizeof(GameObject)];
|
buffer = new GameObject[size];
|
||||||
used_indices = new u64[size/64];
|
used_indices = new u64[size/64];
|
||||||
// std::memset(buffer, 0, size*sizeof(GameObject));
|
// std::memset(buffer, 0, size*sizeof(GameObject));
|
||||||
std::memset(used_indices, 0, size/8);
|
std::memset(used_indices, 0, size/8);
|
||||||
@ -15,13 +15,8 @@ GameObjectPool::GameObjectPool(u32 size)
|
|||||||
|
|
||||||
GameObjectPool::~GameObjectPool()
|
GameObjectPool::~GameObjectPool()
|
||||||
{
|
{
|
||||||
// int i = 0;
|
delete[] buffer;
|
||||||
for(auto&& p : *this){
|
delete[] used_indices;
|
||||||
// std::cout<<"~GameObjectPool i="<<i++<<std::endl;
|
|
||||||
p.second.~GameObject();
|
|
||||||
}
|
|
||||||
delete (char*)buffer;
|
|
||||||
delete used_indices;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GameObjectPool::isIndexUsed(u32 index)
|
bool GameObjectPool::isIndexUsed(u32 index)
|
||||||
@ -81,7 +76,7 @@ GameObject& GameObjectPool::get(u32 index)
|
|||||||
throw UsefulException(format("index %i is out of size %i", index, size));
|
throw UsefulException(format("index %i is out of size %i", index, size));
|
||||||
if(!isIndexUsed(index))
|
if(!isIndexUsed(index))
|
||||||
throw UsefulException(format("there is no object at index %i", index));
|
throw UsefulException(format("there is no object at index %i", index));
|
||||||
return ((GameObject*)buffer)[index];
|
return buffer[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<u32, GameObject&> GameObjectPool::emplace(GameObject&& new_obj)
|
std::pair<u32, GameObject&> GameObjectPool::emplace(GameObject&& new_obj)
|
||||||
@ -89,7 +84,9 @@ std::pair<u32, GameObject&> GameObjectPool::emplace(GameObject&& new_obj)
|
|||||||
u32 i = first_unused_index;
|
u32 i = first_unused_index;
|
||||||
if(i == u32(-1))
|
if(i == u32(-1))
|
||||||
throw UsefulException("can't put new GameObject to GameObjectPool because it's full");
|
throw UsefulException("can't put new GameObject to GameObjectPool because it's full");
|
||||||
GameObject& r = ( ((GameObject*)buffer)[i] = std::move(new_obj) );
|
|
||||||
|
buffer[i] = std::move(new_obj);
|
||||||
|
GameObject& r = buffer[i];
|
||||||
used_indices[i/64] |= u64(1)<<(i%64); // mark index bit as used
|
used_indices[i/64] |= u64(1)<<(i%64); // mark index bit as used
|
||||||
first_unused_index = getNearestUnusedIndex(i+1);
|
first_unused_index = getNearestUnusedIndex(i+1);
|
||||||
return std::pair<u32, GameObject&>(i, r);
|
return std::pair<u32, GameObject&>(i, r);
|
||||||
@ -101,27 +98,29 @@ void GameObjectPool::erase(u32 index)
|
|||||||
throw UsefulException(format("index %i is out of size %i", index, size));
|
throw UsefulException(format("index %i is out of size %i", index, size));
|
||||||
if(!isIndexUsed(index))
|
if(!isIndexUsed(index))
|
||||||
throw UsefulException(format("there is no object at index %i", index));
|
throw UsefulException(format("there is no object at index %i", index));
|
||||||
((GameObject*)buffer)[index].~GameObject();
|
|
||||||
|
buffer[index] = GameObject();
|
||||||
used_indices[index/64] &= ~(u64(1)<<(index%64)); // mark index bit as unused
|
used_indices[index/64] &= ~(u64(1)<<(index%64)); // mark index bit as unused
|
||||||
if(index < first_unused_index)
|
if(index < first_unused_index)
|
||||||
first_unused_index = index;
|
first_unused_index = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
GameObjectPool::iterator::iterator(GameObjectPool* p, u32 index)
|
GameObjectPool::iterator::iterator(GameObjectPool* pool, u32 index)
|
||||||
: p(p), index(index)
|
: pool(pool), index(index)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<u32, GameObject&> GameObjectPool::iterator::operator*()
|
std::pair<u32, GameObject&> GameObjectPool::iterator::operator*()
|
||||||
{
|
{
|
||||||
if(index >= p->size)
|
if(index >= pool->size)
|
||||||
throw UsefulException("can't get value of end() iterator");
|
throw UsefulException("can't get value of end() iterator");
|
||||||
GameObject& r = ((GameObject*)p->buffer)[index];
|
|
||||||
|
GameObject& r = pool->buffer[index];
|
||||||
return std::pair<u32, GameObject&>(index, r);
|
return std::pair<u32, GameObject&>(index, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
GameObjectPool::iterator& GameObjectPool::iterator::operator++()
|
GameObjectPool::iterator& GameObjectPool::iterator::operator++()
|
||||||
{
|
{
|
||||||
index = p->getNearestUsedIndex(index+1);
|
index = pool->getNearestUsedIndex(index+1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -28,7 +28,7 @@ operation 'forward_list::iterate' took 2.0823 ms
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class GameObjectPool {
|
class GameObjectPool {
|
||||||
void* buffer;
|
GameObject* buffer;
|
||||||
u64* used_indices;
|
u64* used_indices;
|
||||||
u32 size;
|
u32 size;
|
||||||
u32 first_unused_index;
|
u32 first_unused_index;
|
||||||
@ -45,17 +45,19 @@ public:
|
|||||||
std::pair<u32, GameObject&> emplace(GameObject&& new_obj);
|
std::pair<u32, GameObject&> emplace(GameObject&& new_obj);
|
||||||
void erase(u32 index);
|
void erase(u32 index);
|
||||||
|
|
||||||
|
#pragma region iterator class
|
||||||
class iterator {
|
class iterator {
|
||||||
GameObjectPool* p;
|
GameObjectPool* pool;
|
||||||
u32 index = 0;
|
u32 index = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
iterator(GameObjectPool* p, u32 index);
|
iterator(GameObjectPool* pool, u32 index);
|
||||||
std::pair<u32, GameObject&> operator*();
|
std::pair<u32, GameObject&> operator*();
|
||||||
iterator& operator++();
|
iterator& operator++();
|
||||||
inline bool operator!=(const iterator& o) const { return index != o.index; };
|
inline bool operator!=(const iterator& o) const { return index != o.index; };
|
||||||
inline bool operator==(const iterator& o) const { return index == o.index; };
|
inline bool operator==(const iterator& o) const { return index == o.index; };
|
||||||
};
|
};
|
||||||
|
#pragma endregion
|
||||||
|
|
||||||
inline iterator begin() { return iterator(this, 0); }
|
inline iterator begin() { return iterator(this, 0); }
|
||||||
inline iterator end() { return iterator(this, -1); }
|
inline iterator end() { return iterator(this, -1); }
|
||||||
|
|||||||
@ -45,7 +45,7 @@ int main(int argc, const char** argv){
|
|||||||
MonoClass* gameObjectClass = a->getClass("Ougge", "GameObject");
|
MonoClass* gameObjectClass = a->getClass("Ougge", "GameObject");
|
||||||
MonoObject* exampleObjectManaged = mono_object_new(mono.getDomain(), gameObjectClass);
|
MonoObject* exampleObjectManaged = mono_object_new(mono.getDomain(), gameObjectClass);
|
||||||
u64 obj_id = 0;
|
u64 obj_id = 0;
|
||||||
auto pair = p.emplace(GameObject(exampleObjectManaged, nullptr));
|
auto pair = p.emplace(GameObject(exampleObjectManaged));
|
||||||
auto gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
auto gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
||||||
gameObjectCtor(exampleObjectManaged, obj_id++, pair.first);
|
gameObjectCtor(exampleObjectManaged, obj_id++, pair.first);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user