SDL2 Wrapper ( That is my work ) No longer uses SDL-CS by Ethan Lee!!!

Posted 1 month ago2023-10-19 19:51:21 UTC
DeafMan1983 DeafMan1983C# Developer and Linux Creator
Hello everyone,

I made SDL2 wrapper with sbyte*, delegate* < ... > and pass-thought.

Add package DeafMan1983.Interop.SDL2 and DeafMan1983.Conversion into your project!
dotnet add package DeafMan1983.Interop.SDL2
dotnet add package DeafMan1983.Conversion
And open code ( Visual Studio Code )
Add lines in your <project>.csproj
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PublishAOT>true</PublishAOT>
<StripSymbols>true</StripSymbols>
Open your Program.cs
namespace YourGame;

using static DeafMan1983.Conversion;

using DeafMan1983.Interop.SDL2;
using static DeafMan1983.Interop.SDL2.SDL;

unsafe class Program
{
    private SDL_Window* window;
    private SDL_Renderer* renderer;
    private SDL_Event evt;

    // Add init main(int, sbyte**)
}
And why do I add manual function like in C/C++ because somebody understands better.
public int main(int argc, sbyte** argv)
{
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        Console.WriteLine("Error: SDL initializing. {0}.\n", StringFromSBytePointer(SDL_GetError()));
        return -1;
    }

    Console.WriteLine("Woohoo! SDL2 initializes.\n");

    window = SDL_CreateWindow(SBytePointerFromString("Hello SDL2"), (int)SDL_WINDOWPOS_CENTERED, (int)SDL_WINDOWPOS_CENTERED, 600, 400, (uint)SDL_WindowFlags.SDL_WINDOW_SHOWN | (uint)SDL_WindowFlags.SDL_WINDOW_OPENGL);
    if (window == null)
    {
        Console.WriteLine("Error: Creating SDL_Window {0}.\n", StringFromSBytePointer(SDL_GetError()));
        return -1;
    }

    renderer = SDL_CreateRenderer(window, -1, (uint)SDL_RendererFlags.SDL_RENDERER_SOFTWARE);
    if (renderer == null)
    {
        Console.WriteLine("Error: Creating SDL_Renderer {0}.\n", StringFromSBytePointer(SDL_GetError()));
        return -1;
    }

    while (true)
    {
        /*
            WARNING: You need add fixed statement = It works fine :D
        */
        fixed (SDL_Event* evtptrs = &evt)
        {
            if (SDL_PollEvent(evtptrs) > 0)
            {
                if (evt.type == (uint)SDL_EventType.SDL_QUIT)
                {
                    Console.WriteLine("Click with frame window!\n");
                    break;
                }

                if (evt.type == (uint)SDL_EventType.SDL_KEYDOWN)
                {
                    if (evt.key.keysym.sym == SDL_KeyCode.SDLK_ESCAPE)
                    {
                        Console.WriteLine("Press key Escape!\n");
                        break;
                    }
                }
            }
        }

        SDL_SetRenderDrawColor(renderer, 255, 255 /3, 0, 255);
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

// static int Main(string[] args)
We add end of static Main function for C#.
static int Main(string[] args)
{
    // Reforce with default main function into static int Main() for C#
    Program game = new();
    return game.main(args.Length, SByteDoublePointersFromStringArray(args));
}
Compile and check:
User posted image
And try to use native executable:
dotnet publish -c Release -r <rid> --self-contained=true

Result:
User posted image
That is all. It works better than Ethan Lee's SDL-CS because SDL-CS has related class Marshal. If you use then native executable will throws errors cause Marshal doesn't support for native aot. I have proof. I am very excited to release OpenGL's Binding but it works in process....
User posted image
And Evergine's OpenGL's Binding has also related class Marshal.

I am very excited to release soon with OpenGL's Binding for DeafMan1983.Interop.GLFW3, DeafMan1983.Interop.SDL2 and X11'S GL ( glx ) and other desktop environments

Thank you for allowing my upgrade for Net6.x or greater.
PS: It works as well on Dotnet 8.0 too

Comments

You must log in to post a comment. You can login or register a new account.