Skip to content
Browse files

Load opengl32.dll before gdi32.dll

According to
http://stackoverflow.com/questions/199016/wglcreatecontext-in-c-sharp-failing-but-not-in-managed-c,
opengl32.dll must be loaded before gdi32.dll. Affect issue #19.
  • Loading branch information...
1 parent f4f793a commit dd31b41f08ced4860cb06d4d87189b97328b98fb @thefiddler thefiddler committed Dec 17, 2013
Showing with 22 additions and 15 deletions.
  1. +20 −0 Source/OpenTK/Platform/Windows/WinFactory.cs
  2. +2 −15 Source/OpenTK/Platform/Windows/WinGLContext.cs
View
20 Source/OpenTK/Platform/Windows/WinFactory.cs
@@ -34,20 +34,29 @@ namespace OpenTK.Platform.Windows
{
using Graphics;
using OpenTK.Input;
+ using System.Runtime.InteropServices;
class WinFactory : IPlatformFactory
{
bool disposed;
readonly object SyncRoot = new object();
IInputDriver2 inputDriver;
+ internal static IntPtr OpenGLHandle { get; private set; }
+ const string OpenGLName = "OPENGL32.DLL";
+
public WinFactory()
{
if (System.Environment.OSVersion.Version.Major <= 4)
{
throw new PlatformNotSupportedException("OpenTK requires Windows XP or higher");
}
+ // Dynamically load opengl32.dll in order to use the extension loading capabilities of Wgl.
+ // Note: opengl32.dll must be loaded before gdi32.dll, otherwise strange failures may occur
+ // (such as "error: 2000" when calling wglSetPixelFormat or slowness/lag on specific GPUs).
+ LoadOpenGL();
+
if (System.Environment.OSVersion.Version.Major >= 6)
{
if (Toolkit.Options.EnableHighResolution)
@@ -60,6 +69,17 @@ public WinFactory()
}
}
+ static void LoadOpenGL()
+ {
+ OpenGLHandle = Functions.LoadLibrary(OpenGLName);
+ if (OpenGLHandle == IntPtr.Zero)
+ {
+ throw new ApplicationException(String.Format("LoadLibrary(\"{0}\") call failed with code {1}",
+ OpenGLName, Marshal.GetLastWin32Error()));
+ }
+ Debug.WriteLine(String.Format("Loaded opengl32.dll: {0}", OpenGLHandle));
+ }
+
#region IPlatformFactory Members
public virtual INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device)
View
17 Source/OpenTK/Platform/Windows/WinGLContext.cs
@@ -30,9 +30,6 @@ internal sealed class WinGLContext : DesktopGraphicsContext
static readonly object LoadLock = new object();
static readonly object SyncRoot = new object();
- static IntPtr opengl32Handle;
- const string opengl32Name = "OPENGL32.DLL";
-
bool vsync_supported;
readonly WinGraphicsMode ModeSelector;
@@ -43,16 +40,6 @@ static WinGLContext()
{
lock (LoadLock)
{
- // Dynamically load opengl32.dll in order to use the extension loading capabilities of Wgl.
- if (opengl32Handle == IntPtr.Zero)
- {
- opengl32Handle = Functions.LoadLibrary(opengl32Name);
- if (opengl32Handle == IntPtr.Zero)
- throw new ApplicationException(String.Format("LoadLibrary(\"{0}\") call failed with code {1}",
- opengl32Name, Marshal.GetLastWin32Error()));
- Debug.WriteLine(String.Format("Loaded opengl32.dll: {0}", opengl32Handle));
- }
-
// We need to create a temp context in order to load
// wgl extensions (e.g. for multisampling or GL3).
// We cannot rely on OpenTK.Platform.Wgl until we
@@ -341,7 +328,7 @@ public override IntPtr GetAddress(string function_string)
IntPtr address = Wgl.GetProcAddress(function_string);
if (!IsValid(address))
{
- address = Functions.GetProcAddress(opengl32Handle, function_string);
+ address = Functions.GetProcAddress(WinFactory.OpenGLHandle, function_string);
}
return address;
}
@@ -351,7 +338,7 @@ public override IntPtr GetAddress(IntPtr function_string)
IntPtr address = Wgl.GetProcAddress(function_string);
if (!IsValid(address))
{
- address = Functions.GetProcAddress(opengl32Handle, function_string);
+ address = Functions.GetProcAddress(WinFactory.OpenGLHandle, function_string);
}
return address;
}

0 comments on commit dd31b41

Please sign in to comment.
Something went wrong with that request. Please try again.