migeran / libgodot_project

58 stars 7 forks source link

Initialization of libgodot as a gdextension DLL with libgodot symbols for Zig #2

Open fire opened 5 months ago

fire commented 5 months ago

Hello, I'm trying to use libgodot but for zig following the c++ sample.

The primary use for this is to for Zig to call the GodotInstance class and start, iterate and shutdown. Also, the gdextension should be possible to be coded normally in zig.

This is causing some confusion and difficulties for me. I am lost understand the flow of the initialization compared with the standard gdextension flow.

Any help or guidance on how to achieve this would be greatly appreciated.

References

https://github.com/godot-zig/godot-zig

We assume that zig is using the libgodot_project's .h and .json.

const std = @import("std");
const Godot = @import("api/Godot.zig");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    defer _ = gpa.deinit();

    const args = try std.process.argsAlloc(allocator);
    defer std.process.argsFree(allocator, args);

    var argvs = std.ArrayList([]const u8).init(allocator);
    defer argvs.deinit();

    for (args) |arg| {
        try argvs.append(arg);
    }

    // Append additional arguments
    try argvs.appendSlice(&[_][]const u8{
        "--path",
        "../../project/",
    });

    std.debug.print("There are {d} args:\n", .{argvs.items.len});
    for(argvs.items) |arg| {
        std.debug.print("  {s}\n", .{arg});
    }

    var instance = Godot.GodotInstance.newGodotInstance(); // TODO: newGodotInstance somehow instantiates and or gets the godot extension pointer from c header?

    if (!instance.start()) {
        return;
    }

    while (!instance.iteration()) {
        continue;
    }

    instance.shutdown();
}