summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/render/winglcontext.h
blob: f0cb1b6af7798365aad9b9ec047175917d1ff44d (plain) (blame)
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
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert, R. Belmont
//============================================================
//
//  winglcontext.h - Windows-specific GL context
//
//  SDLMAME by Olivier Galibert and R. Belmont
//
//============================================================

#pragma once

#ifndef __WIN_GL_CONTEXT__
#define __WIN_GL_CONTEXT__

#include "modules/opengl/osd_opengl.h"
#include "modules/lib/osdlib.h"

// Typedefs for dynamically loaded functions
typedef PROC (WINAPI *wglGetProcAddress_fn)(LPCSTR);
typedef HGLRC (WINAPI *wglCreateContext_fn)(HDC);
typedef BOOL (WINAPI *wglDeleteContext_fn)(HGLRC);
typedef BOOL (WINAPI *wglMakeCurrent_fn)(HDC, HGLRC);

typedef const char * (WINAPI *wglGetExtensionsStringEXT_fn)(void);
typedef BOOL (WINAPI *wglSwapIntervalEXT_fn)(int);
typedef int (WINAPI *wglGetSwapIntervalEXT_fn)(void);

class win_gl_context : public osd_gl_context
{
public:
	win_gl_context(HWND window) : osd_gl_context(), m_context(0), m_window(nullptr), m_hdc(0)
	{
		m_error[0] = 0;

		opengl32_dll = osd::dynamic_module::open({ "opengl32.dll" });

		pfn_wglGetProcAddress = opengl32_dll->bind<wglGetProcAddress_fn>("wglGetProcAddress");
		pfn_wglCreateContext = opengl32_dll->bind<wglCreateContext_fn>("wglCreateContext");
		pfn_wglDeleteContext = opengl32_dll->bind<wglDeleteContext_fn>("wglDeleteContext");
		pfn_wglMakeCurrent = opengl32_dll->bind<wglMakeCurrent_fn>("wglMakeCurrent");

		if (pfn_wglGetProcAddress == nullptr || pfn_wglCreateContext == nullptr ||
			pfn_wglDeleteContext == nullptr || pfn_wglMakeCurrent == nullptr)
		{
			return;
		}

		pfn_wglGetExtensionsStringEXT = (wglGetExtensionsStringEXT_fn)(*pfn_wglGetProcAddress)("wglGetExtensionsStringEXT");

		if (WGLExtensionSupported("WGL_EXT_swap_control"))
		{
			pfn_wglSwapIntervalEXT = (BOOL (WINAPI *) (int)) getProcAddress("wglSwapIntervalEXT");
			pfn_wglGetSwapIntervalEXT = (int (WINAPI *) (void)) getProcAddress("wglGetSwapIntervalEXT");
		}
		else
		{
			pfn_wglSwapIntervalEXT = nullptr;
			pfn_wglGetSwapIntervalEXT = nullptr;
		}

		m_hdc = GetDC(window);
		if (!setupPixelFormat(m_hdc))
		{
			m_context = (*pfn_wglCreateContext)(m_hdc);
			if (!m_context)
			{
				FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, GetLastError(), 0, m_error, 255, nullptr);
				return;
			}
			(*pfn_wglMakeCurrent)(m_hdc, m_context);
		}
	}

	virtual ~win_gl_context()
	{
		(*pfn_wglDeleteContext)(m_context);
		ReleaseDC(m_window, m_hdc);
	}

	virtual void MakeCurrent() override
	{
		(*pfn_wglMakeCurrent)(m_hdc, m_context);
	}

	virtual const char *LastErrorMsg() override
	{
		if (m_error[0] == 0)
			return nullptr;
		else
			return m_error;
	}

	virtual void *getProcAddress(const char *proc) override
	{
		return (void *)(*pfn_wglGetProcAddress)(proc);
	}

	virtual int SetSwapInterval(const int swap) override
	{
		if (pfn_wglSwapIntervalEXT != nullptr)
		{
			pfn_wglSwapIntervalEXT(swap ? 1 : 0);
		}
		return 0;
	}

	virtual void SwapBuffer() override
	{
		SwapBuffers(m_hdc);
		//wglSwapLayerBuffers(GetDC(window().m_hwnd), WGL_SWAP_MAIN_PLANE);
	}

private:

	int setupPixelFormat(HDC hDC)
	{
		PIXELFORMATDESCRIPTOR pfd = {
			sizeof(PIXELFORMATDESCRIPTOR),  /* size */
			1,                              /* version */
			PFD_SUPPORT_OPENGL |
			PFD_DRAW_TO_WINDOW |
			PFD_DOUBLEBUFFER,               /* support double-buffering */
			PFD_TYPE_RGBA,                  /* color type */
			32,                             /* prefered color depth */
			0, 0, 0, 0, 0, 0,               /* color bits (ignored) */
			0,                              /* no alpha buffer */
			0,                              /* alpha bits (ignored) */
			0,                              /* no accumulation buffer */
			0, 0, 0, 0,                     /* accum bits (ignored) */
			16,                             /* depth buffer */
			0,                              /* no stencil buffer */
			0,                              /* no auxiliary buffers */
			PFD_MAIN_PLANE,                 /* main layer */
			0,                              /* reserved */
			0, 0, 0,                        /* no layer, visible, damage masks */
		};

		int pixelFormat = ChoosePixelFormat(hDC, &pfd);

		if (pixelFormat == 0)
		{
			strcpy(m_error, "ChoosePixelFormat failed.");
			return 1;
		}

		if (SetPixelFormat(hDC, pixelFormat, &pfd) != TRUE)
		{
			strcpy(m_error, "SetPixelFormat failed.");
			return 1;
		}
		return 0;
	}

	bool WGLExtensionSupported(const char *extension_name)
	{
		if (pfn_wglGetExtensionsStringEXT == nullptr)
			return false;

		//  printf("%s\n", pfn_wglGetExtensionsStringEXT());

		if (strstr(pfn_wglGetExtensionsStringEXT(), extension_name) != nullptr)
			return true;
		else
			return false;
	}

	HGLRC m_context;
	HWND m_window;
	HDC m_hdc;
	char m_error[256];

	osd::dynamic_module::ptr     opengl32_dll;
	wglGetProcAddress_fn         pfn_wglGetProcAddress;
	wglCreateContext_fn          pfn_wglCreateContext;
	wglDeleteContext_fn          pfn_wglDeleteContext;
	wglMakeCurrent_fn            pfn_wglMakeCurrent;

	wglGetExtensionsStringEXT_fn pfn_wglGetExtensionsStringEXT;
	wglSwapIntervalEXT_fn        pfn_wglSwapIntervalEXT;
	wglGetSwapIntervalEXT_fn     pfn_wglGetSwapIntervalEXT;
};

#endif // __WIN_GL_CONTEXT__