0
\$\begingroup\$

who knows SDL2-CS embeds in Gtk Sharp 2/3?

Similar example by Ethan Lee with Winforms

But I really want know how do I fix with SDL2-CS in Gtk Sharp because I am working under Ubuntu 18.04

SDLWidget.cs

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

namespace GtkSharp3_With_SDL2
{
    public class SDLWidget : GLArea
    {
        public IntPtr SDLWindow { get; private set; }
        public IntPtr glCOntext { get; private set; }

        [DllImport("libgtk-3.so.0", CallingConvention = CallingConvention.Cdecl)]
        public static extern void gtk_window_get_size(Gtk.Window window, out int width, out int height);

        [DllImport("libgtk-3.so.0", CallingConvention = CallingConvention.Cdecl)]
        public static extern void gtk_window_move(Gtk.Window window, int x, int y);

        [DllImport("libgtk-3.so.0", CallingConvention = CallingConvention.Cdecl)]
        public static extern void gtk_window_present_with_time(Gtk.Window window, uint timestamp);

        public SDLWidget(int major, int minor, SDL_Color color)
        {
            if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
            {
#pragma warning disable RECS0155 // Finds issues with format strings
                Console.WriteLine("SDL_Init Error: ", SDL_GetError());
#pragma warning restore RECS0155 // Finds issues with format strings
            }

            SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE);
            SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, major);
            SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, minor);

            SDLWindow = SDL_CreateWindow(string.Empty, Allocation.X, Allocation.Y, Allocation.Width, Allocation.Height, SDL_WindowFlags.SDL_WINDOW_BORDERLESS | SDL_WindowFlags.SDL_WINDOW_OPENGL);
            SDL_HideWindow(SDLWindow);
            glCOntext = SDL_GL_CreateContext(SDLWindow);
            SDL_GL_MakeCurrent(SDLWindow, glCOntext);

            this.Render += delegate {
                GL.ClearColor(color);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                SDL_GL_SwapWindow(SDLWindow);
            };

            SDL_SysWMinfo info = new SDL_SysWMinfo();
            SDL_GetWindowWMInfo(SDLWindow, ref info);

            int width = AllocatedWidth;
            int height = AllocatedHeight;
            gtk_window_move((Gtk.Window)Marshal.PtrToStructure(info.info.win.window, typeof(Gtk.Window)), 0, 0);
            gtk_window_get_size((Gtk.Window)Marshal.PtrToStructure(info.info.win.window, typeof(Gtk.Window)), out width, out height);

            Parent = (Gtk.Window)Marshal.PtrToStructure(info.info.win.window, typeof(Gtk.Window));

            Window = (Gdk.Window)Marshal.PtrToStructure(info.info.win.window, typeof(Gdk.Window));
        }

        protected override bool OnDeleteEvent(Event evnt)
        {
            SDL_GL_DeleteContext(glCOntext);
            SDL_DestroyWindow(SDLWindow);
            SDL_Quit();
            return base.OnDeleteEvent(evnt);
        }
    }
}

And it is example but it can't show drawings of GL Commands. Because I really want know show up in Gtk Sharp's Window

Example:

using SDL2;
using static SDL2.SDL;

namespace GtkSharp3_With_SDL2
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Gtk.Application.Init();
            Gtk.Window win = new Gtk.Window(Gtk.WindowType.Toplevel);
            win.Title = "SDL2-CS in Gtk Sharp 3 - WOW";
            win.SetDefaultSize(500, 300);

            Gtk.VBox vbox = new Gtk.VBox();
            vbox.Margin = 5;
            SDLWidget sdlwidget = new SDLWidget(1, 0, SDL_Color.CornFlowerBlue);
#pragma warning disable CS0612 // Type or member is obsolete
            Gtk.Requisition reqsize = sdlwidget.SizeRequest();
#pragma warning restore CS0612 // Type or member is obsolete
            sdlwidget.Render += delegate {

                //GL.Viewport(0, 0, reqsize.Width, reqsize.Height);

                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();

                GL.Begin(PrimitiveType.Triangles);
                GL.Color3(SDL_Color.Darkgray);
                GL.Vertex3(-0.5f, 0.5f, 0.0f);
                GL.Color3(SDL_Color.Black);
                GL.Vertex3(0.5f, 0.5f, 0.0f);
                GL.Color3(SDL_Color.White);
                GL.Vertex3(0.0f, -0.5f, 0.0f);
                GL.End();

                GL.Flush();

            };
            vbox.PackStart(sdlwidget, true, true, 0);
            win.Add(vbox);
            win.DeleteEvent += delegate {
                Gtk.Application.Quit();
            };
            win.ShowAll();
            Gtk.Application.Run();
        }
    }
}

Result: ClearColor works fine but It can't show drawings or GL-primitives.

How do I fix for in SDL-GLContext? PS: If you can't find GL Commands in SDL2-CS I recommend you use MiniTK and SDL2-CS. Sorry It is my own SDL2-CS with any wrappers. ( OpenGL, Vulkan, ES and DX etc.. ) and my custom SDL_Color with any default colors like System.Drawing.Color, OpenTK.Color4, Microsoft.XNA.Color etc....

Thanks I hope I get solutions.

\$\endgroup\$
  • \$\begingroup\$ It's not at all clear to me what you're asking here. Can you try explaining the problem one step at a time, like you would to someone who knows nothing at all about your project or what you're trying to do? \$\endgroup\$ – DMGregory Jan 19 at 0:07
  • \$\begingroup\$ Please keep in mind that people here are here to help. If someone asks for more information or details, it's most likely because they feel like the question is not that clear or there is ambiguity. They ask more details in order to better help you. And in this case, they ask you to give more details to help you go through the obvious language barrier. Nothing personal here. We're here to help. \$\endgroup\$ – Alexandre Vaillancourt Jan 19 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.