File: MFCGraphicsOperations.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (340 lines) | stat: -rw-r--r-- 8,334 bytes parent folder | download | duplicates (2)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

#include "MFCGraphicsOperations.h"

MFCViewport::MFCViewport(HWND hwnd, HDC dc): _windowHandle(hwnd), _device_context(dc)
{
	Assertion(hwnd != nullptr, "Invalid window handle!");
	Assertion(dc != nullptr, "Invalid device context handle!");

	if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
		Error(LOCATION, "Couldn't init SDL video: %s", SDL_GetError());
		return;
	}
}

MFCViewport::~MFCViewport()
{
	ReleaseDC(_windowHandle, _device_context);

	_windowHandle = nullptr;
	_device_context = nullptr;

	SDL_QuitSubSystem(SDL_INIT_VIDEO);
}

SDL_Window* MFCViewport::toSDLWindow()
{
	return nullptr;
}

std::pair<uint32_t, uint32_t> MFCViewport::getSize()
{
	RECT size;
	if (!GetWindowRect(_windowHandle, &size))
	{
		return std::make_pair(0, 0);
	}

	return std::make_pair(size.right - size.left, size.bottom - size.top);
}

void MFCViewport::swapBuffers()
{
	SwapBuffers(_device_context);
}

void MFCViewport::setState(os::ViewportState state)
{
	// Not implemented
}

void MFCViewport::minimize()
{
	// Not implemented
}

void MFCViewport::restore()
{
	// Not implemented
}

HDC MFCViewport::getHDC()
{
	return _device_context;
}

void* MFCOpenGLContext::_oglDllHandle = nullptr;
size_t MFCOpenGLContext::_oglDllReferenceCount = 0;

MFCOpenGLContext::MFCOpenGLContext(HGLRC hglrc): _render_context(hglrc)
{
	if (_oglDllHandle == nullptr)
	{
		_oglDllHandle = SDL_LoadObject("OPENGL32.DLL");
	}
	++_oglDllReferenceCount;
}

MFCOpenGLContext::~MFCOpenGLContext()
{
	if (!wglDeleteContext(_render_context))
	{
		mprintf(("Failed to delete render context!\n"));
	}

	--_oglDllReferenceCount;
	if (_oglDllReferenceCount == 0)
	{
		SDL_UnloadObject(_oglDllHandle);
		_oglDllHandle = nullptr;
	}
}

void* MFCOpenGLContext::wglLoader(const char* name)
{
	auto wglAddr = reinterpret_cast<void*>(wglGetProcAddress(name));
	if (wglAddr == nullptr && _oglDllHandle != nullptr)
	{
		wglAddr = SDL_LoadFunction(_oglDllHandle, name);
	}

	return wglAddr;
}

os::OpenGLLoadProc MFCOpenGLContext::getLoaderFunction()
{
	return wglLoader;
}

bool MFCOpenGLContext::setSwapInterval(int status)
{
	if (GLAD_WGL_EXT_swap_control)
	{
		wglSwapIntervalEXT(status);
	}
	return true;
}

HGLRC MFCOpenGLContext::getHandle()
{
	return _render_context;
}

MFCGraphicsOperations::MFCGraphicsOperations(HWND hwnd): _windowHandle(hwnd)
{
}

MFCGraphicsOperations::~MFCGraphicsOperations()
{
}

