using System; using System.Collections.Generic; using System.Linq; using System.Text; using SharpGL.Version; namespace SharpGL.RenderContextProviders { public abstract class RenderContextProvider : IRenderContextProvider { /// /// Creates the render context provider. Must also create the OpenGL extensions. /// /// The desired OpenGL version. /// The OpenGL context. /// The width. /// The height. /// The bit depth. /// The extra parameter. /// public virtual bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter) { // Set the width, height and bit depth. Width = width; Height = height; BitDepth = bitDepth; // For now, assume we're going to be able to create the requested OpenGL version. requestedOpenGLVersion = openGLVersion; createdOpenGLVersion = openGLVersion; return true; } /// /// Destroys the render context provider instance. /// public virtual void Destroy() { // If we have a render context, destroy it. if(renderContextHandle != IntPtr.Zero) { Win32.wglDeleteContext(renderContextHandle); renderContextHandle = IntPtr.Zero; } } /// /// Sets the dimensions of the render context provider. /// /// Width. /// Height. public virtual void SetDimensions(int width, int height) { Width = width; Height = height; } /// /// Makes the render context current. /// public abstract void MakeCurrent(); /// /// Blit the rendered data to the supplied device context. /// /// The HDC. public abstract void Blit(IntPtr hdc); /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// void IDisposable.Dispose() { // Destroy the context provider. Destroy(); } /// /// Only valid to be called after the render context is created, this function attempts to /// move the render context to the OpenGL version originally requested. If this is > 2.1, this /// means building a new context. If this fails, we'll have to make do with 2.1. /// /// The OpenGL instance. protected void UpdateContextVersion(OpenGL gl) { // If the request version number is anything up to and including 2.1, standard render contexts // will provide what we need (as long as the graphics card drivers are up to date). var requestedVersionNumber = VersionAttribute.GetVersionAttribute(requestedOpenGLVersion); if (requestedVersionNumber.IsAtLeastVersion(4, 4) == false) { createdOpenGLVersion = requestedOpenGLVersion; return; } // Now the none-trivial case. We must use the WGL_ARB_create_context extension to // attempt to create a 3.0+ context. try { int[] attributes = { OpenGL.WGL_CONTEXT_MAJOR_VERSION_ARB, requestedVersionNumber.Major, OpenGL.WGL_CONTEXT_MINOR_VERSION_ARB, requestedVersionNumber.Minor, OpenGL.WGL_CONTEXT_FLAGS_ARB, OpenGL.WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 }; var hrc = gl.CreateContextAttribsARB(IntPtr.Zero, attributes); Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); Win32.wglDeleteContext(renderContextHandle); Win32.wglMakeCurrent(deviceContextHandle, hrc); renderContextHandle = hrc; } catch(Exception) { createdOpenGLVersion = OpenGLVersion.OpenGL2_1; } } /// /// Gets the render context handle. /// public IntPtr RenderContextHandle { get { return renderContextHandle; } protected set { renderContextHandle = value; } } /// /// Gets the device context handle. /// public IntPtr DeviceContextHandle { get { return deviceContextHandle; } protected set { deviceContextHandle = value; } } /// /// Gets or sets the width. /// /// The width. public int Width { get { return width; } protected set { width = value; } } /// /// Gets or sets the height. /// /// The height. public int Height { get { return height; } protected set { height = value; } } /// /// Gets or sets the bit depth. /// /// The bit depth. public int BitDepth { get { return bitDepth; } protected set { bitDepth = value; } } /// /// Gets a value indicating whether GDI drawing is enabled for this type of render context. /// /// true if GDI drawing is enabled; otherwise, false. public bool GDIDrawingEnabled { get { return gdiDrawingEnabled; } protected set { gdiDrawingEnabled = value; } } /// /// Gets the OpenGL version that was requested when creating the render context. /// public OpenGLVersion RequestedOpenGLVersion { get { return requestedOpenGLVersion; } } /// /// Gets the OpenGL version that is supported by the render context, compare to . /// public OpenGLVersion CreatedOpenGLVersion { get { return createdOpenGLVersion; } } /// /// The render context handle. /// protected IntPtr renderContextHandle = IntPtr.Zero; /// /// The device context handle. /// protected IntPtr deviceContextHandle = IntPtr.Zero; /// /// The width. /// protected int width = 0; /// /// The height. /// protected int height = 0; /// /// The bit depth. /// protected int bitDepth = 0; /// /// Is gdi drawing enabled? /// protected bool gdiDrawingEnabled = true; /// /// The version of OpenGL that was requested when creating the render context. /// protected OpenGLVersion requestedOpenGLVersion; /// /// The actual version of OpenGL that is supported by the render context. /// protected OpenGLVersion createdOpenGLVersion; } }