Great simple trick - I would like to tell how do you write simple with C#


#1

Hello everyone, I am working as C# developer and I want show you if you have not problem.

Getting started with create simple window with SDL2-CS and OpenGL-Wrapper

using System;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using SDL2;
namespace MonoLife.Client
{
    public class DisplayManager
    {
        protected static IntPtr window;
        protected static uint windowID;
        protected static IntPtr context;
        protected static bool isClosed = false;

        public static void CreateDisplay(string title, int width, int height)
        {
            if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) != 0)
            {
                Console.WriteLine("SDL_Init Error:", SDL.SDL_GetError());
            }

            window = SDL.SDL_CreateWindow(title, SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, width, height, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN | SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL);
            windowID = SDL.SDL_GetWindowID(window);

            SDL.SDL_GL_CreateContext(window);
            GraphicsContext.CurrentContext = context;
            GL.LoadAll(SDL.SDL_GL_GetProcAddress);
        }

        public static void UpdateDisplay()
        {
            SDL.SDL_GL_SwapWindow(window);
            SDL.SDL_UpdateWindowSurface(window);
        }

        public static bool WaitForClose
        {
            get
            {
                return isClosed;
            }
        }

        public static void InputDisplay(SDL.SDL_Event evt)
        {
            while (SDL.SDL_PollEvent(out evt) != 0)
            {
                if (evt.type == SDL.SDL_EventType.SDL_QUIT)
                {
                    isClosed = true;
                    break;
                }

                if (evt.type == SDL.SDL_EventType.SDL_KEYDOWN)
                {
                    if (evt.key.keysym.sym == SDL.SDL_Keycode.SDLK_ESCAPE)
                    {
                        isClosed = true;
                        break;
                    }
                }

                if (evt.window.windowEvent == SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED)
                {
                    int screen_width = evt.window.data1;
                    int screen_height = evt.window.data2;
                    GL.Viewport(0, 0, screen_width, screen_height);
                }
            }
        }

        public static void DestoryDisplay()
        {
            SDL.SDL_GL_DeleteContext(context);
            SDL.SDL_DestroyWindow(window);
            SDL.SDL_Quit();
        }
    }
}

There are simple methods like LwjGL 2.9x

I have found similar solution like this.

using System;
using MonoLife.Client;
using SDL2;

namespace MonoLife
{
    class MainClass
    {
        protected static SDL.SDL_Event systemEvent;

        [STAThread]
        static void Main(string[] args)
        {
            DisplayManager.CreateDisplay("Hello Game", 640, 480);
            // game initalization like GL commands for preparations or game's arguments too like for statement with if else etc..


            // end of game initalization
            while(!DisplayManager.WaitForClose)
            {
                DisplayManager.UpdateDisplay();
                // game logic


                // end of game logic
                DisplayManager.InputDisplay(systemEvent);
                // game input manager

                
                // end of game input managers
            }
            // destory or deletion of game logic like shader.CleanUp or model.CleanUp or GL comand GL.DeleteBuffer(vbo);


            // end of destory or deletion of game logic
            DisplayManager.DestoryDisplay();
        }
    }
}

Result:

That was it like LwjGL 2.9

I will create any new tutorials :smiley:

