question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[All] Consider wrapping raylib structs and methods into managed classes

See original GitHub issue

Before submitting a new issue, please verify and check:

  • The issue is specific to Raylib-cs and not raylib
  • I checked there is no similar issue already reported
  • My code has no errors or misuse of Raylib-cs

Issue description

I came across raylib-cpp recently and was wondering if maybe a similar approach would be good for raylib-cs https://github.com/RobLoach/raylib-cpp

Environment

NA

Issue screenshot

NA

Code example

    int screenWidth = 800;
    int screenHeight = 450;

    Window window = new Window(screenWidth, screenHeight, "raylib-cs - basic window");
    Texture logo = new Texture("raylib_logo.png");

    window.SetTargetFPS(60);

    while (!window.ShouldClose())
    {
        window.BeginDrawing();

        window.ClearBackground(RAYWHITE);

        var text = "Congrats! You created your first window!" 
        text.Draw(190, 200, 20, LIGHTGRAY);

        // Object methods.
        logo.Draw(
            screenWidth / 2 - logo.GetWidth() / 2,
            screenHeight / 2 - logo.GetHeight() / 2);

        window.EndDrawing();
    }

    // UnloadTexture() and CloseWindow() are called automatically using finalizer and IDisposable.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:18 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
9ParsonsBcommented, Aug 23, 2022

This would make the bindings rather opinionated and force users to write code in a specific way, potentially kneecapping non-standard/common architectures. I would suggest leaving this to downstream frameworks and libraries.

I believe we want Raylib-cs to be a direct bindings to raylib; choosing a design pattern should be left up to users.

1reaction
JupiterRidercommented, Sep 3, 2022

I think getting rid of structs is not possible, because we directly use them to interop with the native raylib library. Raylib c uses structs as well.

Heres an example:

        [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
        public static extern void ClearBackground(Color color);

This is the color struct: https://github.com/ChrisDill/Raylib-cs/blob/master/Raylib-cs/types/Color.cs

Raylib doesn’t support multiple windows as well: https://github.com/raysan5/raylib/wiki/Use-multiple-windows

Writing a Window class in C# would require you to use a singleton. Using finalizers doesn’t work either since dotnet core. U will have to implement IDisposable and/or use the ProcessExit event in AppDomain: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers

So here is an example of how this could look like:

using Raylib_cs;

public class Window : IDisposable
{
    //singleton
    private static Window? s_instance;
    private int _width, _height;
    private string _title;
    private static bool s_disposed;

    private Window(int width, int height, string title)
    {
        Raylib.InitWindow(width, height, title);
        (_width, _height, _title) = (width, height, title);
        s_instance = this;
        //in case someone forgets to dispose
        AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose();
    }

    public int Width { get => _width; set { _width = value; Raylib.SetWindowSize(_width, _height); } }

    public int Height { get => _height; set { _height = value; Raylib.SetWindowSize(_width, _height); } }

    public string Title { get => _title; set { _title = value; Raylib.SetWindowTitle(_title); } }

    public bool ShouldClose => Raylib.WindowShouldClose();

    public static Window Instance(int width, int height, string title)
    {
        if (s_instance is null)
        {
            return new(width, height, title);
        }

        (s_instance.Width, s_instance.Height, s_instance.Title) = (width, height, title);
        return s_instance;
    }

    public void BeginDrawing() => Raylib.BeginDrawing();

    public void EndDrawing() => Raylib.EndDrawing();

    public void ClearBackground(Color color) => Raylib.ClearBackground(color);

    public void Dispose()
    {
        //make sure the window can only be closed once
        if (s_disposed)
        {
            return;
        }

        Raylib.CloseWindow();
        s_disposed = true;
    }
}

This is the main method:


using Raylib_cs;

using Window window = Window.Instance(1280, 720, "My Game");

while (!window.ShouldClose)
{
    window.BeginDrawing();
    window.ClearBackground(Color.BEIGE);
    window.EndDrawing();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · ChrisDill/Raylib-cs
C# bindings for raylib, a simple and easy-to-use library to learn ... [All] Consider wrapping raylib structs and methods into manag.
Read more >
raylib - cheatsheet
Don't miss latest functions added to raylib... check raylib cheatsheet. ... does this job: draws everything + SwapScreenBuffer() + manage frame timing + ......
Read more >
Creating Raspberry Pi applications with Raylib and Ruby
As discussed above, the SWIG-generated extension defines Ruby classes for each C struct it encounters. Whereas Raylib heavily uses stack- ...
Read more >
Help with structs? : r/raylib
I'm trying to wrap rayLib for OpenXTalk The help I have said: struct Color; [ 4 bytes] - // RGBA values, 4 char,...
Read more >
Creating Raspberry Pi applications with Raylib and Ruby
To wrap around an entire library like Raylib, each function that needs to be available in Ruby needs a wrapper function, complete with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found