-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwglInitializationDummyWindow.cs
76 lines (63 loc) · 3.02 KB
/
wglInitializationDummyWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Windows.Forms;
using HDC=System.IntPtr;
using HGLRC=System.IntPtr;
namespace Win32.WGL
{
class wglInitializationDummyWindow : Form
{
public bool Success=false;
public wglInitializationDummyWindow()
{
SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.Opaque|ControlStyles.ResizeRedraw|ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
wgl.GetCurrentDC(); // useless wgl command, to force the environment to load opengl32.dll, so that setting the pixel format reaches the opengl driver, too.
HDC dc=Window.GetDC(Handle);
if(dc==HDC.Zero) throw new NotSupportedException("OpenGL not supported. Couldn't get device context.");
try
{
PixelFormatDescriptor pfd=new PixelFormatDescriptor(PFD.Default);
int pixelFormat=DeviceContext.ChoosePixelFormat(dc, ref pfd);
if(pixelFormat==0) throw new NotSupportedException("OpenGL not supported. Can't find a pixel format.");
PixelFormatDescriptor checkpfd=new PixelFormatDescriptor();
if(DeviceContext.DescribePixelFormat(dc, pixelFormat, pfd.nSize, out checkpfd)==0) throw new NotSupportedException("OpenGL not supported. Couldn't retrieve informations about a pixel format.");
if(checkpfd.dwFlags.HasFlag(PFD.GENERIC_FORMAT))
{
// First pixel format should be a ICD. Generic implentation comes later.
checkpfd=new PixelFormatDescriptor();
if(DeviceContext.DescribePixelFormat(dc, 1, pfd.nSize, out checkpfd)==0) throw new NotSupportedException("OpenGL not supported. Couldn't retrieve informations about a pixel format.");
if(checkpfd.dwFlags.HasFlag(PFD.GENERIC_FORMAT)) throw new NotSupportedException("OpenGL not supported. Not ICD (hardware renderer) found.");
pixelFormat=1;
pfd=checkpfd;
}
if(!DeviceContext.SetPixelFormat(dc, pixelFormat, ref pfd)) throw new NotSupportedException("OpenGL not supported. Couldn't initialize pixel format.");
HGLRC rc=wgl.CreateContext(dc);
if(rc==HGLRC.Zero) throw new NotSupportedException("OpenGL not supported. Couldn't create render context.");
try
{
if(!wgl.MakeCurrent(dc, rc)) throw new NotSupportedException("OpenGL not supported. Couldn't make render and device context current.");
Success=wgl.LoadWGLExtensions(true); // we are already in the window, so no dummy window necessary.
if(!wgl.MakeCurrent(HDC.Zero, HGLRC.Zero)) throw new NotSupportedException("OpenGL not supported. Couldn't release render and device context from current thread.");
}
finally
{
if(!wgl.DeleteContext(rc)) throw new NotSupportedException("OpenGL not supported. Couldn't destroy render context.");
}
}
finally
{
if(Window.ReleaseDC(Handle, dc)==0) throw new NotSupportedException("OpenGL not supported. Couldn't release device context.");
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp=base.CreateParams;
cp.ClassStyle|=CS.VREDRAW|CS.HREDRAW|CS.OWNDC;
cp.Style|=WS.CLIPCHILDREN|WS.CLIPSIBLINGS;
return cp;
}
}
}
}