implemented functions from NativeFunctions.cs
This commit is contained in:
@@ -39,7 +39,7 @@ public class GameObject
|
|||||||
|
|
||||||
static public GameObject Create()
|
static public GameObject Create()
|
||||||
{
|
{
|
||||||
NativeMethods.createGameObject(out ulong id, out uint index);
|
NativeFunctions.createGameObject(out ulong id, out uint index);
|
||||||
var o = new GameObject(id, index);
|
var o = new GameObject(id, index);
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ public class GameObject
|
|||||||
if(_isDestroyed)
|
if(_isDestroyed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_isDestroyed = NativeMethods.destroyGameObject(_index);
|
_isDestroyed = NativeFunctions.freeGameObject(_index);
|
||||||
if(!_isDestroyed)
|
if(!_isDestroyed)
|
||||||
throw new Exception($"Can't destroy GameObject({_id})");
|
throw new Exception($"Can't destroy GameObject({_id})");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ using System.Runtime.InteropServices;
|
|||||||
|
|
||||||
namespace Ougge;
|
namespace Ougge;
|
||||||
|
|
||||||
internal static class NativeMethods
|
internal static class NativeFunctions
|
||||||
{
|
{
|
||||||
[DllImport("__Internal")]
|
[DllImport("__Internal")]
|
||||||
internal extern static bool destroyGameObject(uint index);
|
internal extern static void createGameObject(out ulong id, out uint index);
|
||||||
|
|
||||||
[DllImport("__Internal")]
|
[DllImport("__Internal")]
|
||||||
internal extern static void createGameObject(out ulong id, out uint index);
|
internal extern static bool freeGameObject(uint index);
|
||||||
}
|
}
|
||||||
@@ -94,17 +94,20 @@ std::pair<u32, GameObject&> GameObjectPool::emplace(GameObject&& new_obj)
|
|||||||
return std::pair<u32, GameObject&>(i, r);
|
return std::pair<u32, GameObject&>(i, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameObjectPool::erase(u32 index)
|
bool GameObjectPool::erase(u32 index)
|
||||||
{
|
{
|
||||||
if(index >= size)
|
if(index >= size)
|
||||||
throw UsefulException(ougge_format("index %i is out of size %i", index, size));
|
throw UsefulException(ougge_format("index %i is out of size %i", index, size));
|
||||||
if(!isIndexUsed(index))
|
if(!isIndexUsed(index)){
|
||||||
throw UsefulException(ougge_format("there is no object at index %i", index));
|
// throw UsefulException(ougge_format("there is no object at index %i", index));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
GameObjectPool::iterator::iterator(GameObjectPool* pool, u32 index)
|
GameObjectPool::iterator::iterator(GameObjectPool* pool, u32 index)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public:
|
|||||||
~GameObjectPool();
|
~GameObjectPool();
|
||||||
GameObject& get(u32 index);
|
GameObject& get(u32 index);
|
||||||
std::pair<u32, GameObject&> emplace(GameObject&& new_obj);
|
std::pair<u32, GameObject&> emplace(GameObject&& new_obj);
|
||||||
void erase(u32 index);
|
bool erase(u32 index);
|
||||||
|
|
||||||
#pragma region iterator class
|
#pragma region iterator class
|
||||||
class iterator {
|
class iterator {
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public:
|
|||||||
void createExampleObject(Engine& engine){
|
void createExampleObject(Engine& engine){
|
||||||
std::cout<<"creating ExampleObject"<<std::endl;
|
std::cout<<"creating ExampleObject"<<std::endl;
|
||||||
auto& monoSystem = engine.getModule<modules::MonoGameObjectSystem>();
|
auto& monoSystem = engine.getModule<modules::MonoGameObjectSystem>();
|
||||||
game::GameObject& exampleObj = monoSystem.createGameObject();
|
game::GameObject& exampleObj = monoSystem.createAndConstructGameObject();
|
||||||
std::string componentClassName = "Ougge.ExampleComponent";
|
std::string componentClassName = "Ougge.ExampleComponent";
|
||||||
if(!monoSystem.tryCreateComponent(exampleObj, componentClassName))
|
if(!monoSystem.tryCreateComponent(exampleObj, componentClassName))
|
||||||
throw UsefulException(ougge_format("couldn't create component '%s'", componentClassName.c_str()));
|
throw UsefulException(ougge_format("couldn't create component '%s'", componentClassName.c_str()));
|
||||||
|
|||||||
@@ -15,6 +15,28 @@ MonoGameObjectSystem::MonoGameObjectSystem(Engine& engine, u32 max_game_objects)
|
|||||||
gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
||||||
gameObjectInvokeUpdate = Mono::Method<void(f64)>(gameObjectClass, "InvokeUpdate");
|
gameObjectInvokeUpdate = Mono::Method<void(f64)>(gameObjectClass, "InvokeUpdate");
|
||||||
gameObjectTryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent_internal");
|
gameObjectTryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent_internal");
|
||||||
|
registerNativeCallbacks();
|
||||||
|
}
|
||||||
|
|
||||||
|
// sets implementations for extern functions from src-csharp/NativeFunctions.cs
|
||||||
|
void MonoGameObjectSystem::registerNativeCallbacks(){
|
||||||
|
static MonoGameObjectSystem* this_static = this;
|
||||||
|
if(this_static != this)
|
||||||
|
throw new UsefulException("creation of more than one instance of MonoGameObjectSystem is not allowed");
|
||||||
|
|
||||||
|
auto createGameObject_callback = static_cast<void(*)(u64*, u32*)> (
|
||||||
|
[](u64* id_out, u32* index_out) -> void {
|
||||||
|
this_static->createGameObjectInPool(id_out, index_out);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
mono_add_internal_call("createGameObject", (void*)createGameObject_callback);
|
||||||
|
|
||||||
|
auto freeGameObject_callback = static_cast<bool(*)(u32)>(
|
||||||
|
[](u32 index) -> bool {
|
||||||
|
return this_static->gameObjectPool.erase(index);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
mono_add_internal_call("freeGameObject", (void*)freeGameObject_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MonoGameObjectSystem::beginFrame(){
|
void MonoGameObjectSystem::beginFrame(){
|
||||||
@@ -23,10 +45,20 @@ void MonoGameObjectSystem::beginFrame(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
game::GameObject& MonoGameObjectSystem::createGameObject(){
|
// is used in NativeFunctions.cs
|
||||||
|
game::GameObject& MonoGameObjectSystem::createGameObjectInPool(u64* id_out, u32* index_out){
|
||||||
auto pair = gameObjectPool.emplace(game::GameObject(mono.createObject(gameObjectClass)));
|
auto pair = gameObjectPool.emplace(game::GameObject(mono.createObject(gameObjectClass)));
|
||||||
|
*id_out = ++obj_id;
|
||||||
|
*index_out = pair.first;
|
||||||
game::GameObject& obj = pair.second;
|
game::GameObject& obj = pair.second;
|
||||||
gameObjectCtor(obj.getObjectHandle().getObject(), ++obj_id, pair.first);
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
game::GameObject& MonoGameObjectSystem::createAndConstructGameObject(){
|
||||||
|
u64 obj_id;
|
||||||
|
u32 obj_index;
|
||||||
|
game::GameObject& obj = createGameObjectInPool(&obj_id, &obj_index);
|
||||||
|
gameObjectCtor(obj.getObjectHandle().getObject(), obj_id, obj_index);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,12 @@ public:
|
|||||||
const std::string& getName() override;
|
const std::string& getName() override;
|
||||||
void beginFrame() override;
|
void beginFrame() override;
|
||||||
|
|
||||||
game::GameObject& createGameObject();
|
game::GameObject& createAndConstructGameObject();
|
||||||
bool tryCreateComponent(game::GameObject& obj, const std::string& componentClassName);
|
bool tryCreateComponent(game::GameObject& obj, const std::string& componentClassName);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void registerNativeCallbacks();
|
||||||
|
game::GameObject& createGameObjectInPool(u64* id_out, u32* index_out);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user