From ec7a8de0cfd0c5575a40e11f35516e0839f20b6b Mon Sep 17 00:00:00 2001 From: Timerix Date: Tue, 10 Sep 2024 23:23:37 +0500 Subject: [PATCH] c# update callback --- src-csharp/A.cs | 18 ------------------ src-csharp/Ougge.csproj | 2 +- src-csharp/Script.cs | 6 ++++-- src/GUI/MainWindow.cpp | 2 +- src/GUI/MainWindow.hpp | 7 ++++--- src/Mono/Mono.hpp | 14 +++++++++++++- src/Mono/Runtime.cpp | 2 +- src/main.cpp | 11 +++++++++-- 8 files changed, 33 insertions(+), 29 deletions(-) delete mode 100644 src-csharp/A.cs diff --git a/src-csharp/A.cs b/src-csharp/A.cs deleted file mode 100644 index 48be58f..0000000 --- a/src-csharp/A.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -class A { - public int a = 0; - public int b = 0; - public int c = 0; - - static public int Method(){ - return 0; - } - static public int Method(float f, byte b){ - return 1; - } - static public int Method(byte b, float f){ - Console.WriteLine($"Hello from C#! Received arguments: b={b} f={f}"); - return 2; - } -} \ No newline at end of file diff --git a/src-csharp/Ougge.csproj b/src-csharp/Ougge.csproj index e5b3070..0772100 100644 --- a/src-csharp/Ougge.csproj +++ b/src-csharp/Ougge.csproj @@ -1,7 +1,7 @@  - netstandard2.1 + net48 latest Ougge disable diff --git a/src-csharp/Script.cs b/src-csharp/Script.cs index cfddcb3..69801e4 100644 --- a/src-csharp/Script.cs +++ b/src-csharp/Script.cs @@ -1,14 +1,16 @@ using System; using System.Data; +namespace Ougge; + public abstract class ScriptBase { public virtual void Update(double deltaTime) {} } -public class Script : ScriptBase +public class ExampleScript : ScriptBase { public override void Update(double deltaTime) { - Console.WriteLine() + Console.WriteLine($"C# deltaTime {deltaTime}"); } } \ No newline at end of file diff --git a/src/GUI/MainWindow.cpp b/src/GUI/MainWindow.cpp index 6237bd0..cd3a544 100644 --- a/src/GUI/MainWindow.cpp +++ b/src/GUI/MainWindow.cpp @@ -21,7 +21,7 @@ f32 MainWindow::getDPI(){ return dpi; } -void MainWindow::open(const char* window_title, UpdatingFunc _update){ +void MainWindow::open(const char* window_title, UpdateFunc_t _update){ update = _update; SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING)); diff --git a/src/GUI/MainWindow.hpp b/src/GUI/MainWindow.hpp index 2456970..df9b785 100644 --- a/src/GUI/MainWindow.hpp +++ b/src/GUI/MainWindow.hpp @@ -4,6 +4,7 @@ #include #include "../std.hpp" #include "../time.hpp" +#include /// converts hex color to float vector #define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f) @@ -14,7 +15,7 @@ namespace ougge::GUI { #define default_font "DroidSans" -using UpdatingFunc = void (*)(f64 deltaTime); +using UpdateFunc_t = std::function; class MainWindow { public: @@ -22,7 +23,7 @@ public: f32 default_font_size = 14.0f; int fps_max = 60; // called on each frame - UpdatingFunc update = nullptr; + UpdateFunc_t update = nullptr; private: bool loop_running = false; @@ -32,7 +33,7 @@ private: SDL_Renderer* sdl_renderer = nullptr; public: - void open(const char* window_title, UpdatingFunc update); + void open(const char* window_title, UpdateFunc_t update); void startUpdateLoop(); void close(); diff --git a/src/Mono/Mono.hpp b/src/Mono/Mono.hpp index 2ee8144..510ed65 100644 --- a/src/Mono/Mono.hpp +++ b/src/Mono/Mono.hpp @@ -4,6 +4,7 @@ #include "../UsefulException.hpp" #include "../format.hpp" #include +#include #include #include #include @@ -66,7 +67,9 @@ class Method MonoMethod* method_ptr; public: - ReturnT operator()(MonoObject* class_instance, ArgTypes... args) { + + template + std::enable_if_t::value, RT> operator()(MonoObject* class_instance, ArgTypes... args) const { void* arg_array[] = { valueToVoidPtr(args)..., nullptr }; MonoObject* ex = nullptr; MonoObject* result = mono_runtime_invoke(method_ptr, class_instance, arg_array, &ex); @@ -74,6 +77,14 @@ public: mono_print_unhandled_exception(ex); return valueFromMonoObject(result); }; + template + std::enable_if_t::value, RT> operator()(MonoObject* class_instance, ArgTypes... args) const { + void* arg_array[] = { valueToVoidPtr(args)..., nullptr }; + MonoObject* ex = nullptr; + mono_runtime_invoke(method_ptr, class_instance, arg_array, &ex); + if(ex) + mono_print_unhandled_exception(ex); + }; /// all types must implement getClass() Method(MonoClass* target_class, const std::string& name){ @@ -109,6 +120,7 @@ public: RuntimeJIT(const RuntimeJIT&) = delete; ~RuntimeJIT(); + inline MonoDomain* getDomain() { return domain; } std::shared_ptr loadAssembly(const std::string& name); }; diff --git a/src/Mono/Runtime.cpp b/src/Mono/Runtime.cpp index a85ebdf..bc4cd9e 100644 --- a/src/Mono/Runtime.cpp +++ b/src/Mono/Runtime.cpp @@ -19,7 +19,7 @@ RuntimeJIT::~RuntimeJIT(){ } std::shared_ptr RuntimeJIT::loadAssembly(const std::string &name){ - MonoAssembly* ptr = mono_domain_assembly_open(domain, "app.exe"); + MonoAssembly* ptr = mono_domain_assembly_open(domain, name.c_str()); if(!ptr) throw UsefulException(format("can't load assembly '%s'", name.c_str())); return std::make_shared(ptr); diff --git a/src/main.cpp b/src/main.cpp index 9fa9db4..87e92ff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,7 @@ using namespace ougge; -std::vector updateCallbacks; +std::vector updateCallbacks; void update(f64 deltaTime){ for(auto upd : updateCallbacks){ @@ -25,7 +25,14 @@ int main(int argc, const char** argv){ Mono::RuntimeJIT mono; std::cout<<"initialized mono jit runtime"< void { std::cout<<"deltaTime: "<getClass("Ougge", "ExampleScript"); + auto scriptInstance = mono_object_new(mono.getDomain(), c); + mono_runtime_object_init(scriptInstance); + auto scriptUpdate = Mono::Method(c, "Update"); + updateCallbacks.push_back([scriptInstance, scriptUpdate](f64 deltaTime) -> void { + scriptUpdate(scriptInstance, deltaTime); + }); GUI::MainWindow w; w.open("ougge", update);