Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can U try c# demo? #1

Closed
GeorgeS2019 opened this issue Jul 23, 2024 · 6 comments
Closed

Can U try c# demo? #1

GeorgeS2019 opened this issue Jul 23, 2024 · 6 comments

Comments

@GeorgeS2019
Copy link

No description provided.

@JiepengTan
Copy link
Owner

ok, i will try it later

@GeorgeS2019
Copy link
Author

@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)

@JiepengTan
Copy link
Owner

@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
Copy link
Owner

@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
Copy link
Author

GeorgeS2019 commented Aug 14, 2024

Thank you for your effort.

int main(int argc, char **argv) {
	LibGodot libgodot;

	std::string program;
	std::string project_path = "../../project/";
	if (argc > 0) {
		program = std::string(argv[0]);
	}
	if (argc > 1) {
		project_path = std::string(argv[1]);
	}
	std::vector<std::string> args = { program, "--path", project_path, "--rendering-method", "gl_compatibility", "--rendering-driver", "opengl3" };

	std::vector<char *> argvs;
	for (const auto &arg : args) {
		argvs.push_back((char *)arg.data());
	}
	argvs.push_back(nullptr);
	GDExtensionObjectPtr instance = libgodot.create(argvs.size(), argvs.data());
	if (instance == nullptr) {
		fprintf(stderr, "Error creating Godot instance\n");
		return EXIT_FAILURE;
	}
	bool succ = libgodot.start(instance);
	while (!libgodot.iteration(instance)) {
	}
	libgodot.destroy(instance);
	return EXIT_SUCCESS;
}

@GeorgeS2019
Copy link
Author

GeorgeS2019 commented Aug 15, 2024

@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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants