GteWGLEngine.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.1 (2016/06/30)
7 
8 #include <GTEnginePCH.h>
10 using namespace gte;
11 
13 {
14  Terminate();
15 }
16 
17 WGLEngine::WGLEngine(HWND handle, bool saveDriverInfo, int requiredMajor, int requiredMinor)
18  :
19  GL4Engine(),
20  mHandle(handle),
21  mDevice(nullptr),
22  mImmediate(nullptr),
24 {
25  Initialize(requiredMajor, requiredMinor, saveDriverInfo);
26 }
27 
28 WGLEngine::WGLEngine(bool saveDriverInfo, int requiredMajor, int requiredMinor)
29  :
30  GL4Engine(),
31  mHandle(nullptr),
32  mDevice(nullptr),
33  mImmediate(nullptr),
35 {
36  mComputeWindowClass = L"GL4ComputeWindowClass" +
37  std::to_wstring(reinterpret_cast<uint64_t>(this));
38 
39  WNDCLASS wc;
40  wc.style = CS_OWNDC;
41  wc.lpfnWndProc = DefWindowProc;
42  wc.cbClsExtra = 0;
43  wc.cbWndExtra = 0;
44  wc.hInstance = nullptr;
45  wc.hIcon = LoadIcon(0, IDI_APPLICATION);
46  wc.hCursor = LoadCursor(0, IDC_ARROW);
47  wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
48  wc.lpszClassName = mComputeWindowClass.c_str();
49  wc.lpszMenuName = nullptr;
50  mComputeWindowAtom = RegisterClass(&wc);
51 
52  DWORD style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
53  RECT rect = { 0, 0, 15, 15 };
54  if (AdjustWindowRect(&rect, style, FALSE) != FALSE)
55  {
56  int xSize = static_cast<int>(rect.right - rect.left + 1);
57  int ySize = static_cast<int>(rect.bottom - rect.top + 1);
58  mHandle = CreateWindow(mComputeWindowClass.c_str(), L"", style,
59  0, 0, xSize, ySize, nullptr, nullptr, nullptr, nullptr);
60  Initialize(requiredMajor, requiredMinor, saveDriverInfo);
61  }
62  else
63  {
64  UnregisterClass(mComputeWindowClass.c_str(), 0);
66  }
67 }
68 
69 bool WGLEngine::IsActive() const
70 {
71  return mImmediate == wglGetCurrentContext();
72 }
73 
75 {
76  if (mImmediate != wglGetCurrentContext())
77  {
78  wglMakeCurrent(mDevice, mImmediate);
79  }
80 }
81 
82 void WGLEngine::DisplayColorBuffer(unsigned int syncInterval)
83 {
84  wglSwapIntervalEXT(syncInterval > 0 ? 1 : 0);
85  SwapBuffers(mDevice);
86 }
87 
88 bool WGLEngine::Initialize(int requiredMajor, int requiredMinor, bool saveDriverInfo)
89 {
90  if (!mHandle)
91  {
92  LogError("Invalid window handle.");
93  return false;
94  }
95 
96  mDevice = GetDC(mHandle);
97  if (!mDevice)
98  {
99  LogError("Invalid device context.");
100  mHandle = nullptr;
101  return false;
102  }
103 
104  RECT rect;
105  BOOL result = GetClientRect(mHandle, &rect); (void)result;
106  mXSize = static_cast<unsigned int>(rect.right - rect.left);
107  mYSize = static_cast<unsigned int>(rect.bottom - rect.top);
108 
109  // Select the format for the drawing surface.
110  PIXELFORMATDESCRIPTOR pfd;
111  memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
112  pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
113  pfd.nVersion = 1;
114  pfd.dwFlags =
115  PFD_DRAW_TO_WINDOW |
116  PFD_SUPPORT_OPENGL |
117  PFD_GENERIC_ACCELERATED |
118  PFD_DOUBLEBUFFER;
119 
120  // Create an R8G8B8A8 buffer.
121  pfd.iPixelType = PFD_TYPE_RGBA;
122  pfd.cColorBits = 32;
123 
124  // Create a D24S8 depth-stencil buffer.
125  pfd.cDepthBits = 24;
126  pfd.cStencilBits = 8;
127 
128  // Set the pixel format for the rendering context.
129  int pixelFormat = ChoosePixelFormat(mDevice, &pfd);
130  if (pixelFormat == 0)
131  {
132  LogError("ChoosePixelFormat failed.");
133  ReleaseDC(mHandle, mDevice);
134  mXSize = 0;
135  mYSize = 0;
136  mDevice = nullptr;
137  mHandle = nullptr;
138  return false;
139  }
140 
141  if (!SetPixelFormat(mDevice, pixelFormat, &pfd))
142  {
143  LogError("SetPixelFormat failed.");
144  ReleaseDC(mHandle, mDevice);
145  mXSize = 0;
146  mYSize = 0;
147  mDevice = nullptr;
148  mHandle = nullptr;
149  return false;
150  }
151 
152  // Create an OpenGL context.
153  mImmediate = wglCreateContext(mDevice);
154  if (!mImmediate)
155  {
156  LogError("wglCreateContext failed.");
157  ReleaseDC(mHandle, mDevice);
158  mXSize = 0;
159  mYSize = 0;
160  mDevice = nullptr;
161  mHandle = nullptr;
162  return false;
163  }
164 
165  // Activate the context.
166  if (!wglMakeCurrent(mDevice, mImmediate))
167  {
168  LogError("wglMakeCurrent failed.");
169  wglDeleteContext(mImmediate);
170  ReleaseDC(mHandle, mDevice);
171  mXSize = 0;
172  mYSize = 0;
173  mImmediate = nullptr;
174  mDevice = nullptr;
175  mHandle = nullptr;
176  return false;
177  }
178 
179  // Get the function pointers for WGL.
180  InitializeWGL();
181 
182  // Get the function pointers for OpenGL; initialize the viewport,
183  // default global state, and default font.
184  return GL4Engine::Initialize(requiredMajor, requiredMinor, saveDriverInfo);
185 }
186 
188 {
190 
191  if (mHandle && mDevice && mImmediate)
192  {
193  wglMakeCurrent(mDevice, nullptr);
194  wglDeleteContext(mImmediate);
195  ReleaseDC(mHandle, mDevice);
196  }
197 
198  if (mComputeWindowAtom)
199  {
200  DestroyWindow(mHandle);
201  UnregisterClass(mComputeWindowClass.c_str(), 0);
202  }
203 }
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
virtual void MakeActive() override
unsigned int mXSize
Definition: GteBaseEngine.h:95
std::wstring mComputeWindowClass
Definition: GteWGLEngine.h:51
virtual bool Initialize(int requiredMajor, int requiredMinor, bool saveDriverInfo) override
virtual void DisplayColorBuffer(unsigned int syncInterval) override
ATOM mComputeWindowAtom
Definition: GteWGLEngine.h:52
WGLEngine(HWND handle, bool saveDriverInfo, int requiredMajor=4, int requiredMinor=3)
#define LogError(message)
Definition: GteLogger.h:92
virtual bool IsActive() const override
int __stdcall wglSwapIntervalEXT(int interval)
virtual bool Initialize(int requiredMajor, int requiredMinor, bool saveDriverInfo)
typedef BOOL(WINAPI *PFNWGLSAVEBUFFERREGIONARBPROC)(HANDLE hRegion
virtual ~WGLEngine()
unsigned int mYSize
Definition: GteBaseEngine.h:95
void InitializeWGL()
GLuint64EXT * result
Definition: glext.h:10003


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:02