MonoGameControl for WinForms and MonoGameWidget for GTK Sharp 2.x - [ MonoGameCanvas for Xwt ( Coming soon ) ]

Hello guys, I want know what is class / method of SwapChainRenderTarget () from MonoGame WindowsDX but I am using only OpenGL because I want use better with OpenTK / DesktopGL. Thanks

I really want create embedded MonoGame DesktopGL in Windows.Forms
Or I should use OpenTK.GLControl for MonoGame DesktopGL.

I really don’t understand why they create only SharpDX.

For me! My powerful display card was is new since last 1 year old. I want use with OpenGL. Thanks!

// EDIT:

I have written code but I am not sure if it is correctly.
MonoGameControl.cs

using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using OpenTK;

namespace MonoGameControl
{
    abstract public class MonoGameControl : GLControl
    {
        GraphicsDeviceService graphicsDeviceService;
        public GraphicsDevice GraphicsDevice
        {
            get { return graphicsDeviceService.GraphicsDevice; }
        }

        public ServiceContainer Services
        {
            get { return services; }
        }

        private ServiceContainer services = new ServiceContainer();
        protected override void OnCreateControl()
        {
            // Don't initialize the graphics device if we are running in the designer.
            if (!DesignMode)
            {
                graphicsDeviceService = GraphicsDeviceService.AddRef(Handle,
                                                                     ClientSize.Width,
                                                                     ClientSize.Height);

                services.AddService(typeof(IGraphicsDeviceService),graphicsDeviceService);

                // Give derived classes a chance to initialize themselves.
                Initialize();
            }

            base.OnCreateControl();
        }


        /// <summary>
        /// Disposes the control.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (graphicsDeviceService != null)
            {
                graphicsDeviceService.Release(disposing);
                graphicsDeviceService = null;
            }

            base.Dispose(disposing);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            string beginDrawError = BeginDraw();

            if (string.IsNullOrEmpty(beginDrawError))
            {
                // Draw the control using the GraphicsDevice.
                Draw();
                EndDraw();
            }
            else
            {
                // If BeginDraw failed, show an error message using System.Drawing.
                PaintUsingSystemDrawing(e.Graphics, beginDrawError);
            }
        }

        string BeginDraw()
        {
            // If we have no graphics device, we must be running in the designer.
            if (graphicsDeviceService == null)
            {
                return Text + "\n\n" + GetType();
            }

            // Make sure the graphics device is big enough, and is not lost.
            string deviceResetError = HandleDeviceReset();

            if (!string.IsNullOrEmpty(deviceResetError))
            {
                return deviceResetError;
            }

            GLControl control = FromHandle(graphicsDeviceService.GraphicsDevice.PresentationParameters.DeviceWindowHandle) as GLControl;
            if (control != null)
            {
                control.Context.MakeCurrent(WindowInfo);
                graphicsDeviceService.GraphicsDevice.PresentationParameters.BackBufferHeight = ClientSize.Height;
                graphicsDeviceService.GraphicsDevice.PresentationParameters.BackBufferWidth = ClientSize.Width;
            }

            Viewport viewport = new Viewport();

            viewport.X = 0;
            viewport.Y = 0;

            viewport.Width = ClientSize.Width;
            viewport.Height = ClientSize.Height;

            viewport.MinDepth = 0;
            viewport.MaxDepth = 1;

            GraphicsDevice.Viewport = viewport;

            return null;
        }


        /// <summary>
        /// Ends drawing the control. This is called after derived classes
        /// have finished their Draw method, and is responsible for presenting
        /// the finished image onto the screen, using the appropriate WinForms
        /// control handle to make sure it shows up in the right place.
        /// </summary>
        void EndDraw()
        {
            SwapBuffers();
        }


        /// <summary>
        /// Helper used by BeginDraw. This checks the graphics device status,
        /// making sure it is big enough for drawing the current control, and
        /// that the device is not lost. Returns an error string if the device
        /// could not be reset.
        /// </summary>
        string HandleDeviceReset()
        {
            bool deviceNeedsReset = false;

            switch (GraphicsDevice.GraphicsDeviceStatus)
            {
                case GraphicsDeviceStatus.Lost:
                    // If the graphics device is lost, we cannot use it at all.
                    return "Graphics device lost";

                case GraphicsDeviceStatus.NotReset:
                    // If device is in the not-reset state, we should try to reset it.
                    deviceNeedsReset = true;
                    break;

                default:
                    // If the device state is ok, check whether it is big enough.
                    PresentationParameters pp = GraphicsDevice.PresentationParameters;

                    deviceNeedsReset = (ClientSize.Width > pp.BackBufferWidth) ||
                                       (ClientSize.Height > pp.BackBufferHeight);
                    break;
            }

            return null;
        }


        /// <summary>
        /// If we do not have a valid graphics device (for instance if the device
        /// is lost, or if we are running inside the Form designer), we must use
        /// regular System.Drawing method to display a status message.
        /// </summary>
        protected virtual void PaintUsingSystemDrawing(Graphics graphics, string text)
        {
            graphics.Clear(Color.CornflowerBlue);

            using (Brush brush = new SolidBrush(Color.Black))
            {
                using (StringFormat format = new StringFormat())
                {
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;

                    graphics.DrawString(text, Font, brush, ClientRectangle, format);
                }
            }
        }


        /// <summary>
        /// Ignores WinForms paint-background messages. The default implementation
        /// would clear the control to the current background color, causing
        /// flickering when our OnPaint implementation then immediately draws some
        /// other color over the top using the XNA Framework GraphicsDevice.
        /// </summary>
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
        }
        
        /// <summary>
        /// Derived classes override this to initialize their drawing code.
        /// </summary>
        protected abstract void Initialize();


        /// <summary>
        /// Derived classes override this to draw themselves using the GraphicsDevice.
        /// </summary>
        protected abstract void Draw();
    }
}