PS: Thanks for welcome and I am sorry for my bad English. I am deaf because I was bullying by bad people:( I swear that I will help your development if you would like to care with SDL2-CS by Ethan Lee ( aka. flibitijibibo ) - He is really good library creator for SDL2, SDL2-Image and more.

I would like to improve for next OpenGL wrapper for SDL2-CS

// UPDATE:

I add new many features of OpenGL, ModernOpenGL, VulkanGL, DirectX and more I am working it.

I would like to show about my own custom SDL2-CS with built-in wrappers. I am hopeful for next release for C# Programming - Don’t worry!

PS: If I am wrong for Community than you should tell me. Thanks! Because Ethan Lee suggests me.


#2

Hello everyone are you there?? I want know how do I fix with C# with Delegate with <T2>

// EDIT: Bad news:

delegate with <T> cannot able

Just wok around as public static void with <T> than it works fine

Now I am working with GL commands until. I check and I work hard.


#3

No answer??? Are you ignoring me? I wait longer? I think you are my wrong persons?

PLEASE SAY SERIOUSLY!

I am very disappointed because I can’t understand why SDL2 has problem with resizing :frowning: I really hate resizing with SDL_Renderer

using System;
using System.Runtime.InteropServices;
using Gtk;
using static SDL2.SDL;

namespace TestGtk
{
    internal class Program
    {
	    [DllImport("libgtk-3", CallingConvention = CallingConvention.Cdecl)]
	    internal static extern IntPtr gtk_widget_get_window (IntPtr widget);
	    
	    [DllImport ("libgdk-3", CallingConvention = CallingConvention.Cdecl)]
	    internal static extern IntPtr gdk_x11_window_get_xid (IntPtr raw);

	    protected static bool isRun = false;
	    
        static void Main(string[] args)
        {
	        Application.Init();
	        Window win = new Window(WindowType.Toplevel);
	        win.SetPosition(WindowPosition.Center);
	        win.SetDefaultSize(400, 400);
	        win.Title = "Hello Gtk3";
	        VBox box = new VBox();
	        box.Margin = 50;
	        
	        DrawingArea sdlArea = new DrawingArea();
	        sdlArea.AppPaintable = true;
			box.PackStart(sdlArea, true, true, 0);
			win.Add(box);
	        win.ShowAll();

	        IntPtr gdkwin = gtk_widget_get_window(sdlArea.Handle);
	        if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
	        {
				Console.WriteLine("Error: SDL2 doesn't work because you haven't enabled.");
	        }

	        IntPtr SDL_Window = SDL_CreateWindowFrom(gdk_x11_window_get_xid(gdkwin));
	        uint SDL_WindowID = 0;

	        IntPtr SDL_Renderer = SDL_CreateRenderer(SDL_Window, -1,
		        SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);
	        
	        SDL_Event evt;
	        
	        while (isRun)
	        {
		        while (SDL_PollEvent(out evt) != 0)
		        {
			        SDL_SetWindowResizable(SDL_Window, SDL_bool.SDL_TRUE);
			        if (evt.window.windowEvent == SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED || SDL_WindowID == SDL_GetWindowID(SDL_Window))
			        {
				        int width = evt.window.data1;
				        int height = evt.window.data2;
				        SDL_GetWindowSize(SDL_Window, out width, out height);
			        }
		        }
	        }

	        SDL_UpdateWindowSurface(SDL_Window);
		        
	        SDL_SetRenderDrawColor(SDL_Renderer, 255, 0, 0, 255);
	        SDL_RenderClear(SDL_Renderer);
	        SDL_RenderPresent(SDL_Renderer);
			
	        win.DeleteEvent += delegate
	        {
		        SDL_DestroyRenderer(SDL_Renderer);
		        SDL_DestroyWindow(SDL_Window);
		        SDL_Quit();
		        Application.Quit();
	        };
	        
	        Application.Run();
        }
    }
}

It is embedding in Gtk Sharp 3 / 2

But I have biiiiig problem withn resizable mode. :frowning: I really hate SDL_Renderer-resizing PLEASE TELL ME! HOW DO I GET WORKING RESIZABLE MODE OF SDL_Renderer


#4

I guess most people here (including myself) are using SDL with C or C++, not C#
So I don’t really understand the code or the problem with delegate (whatever that’s supposed to do), sorry…


#5

I am sorry for my bad English. Delegate is as listener for C# Like this See here

I thought Ethan Lee is here because he is also C# programmer.

I already tried SDL2 wrapper in Gtk Sharp 3.x

Like I have made SDL2 in Gtk Sharp 2/3 like SDL2 in Winforms made by Ethan Lee ( aka flibitijibibo )

He has created SDL2 Wrapper for SDL2 And I have tried to get success with Gtk Sharp 2/3 or X11/Xlib Sharp for Linux

If someone has experiences of C# like Ethan Lee or who is also C# Programmer like me too.

I show you I have got success with SDL2 into Gtk Sharp 3 via SDL_CreateWindowFrom()
Embedding = OK - but why does program start while SDL_Window in DrawingArea is black ??
Resizing = OK - If I start to resize than SDL_Window in Drawing in SDL_SetRenderDrawColor() shows up current color.
Drawing = FAILED - I can’t see fillrect in SDL_Window
Maximizing = FAILED - It looks like dead container.
Result: I am using Rider ( demo version, I will buy later )

Here is code:

using System;
using System.Runtime.InteropServices;
using Gtk;
using static SDL2.SDL;

namespace TestGtk
{
    internal class Program
    {
	    [DllImport("libgtk-3", CallingConvention = CallingConvention.Cdecl)]
	    internal static extern IntPtr gtk_widget_get_window (IntPtr widget);
	    
	    [DllImport ("libgdk-3", CallingConvention = CallingConvention.Cdecl)]
	    internal static extern IntPtr gdk_x11_window_get_xid (IntPtr raw);

	    protected static bool isRun = false;
	    
        static void Main(string[] args)
        {
	        Application.Init();
	        Window win = new Window(WindowType.Toplevel);
	        win.SetPosition(WindowPosition.Center);
	        win.SetDefaultSize(400, 400);
	        win.SetSizeRequest(200, 200);
	        win.Title = "Hello SDL2-CS in Gtk Sharp 3";
	        VBox box = new VBox();
	        box.Margin = 20;
	        
	        DrawingArea sdlArea = new DrawingArea();
	        sdlArea.AppPaintable = true;
			box.PackStart(sdlArea, true, true, 0);
			win.Add(box);
			
			win.ShowAll();
	        
	        IntPtr gdkwin = gtk_widget_get_window(sdlArea.Handle);
	        if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
	        {
				Console.WriteLine("Error: SDL2 doesn't work because you haven't enabled.");
	        }

	        IntPtr SDL_Window = SDL_CreateWindowFrom(gdk_x11_window_get_xid(gdkwin));

	        IntPtr SDL_Renderer = SDL_CreateRenderer(SDL_Window, -1, SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC );
	        
	        sdlArea.SizeAllocated += delegate(object o, SizeAllocatedArgs allocatedArgs)
	        {
		        SDL_Event evt;
		        while (SDL_PollEvent(out evt) != 0)
		        {
			        if(evt.type == SDL_EventType.SDL_MOUSEMOTION)
						if (evt.window.windowEvent == SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED)
						{
							int rw = evt.window.data1;
							int rh = evt.window.data2;
							SDL_SetWindowSize(SDL_Window, rw, rh);
						}
		        }
		        
		        SDL_Color cornflowerblue = SDL_Color.CornFlowerBlue;
		        SDL_SetRenderDrawColor(SDL_Renderer,cornflowerblue.r, cornflowerblue.g, cornflowerblue.b, cornflowerblue.a);
		        SDL_RenderClear(SDL_Renderer);

		        SDL_Rect fillRect = new SDL_Rect()
		        {
					x = sdlArea.WidthRequest/4,
					y = sdlArea.HeightRequest/4,
					w = sdlArea.WidthRequest/2,
					h = sdlArea.HeightRequest/2
		        };
			        
		        SDL_SetRenderDrawColor( SDL_Renderer, 255, 0, 0, 255);		
		        SDL_RenderFillRect( SDL_Renderer, ref fillRect );
		        
		        SDL_RenderPresent(SDL_Renderer);
	        };
			
	        win.DeleteEvent += delegate
	        {
		        SDL_DestroyRenderer(SDL_Renderer);
		        SDL_DestroyWindow(SDL_Window);
		        SDL_Quit();
		        Application.Quit();
	        };
			
	        Application.Run();
        }
    }
}

I understand you don’t have experience of C#. I think C# is really similar than C/c++
If you are beginner of C# or if you have to start with C# than you can try out OpenTK, Veldrid or SkiaSharp, UhroSharp or another frameworks. Good luck.