using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace StgSharp.Graphics { public abstract partial class glRenderStream:RenderStream { protected IntPtr glContextHandle { get { return ContextHandle; } set { ContextHandle = value; } } /// /// Init an OpenGl context if current form has not been binded to any context. and set the context binded to /// current form as current OpenGL context. /// /// /// Something goes wrong and StgSharp fails in creating a new OpenGL context. /// public unsafe void MakeAsCurrentContext() { if (CanvasHandle == default) { IntPtr windowPtr; byte[] tittleBytes = Encoding.UTF8.GetBytes(name); if (SharedCanvas == IntPtr.Zero) { windowPtr = InternalIO.glfwCreateWindow(Width, Height, tittleBytes, monitor, IntPtr.Zero); } else { windowPtr = InternalIO.glfwCreateWindow(Width, Height, tittleBytes, monitor, SharedCanvas.CanvasHandle); } if (((long)windowPtr == -1) || ((long)windowPtr == -2)) { throw new Exception("Cannot create window instance!"); } else { CanvasHandle = windowPtr; } ContextHandle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GLcontext))); GLcontext* contextID = (GLcontext*)ContextHandle; *contextID = new GLcontext(); contextID->glGetString = (delegate*)IntPtr.Zero; } GL.api = (GLcontext*)ContextHandle; InternalIO.glfwMakeContextCurrent(CanvasHandle); } internal sealed override void InternalInit() { Init(); } internal sealed override void InternalDraw() { MakeAsCurrentContext(); Main(); } internal sealed override void InternalTerminate() { Deinit(); } } }