c# update callback
This commit is contained in:
parent
e71a6b71fe
commit
ec7a8de0cf
@ -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;
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<TargetFramework>net48</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<RootNamespace>Ougge</RootNamespace>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
|
||||
@ -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}");
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include <imgui.h>
|
||||
#include "../std.hpp"
|
||||
#include "../time.hpp"
|
||||
#include <functional>
|
||||
|
||||
/// 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<void(f64)>;
|
||||
|
||||
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();
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include "../UsefulException.hpp"
|
||||
#include "../format.hpp"
|
||||
#include <vector>
|
||||
#include <type_traits>
|
||||
#include <mono/metadata/metadata.h>
|
||||
#include <mono/metadata/assembly.h>
|
||||
#include <mono/metadata/object.h>
|
||||
@ -66,7 +67,9 @@ class Method<ReturnT(ArgTypes...)>
|
||||
MonoMethod* method_ptr;
|
||||
|
||||
public:
|
||||
ReturnT operator()(MonoObject* class_instance, ArgTypes... args) {
|
||||
|
||||
template<typename RT = ReturnT>
|
||||
std::enable_if_t<!std::is_void<RT>::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<ReturnT>(result);
|
||||
};
|
||||
template<typename RT = ReturnT>
|
||||
std::enable_if_t<std::is_void<RT>::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<T>()
|
||||
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<Assembly> loadAssembly(const std::string& name);
|
||||
};
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ RuntimeJIT::~RuntimeJIT(){
|
||||
}
|
||||
|
||||
std::shared_ptr<Assembly> 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<Assembly>(ptr);
|
||||
|
||||
11
src/main.cpp
11
src/main.cpp
@ -9,7 +9,7 @@
|
||||
|
||||
using namespace ougge;
|
||||
|
||||
std::vector<GUI::UpdatingFunc> updateCallbacks;
|
||||
std::vector<GUI::UpdateFunc_t> 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"<<std::endl;
|
||||
|
||||
updateCallbacks.push_back([](f64 deltaTime) -> void { std::cout<<"deltaTime: "<<deltaTime<<std::endl; });
|
||||
auto a = mono.loadAssembly("Ougge.dll");
|
||||
auto c = a->getClass("Ougge", "ExampleScript");
|
||||
auto scriptInstance = mono_object_new(mono.getDomain(), c);
|
||||
mono_runtime_object_init(scriptInstance);
|
||||
auto scriptUpdate = Mono::Method<void(f64)>(c, "Update");
|
||||
updateCallbacks.push_back([scriptInstance, scriptUpdate](f64 deltaTime) -> void {
|
||||
scriptUpdate(scriptInstance, deltaTime);
|
||||
});
|
||||
|
||||
GUI::MainWindow w;
|
||||
w.open("ougge", update);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user