JiepengTan / libgodot_llgo_demo

3 stars 0 forks source link

Can U try c# demo? #1

Closed GeorgeS2019 closed 3 months ago

JiepengTan commented 4 months ago

ok, i will try it later

GeorgeS2019 commented 3 months ago

@JiepengTan I am unsure how challenging to have a c# demo.

I wonder if it is due to lack of readiness of the c# project : https://github.com/raulsntos/godot-dotnet

https://github.com/godotengine/godot/pull/90510#issuecomment-2285924926

JiepengTan commented 3 months ago

@JiepengTan I am unsure how challenging to have a c# demo.

I wonder if it is due to lack of readiness of the c# project : https://github.com/raulsntos/godot-dotnet

godotengine/godot#90510 (comment)

sorry. I've been busy with other things for the last few weeks, I have tried building the godot-dotnet repository on Windows, Ubuntu, and Mac (following the instructions in the README), but I failed to build it

JiepengTan commented 3 months ago

@JiepengTan I am unsure how challenging to have a c# demo. I wonder if it is due to lack of readiness of the c# project : https://github.com/raulsntos/godot-dotnet godotengine/godot#90510 (comment)

sorry. I've been busy with other things for the last few weeks, I have tried building the godot-dotnet repository on Windows, Ubuntu, and Mac (following the instructions in the README), but I failed to build it

The author mentioned the solution to this problem in the issues. I will add a C# example tomorrow.

GeorgeS2019 commented 3 months ago

Thank you for your effort.

GeorgeS2019 commented 3 months ago

@JiepengTan Thank you, exactly what I hope for. Thank you again.

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello Libgodot-csharp ! ");
        string program = "";
        string projectPath = "../../Summator/Game";
        if (args.Length > 0)
        {
            projectPath = args[0];
            Console.WriteLine("Project path: " + projectPath);
        }
        List<string> arguments = new List<string> { program, "--path", projectPath, "--rendering-method", "gl_compatibility", "--rendering-driver", "opengl3" };

        IntPtr instance = LibGodot.libgodot_create_godot_instance(arguments.Count, arguments.ToArray(), IntPtr.Zero);
        if (instance == IntPtr.Zero)
        {
            Console.Error.WriteLine("Error creating Godot instance");
            Environment.Exit(1);
        }
        bool success = LibGodot.libgodot_start_godot_instance(instance);
        while (LibGodot.libgodot_iteration_godot_instance(instance))
        {
        }
        LibGodot.libgodot_destroy_godot_instance(instance);
        Environment.Exit(0);
    }
}

The essential c# interopt with Windows: libgodot.dll

using System;
using System.Runtime.InteropServices;

public enum GDExtensionInitializationLevel
{
    GDEXTENSION_INITIALIZATION_CORE,
    GDEXTENSION_INITIALIZATION_SERVERS,
    GDEXTENSION_INITIALIZATION_SCENE,
    GDEXTENSION_INITIALIZATION_EDITOR,
    GDEXTENSION_MAX_INITIALIZATION_LEVEL
}

[StructLayout(LayoutKind.Sequential)]
public struct GDExtensionInitialization
{
    public GDExtensionInitializationLevel minimum_initialization_level;
    public IntPtr userdata;
    public Action<IntPtr, GDExtensionInitializationLevel> initialize;
    public Action<IntPtr, GDExtensionInitializationLevel> deinitialize;
}

public class LibGodot
{
    const string LIBGODOT_LIBRARY_NAME = "libgodot.dll";

    [DllImport(LIBGODOT_LIBRARY_NAME)]
    public static extern IntPtr libgodot_create_godot_instance(int argc, string[] argv, IntPtr handle);

    [DllImport(LIBGODOT_LIBRARY_NAME)]
    public static extern void libgodot_destroy_godot_instance(IntPtr instance);

    [DllImport(LIBGODOT_LIBRARY_NAME)]
    [return: MarshalAs(UnmanagedType.I1)]
    public static extern bool libgodot_start_godot_instance(IntPtr instance);

    [DllImport(LIBGODOT_LIBRARY_NAME)]
    [return: MarshalAs(UnmanagedType.I1)]
    public static extern bool libgodot_iteration_godot_instance(IntPtr instance);
}