GameObject and Component

This commit is contained in:
2025-04-18 18:24:02 +05:00
parent 47574e9b30
commit 04e4f63fd7
7 changed files with 136 additions and 41 deletions

View File

@@ -73,8 +73,11 @@ public:
void* arg_array[] = { valueToVoidPtr(args)..., nullptr };
MonoObject* ex = nullptr;
MonoObject* result = mono_runtime_invoke(method_ptr, class_instance, arg_array, &ex);
if(ex) //TODO: call mono_trace_set_printerr_handler from mono/mono/utils/mono-logger.h
if(ex){
//TODO: call mono_trace_set_printerr_handler from mono/mono/utils/mono-logger.h
mono_print_unhandled_exception(ex);
throw UsefulException("Some C# exception occured");
}
return valueFromMonoObject<ReturnT>(result);
};
template<typename RT = ReturnT>
@@ -107,6 +110,8 @@ public:
Assembly(const Assembly&) = delete;
MonoClass* getClass(const std::string& name_space, const std::string& name);
MonoAssembly* getMonoAssembly() const { return ptr; }
MonoImage* getMonoImage() const { return image; }
};

View File

@@ -42,22 +42,25 @@ int main(int argc, const char** argv){
GameObjectPool p(GAMEOBJECTPOOL_SIZE);
auto a = mono.loadAssembly("Ougge.dll");
auto gameObjectClass = a->getClass("Ougge", "GameObject");
for(int i = 0; i < GAMEOBJECTPOOL_SIZE; i++){
Mono::Object gameObjectManaged = mono_object_new(mono.getDomain(), gameObjectClass);
p.emplace(GameObject(gameObjectManaged, nullptr));
GameObject& o = p.get(0);
if(i+1 % 8192 == 0)
std::cout<<'['<<i<<"] "<<o.getTransform()<<std::endl;
}
return 0;
MonoClass* gameObjectClass = a->getClass("Ougge", "GameObject");
MonoObject* exampleObjectManaged = mono_object_new(mono.getDomain(), gameObjectClass);
u64 obj_id = 0;
auto pair = p.emplace(GameObject(exampleObjectManaged, nullptr));
mono_runtime_object_init(exampleObjectManaged);
auto exampleObjectInit = Mono::Method<void(u64, u32)>(gameObjectClass, "Init");
exampleObjectInit(exampleObjectManaged, obj_id++, pair.first);
auto exampleObjectUpdate = Mono::Method<void(f64)>(gameObjectClass, "UpdateComponents");
// mono_runtime_object_init(scriptInstance);
// auto scriptUpdate = Mono::Method<void(f64)>(c, "Update");
// updateCallbacks.push_back([scriptInstance, scriptUpdate](f64 deltaTime) -> void {
// scriptUpdate(scriptInstance, deltaTime);
// });
updateCallbacks.push_back([exampleObjectManaged, exampleObjectUpdate](f64 deltaTime) -> void {
exampleObjectUpdate(exampleObjectManaged, deltaTime);
});
auto tryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent");
Mono::String componentNameManaged = mono_string_new(mono.getDomain(), "ExampleComponent");
Mono::ObjectHandle componentNameObjectHandle((MonoObject*)(void*)componentNameManaged);
Mono::Bool created = tryCreateComponent(exampleObjectManaged, componentNameManaged);
if(!created.wide_bool)
throw UsefulException("couldn't create ExampleComponent");
GUI::MainWindow w;
w.open("ougge", update);