GraphicsDeviceService.cs

using System;
using System.Threading;
using Microsoft.Xna.Framework.Graphics;

namespace MonoGameControl
{
   class GraphicsDeviceService : IGraphicsDeviceService
    {
        static GraphicsDeviceService singletonInstance;


        // Keep track of how many controls are sharing the singletonInstance.
        static int referenceCount;


        /// <summary>
        /// Constructor is private, because this is a singleton class:
        /// client controls should use the public AddRef method instead.
        /// </summary>
        GraphicsDeviceService(IntPtr windowHandle, int width, int height)
        {
            GraphicsAdapter adapter = GraphicsAdapter.DefaultAdapter;
            GraphicsProfile profile = GraphicsProfile.Reach;
            PresentationParameters pp = new PresentationParameters()
            {
                DeviceWindowHandle = windowHandle,
                BackBufferWidth = Math.Max(width, 1),
                BackBufferHeight = Math.Max(height, 1),
            };
            graphicsDevice = new GraphicsDevice(adapter, profile, pp);
        }


        /// <summary>
        /// Gets a reference to the singleton instance.
        /// </summary>
        public static GraphicsDeviceService AddRef(IntPtr windowHandle,
                                                   int width, int height)
        {
            // Increment the "how many controls sharing the device" reference count.
            if (Interlocked.Increment(ref referenceCount) == 1)
            {
                // If this is the first control to start using the
                // device, we must create the singleton instance.
                singletonInstance = new GraphicsDeviceService(windowHandle,
                                                              width, height);
            }

            return singletonInstance;
        }


        /// <summary>
        /// Releases a reference to the singleton instance.
        /// </summary>
        public void Release(bool disposing)
        {
            // Decrement the "how many controls sharing the device" reference count.
            if (Interlocked.Decrement(ref referenceCount) == 0)
            {
                // If this is the last control to finish using the
                // device, we should dispose the singleton instance.
                if (disposing)
                {
                    if (DeviceDisposing != null)
                        DeviceDisposing(this, EventArgs.Empty);

                    graphicsDevice.Dispose();
                }

                graphicsDevice = null;
            }
        }

        /// <summary>
        /// Gets the current graphics device.
        /// </summary>
        public GraphicsDevice GraphicsDevice
        {
            get { return graphicsDevice; }
        }

        GraphicsDevice graphicsDevice;


        // Store the current device settings.
        PresentationParameters parameters;

        // IGraphicsDeviceService events.
        public event EventHandler<EventArgs> DeviceCreated;
        public event EventHandler<EventArgs> DeviceDisposing;
        public event EventHandler<EventArgs> DeviceReset;
        public event EventHandler<EventArgs> DeviceResetting;
    }
}

And I add MonoGameControl to ToolBar but it throws it hasn’t Design mode? What is this? What do I forget this?

Throwing exception System.NullReferenceException: ‘Object reference not set to an instance of an object.’

I already tried…

I argue that because It won’t work for me. I miss MonoGame + OpenGL :sob: I found website but it doesn’t help me.

Yeeeaaaahhh IT WORKS with GTK Sharp 2x

But I found bug GraphicsAdapter.get_CurrentAdapter() Please fix it!

I am very happy now with GTK Sharp 2.0 + MonoGame + OpenGL.

You can build GTK Sharp 2.x with embedded MonoGame and enjoy your own editor under Linux and Mac:

I have tested Ubuntu + Windows good but throws get_currentadapter() disturbs always

Please fix get:_currentadapter()!

MonoGameWidget Download

PS: For Windows you need add reference GTK 2.x Sharp Thanks!
Thanks for using MonoGameWidget

PS: For 64 Bit do not compile x64! Because Gtk Runtime like libgdk-win32.2.0.0.dll couldn’t find… That is why!

We need build Gtk 2.x Sharp and Runtime for 64 Bit

Hello everyone

why are you not answering me? I wait longer… Stop and do not force me! I tried that

OpenTK has bad news because for Gtk# incompatible to OpenTK 2.x and 3.x

I think - If you are not sure - like you have to get chance with OpenGL:Net because OpenGL.Net works fine with Gtk# I have tested with latest version OpenGL 4.6, 4.7 and later

That is why I have problem I really don’t understand how are OpenTK and OpenGL.Net very differently?

OpenTK:                                     OpenGL.Net
Initalized                                     ContextInitaze
UpdateFrame                             ContextUpdate
...

GraphicsContext                        DeviceContext
Color.rgba                                  float r, float g, float b, float a

´Do you think MonoGame works with OpenGL.Net?

Hello I found bug:

OpenTK is really incompatible. But I really don’t understand why do MonoGamers lie me because I really want “embed” to WinForms, GTK3 ( using Msys2 / Pacman )

I have tired another OpenTK-alternative to Net reference: OpenGL.Net 0.71

But OpenGL.Net works chain. but why does GraphicsAdapter always throw? I really don’t understand. STOP SCAMMING ME! I AM MAD NOW. I don’t like because MonoGamers force me to fixing GraphicsAdapter’s bug - I wait longer longer longer…

PLEASE CHECK WINFORMS or WPF or GTK 3 with Msys2 / Pacam.

Why do you not like to embed with OpenGL.net/OpenTK with MonoGame like own editors who has developed editor like tile editor or whatever 3D like alternative to Unity3D or Unreal Engine into C#

Please listen me! And wake up! I want MonoGamers need fix GraphicsAdapter class

THANKS for understanding!