std::unique_ptr<os::Viewport> MFCGraphicsOperations::createViewport(const os::ViewPortProperties& props)
{
	int PixelFormat;
	PIXELFORMATDESCRIPTOR pfd_test;
	PIXELFORMATDESCRIPTOR GL_pfd;

	mprintf(("  Initializing WGL...\n"));

	memset(&GL_pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
	memset(&pfd_test, 0, sizeof(PIXELFORMATDESCRIPTOR));

	GL_pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	GL_pfd.nVersion = 1;
	GL_pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
	if (props.enable_opengl)
	{
		GL_pfd.dwFlags |= PFD_SUPPORT_OPENGL;
	}

	GL_pfd.iPixelType = PFD_TYPE_RGBA;
	GL_pfd.cColorBits = (BYTE)(props.pixel_format.red_size + props.pixel_format.green_size + props.pixel_format.blue_size + props.pixel_format.alpha_size);
	GL_pfd.cRedBits = (BYTE)(props.pixel_format.red_size);
	GL_pfd.cGreenBits = (BYTE)(props.pixel_format.green_size);
	GL_pfd.cBlueBits = (BYTE)(props.pixel_format.blue_size);
	GL_pfd.cAlphaBits = (BYTE)(props.pixel_format.alpha_size);
	GL_pfd.cDepthBits = (BYTE)(props.pixel_format.depth_size);
	GL_pfd.cStencilBits = (BYTE)(props.pixel_format.stencil_size);

	Assert(_windowHandle != NULL);

	auto device_context = GetDC(_windowHandle);

	if (!device_context)
	{
		mprintf(("Unable to get device context for OpenGL W32!\n"));
		return nullptr;
	}

	PixelFormat = ChoosePixelFormat(device_context, &GL_pfd);

	if (!PixelFormat)
	{
		mprintf(("Unable to choose pixel format for OpenGL W32!\n"));
		ReleaseDC(_windowHandle, device_context);
		return nullptr;
	}
	else
	{
		DescribePixelFormat(device_context, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd_test);
	}

	if (!SetPixelFormat(device_context, PixelFormat, &GL_pfd))
	{
		mprintf(("Unable to set pixel format for OpenGL W32!\n"));
		ReleaseDC(_windowHandle, device_context);
		return nullptr;
	}

	mprintf(("  Requested SDL Video values = R: %d, G: %d, B: %d, depth: %d, stencil: %d\n",
		props.pixel_format.red_size, props.pixel_format.green_size, props.pixel_format.blue_size,
		props.pixel_format.depth_size, props.pixel_format.stencil_size));

	return std::unique_ptr<os::Viewport>(new MFCViewport(_windowHandle, device_context));
}

static void* gladWGLLoader(const char* name)
{
	return wglGetProcAddress(name);
}

std::unique_ptr<os::OpenGLContext> MFCGraphicsOperations::createOpenGLContext(os::Viewport* port, const os::OpenGLContextAttributes& ctx)
{
	auto mfcView = reinterpret_cast<MFCViewport*>(port);

	auto temp_ctx = wglCreateContext(mfcView->getHDC());
	if (!temp_ctx)
	{
		mprintf(("Unable to create fake context for OpenGL W32!\n"));
		return nullptr;
	}

	if (!wglMakeCurrent(mfcView->getHDC(), temp_ctx))
	{
		mprintf(("Unable to make current thread for OpenGL W32!\n"));
		if (!wglDeleteContext(temp_ctx))
		{
			mprintf(("Failed to delete render context!\n"));
		}
		return nullptr;
	}

	if (!gladLoadWGLLoader(gladWGLLoader, mfcView->getHDC()))
	{
		mprintf(("Failed to load WGL functions!\n"));
		wglMakeCurrent(nullptr, nullptr);
		if (!wglDeleteContext(temp_ctx))
		{
			mprintf(("Failed to delete render context!\n"));
		}
		return nullptr;
	}

	if (!GLAD_WGL_ARB_create_context)
	{
		// No support for 3.x
		if (ctx.major_version >= 3)
		{
			mprintf(("Can't create requested OpenGL context!\n"));
			wglMakeCurrent(nullptr, nullptr);
			if (!wglDeleteContext(temp_ctx))
			{
				mprintf(("Failed to delete fake context!\n"));
			}
			return nullptr;
		}
		// Major version is low enough -> just use the fake context
		return std::unique_ptr<os::OpenGLContext>(new MFCOpenGLContext(temp_ctx));
	}
	
	SCP_vector<int> attrib_list;
	attrib_list.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
	attrib_list.push_back(ctx.major_version);

	attrib_list.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
	attrib_list.push_back(ctx.minor_version);

	if (GLAD_WGL_ARB_create_context_profile)
	{
		attrib_list.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
		attrib_list.push_back(ctx.profile == os::OpenGLProfile::Core ?
			WGL_CONTEXT_CORE_PROFILE_BIT_ARB : WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB);
	}

	int flags = 0;
	if (ctx.flags[os::OpenGLContextFlags::Debug])
	{
		flags |= WGL_CONTEXT_DEBUG_BIT_ARB;
	}
	if (ctx.flags[os::OpenGLContextFlags::ForwardCompatible])
	{
		flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
	}
	if (flags != 0)
	{
		attrib_list.push_back(WGL_CONTEXT_FLAGS_ARB);
		attrib_list.push_back(flags);
	}

	attrib_list.push_back(0);

	auto attrib_ctx = wglCreateContextAttribsARB(mfcView->getHDC(), nullptr, attrib_list.data());

	// We don't need the fake context anymore
	wglMakeCurrent(nullptr, nullptr);
	if (!wglDeleteContext(temp_ctx))
	{
		mprintf(("Failed to delete fake context!\n"));
	}
	
	if (attrib_ctx == nullptr)
	{
		mprintf(("Failed to create OpenGL context!\n"));
		return nullptr;
	}
	if (!wglMakeCurrent(mfcView->getHDC(), attrib_ctx))
	{
		mprintf(("Unable to make current thread for OpenGL W32!\n"));
		if (!wglDeleteContext(attrib_ctx))
		{
			mprintf(("Failed to delete render context!\n"));
		}
		return nullptr;
	}

	if (!gladLoadWGLLoader(gladWGLLoader, mfcView->getHDC()))
	{
		mprintf(("Failed to load WGL functions!\n"));
		wglMakeCurrent(nullptr, nullptr);
		if (!wglDeleteContext(temp_ctx))
		{
			mprintf(("Failed to delete render context!\n"));
		}
		return nullptr;
	}

	return std::unique_ptr<os::OpenGLContext>(new MFCOpenGLContext(attrib_ctx));
}

void MFCGraphicsOperations::makeOpenGLContextCurrent(os::Viewport* view, os::OpenGLContext* ctx)
{
	if (view == nullptr && ctx == nullptr)
	{
		if (!wglMakeCurrent(nullptr, nullptr))
		{
			mprintf(("Failed to make OpenGL context current!\n"));
		}
		return;
	}

	Assertion(view != nullptr, "Both viewport of context must be valid at this point!");
	Assertion(ctx != nullptr, "Both viewport of context must be valid at this point!");

	auto mfcCtx = reinterpret_cast<MFCOpenGLContext*>(ctx);
	auto mfcView = reinterpret_cast<MFCViewport*>(view);

	if (!wglMakeCurrent(mfcView->getHDC(), mfcCtx->getHandle()))
	{
		mprintf(("Failed to make OpenGL context current!\n"));
	}
}