2023-10-03 20:37:00 +02:00
|
|
|
// Copyright (C) 2013 Christian Stehno
|
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in Irrlicht.h
|
|
|
|
|
|
|
|
#include "CWGLManager.h"
|
|
|
|
|
2024-03-09 22:34:05 +01:00
|
|
|
#ifdef _IRR_COMPILE_WITH_WGL_MANAGER_
|
|
|
|
|
2023-10-03 20:37:00 +02:00
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/wglext.h>
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace video
|
|
|
|
{
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
CWGLManager::CWGLManager() :
|
|
|
|
PrimaryContext(SExposedVideoData(0)), PixelFormat(0), libHandle(NULL)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
#ifdef _DEBUG
|
2023-10-03 20:37:00 +02:00
|
|
|
setDebugName("CWGLManager");
|
2024-03-20 19:35:52 +01:00
|
|
|
#endif
|
2023-10-03 20:37:00 +02:00
|
|
|
memset(FunctionPointers, 0, sizeof(FunctionPointers));
|
|
|
|
}
|
|
|
|
|
|
|
|
CWGLManager::~CWGLManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CWGLManager::initialize(const SIrrlichtCreationParameters ¶ms, const SExposedVideoData &videodata)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
// store params, videoData is set later as it would be overwritten else
|
2024-03-20 19:35:52 +01:00
|
|
|
Params = params;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
// Create a window to test antialiasing support
|
2024-03-20 19:35:52 +01:00
|
|
|
const fschar_t *ClassName = __TEXT("CWGLManager");
|
2023-10-03 20:37:00 +02:00
|
|
|
HINSTANCE lhInstance = GetModuleHandle(0);
|
|
|
|
|
|
|
|
// Register Class
|
|
|
|
WNDCLASSEX wcex;
|
2024-03-20 19:35:52 +01:00
|
|
|
wcex.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wcex.style = CS_HREDRAW | CS_VREDRAW;
|
|
|
|
wcex.lpfnWndProc = (WNDPROC)DefWindowProc;
|
|
|
|
wcex.cbClsExtra = 0;
|
|
|
|
wcex.cbWndExtra = 0;
|
|
|
|
wcex.hInstance = lhInstance;
|
|
|
|
wcex.hIcon = 0;
|
|
|
|
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
|
|
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
|
|
|
|
wcex.lpszMenuName = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
wcex.lpszClassName = ClassName;
|
2024-03-20 19:35:52 +01:00
|
|
|
wcex.hIconSm = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
RegisterClassEx(&wcex);
|
|
|
|
|
|
|
|
RECT clientSize;
|
|
|
|
clientSize.top = 0;
|
|
|
|
clientSize.left = 0;
|
|
|
|
clientSize.right = Params.WindowSize.Width;
|
|
|
|
clientSize.bottom = Params.WindowSize.Height;
|
|
|
|
|
|
|
|
DWORD style = WS_POPUP;
|
|
|
|
if (!Params.Fullscreen)
|
|
|
|
style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
|
|
|
|
|
|
|
|
AdjustWindowRect(&clientSize, style, FALSE);
|
|
|
|
|
|
|
|
const s32 realWidth = clientSize.right - clientSize.left;
|
|
|
|
const s32 realHeight = clientSize.bottom - clientSize.top;
|
|
|
|
|
|
|
|
const s32 windowLeft = (GetSystemMetrics(SM_CXSCREEN) - realWidth) / 2;
|
|
|
|
const s32 windowTop = (GetSystemMetrics(SM_CYSCREEN) - realHeight) / 2;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
HWND temporary_wnd = CreateWindow(ClassName, __TEXT(""), style, windowLeft,
|
2023-10-03 20:37:00 +02:00
|
|
|
windowTop, realWidth, realHeight, NULL, NULL, lhInstance, NULL);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!temporary_wnd) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot create a temporary window.", ELL_ERROR);
|
|
|
|
UnregisterClass(ClassName, lhInstance);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HDC HDc = GetDC(temporary_wnd);
|
|
|
|
|
|
|
|
// Set up pixel format descriptor with desired parameters
|
|
|
|
PIXELFORMATDESCRIPTOR tmp_pfd = {
|
2024-03-20 19:35:52 +01:00
|
|
|
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
|
|
|
|
1, // Version Number
|
|
|
|
(DWORD)(PFD_DRAW_TO_WINDOW | // Format Must Support Window
|
|
|
|
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
|
|
|
|
(Params.Doublebuffer ? PFD_DOUBLEBUFFER : 0) | // Must Support Double Buffering
|
|
|
|
(Params.Stereobuffer ? PFD_STEREO : 0)), // Must Support Stereo Buffer
|
|
|
|
PFD_TYPE_RGBA, // Request An RGBA Format
|
|
|
|
Params.Bits, // Select Our Color Depth
|
|
|
|
0, 0, 0, 0, 0, 0, // Color Bits Ignored
|
|
|
|
0, // No Alpha Buffer
|
|
|
|
0, // Shift Bit Ignored
|
|
|
|
0, // No Accumulation Buffer
|
|
|
|
0, 0, 0, 0, // Accumulation Bits Ignored
|
|
|
|
Params.ZBufferBits, // Z-Buffer (Depth Buffer)
|
|
|
|
BYTE(Params.Stencilbuffer ? 1 : 0), // Stencil Buffer Depth
|
|
|
|
0, // No Auxiliary Buffer
|
|
|
|
PFD_MAIN_PLANE, // Main Drawing Layer
|
|
|
|
0, // Reserved
|
|
|
|
0, 0, 0 // Layer Masks Ignored
|
2023-10-03 20:37:00 +02:00
|
|
|
};
|
2024-03-20 19:35:52 +01:00
|
|
|
pfd = tmp_pfd;
|
|
|
|
|
|
|
|
for (u32 i = 0; i < 6; ++i) {
|
|
|
|
if (i == 1) {
|
|
|
|
if (Params.Stencilbuffer) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot create a GL device with stencil buffer, disabling stencil shadows.", ELL_WARNING);
|
|
|
|
Params.Stencilbuffer = false;
|
|
|
|
pfd.cStencilBits = 0;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else
|
2023-10-03 20:37:00 +02:00
|
|
|
continue;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (i == 2) {
|
2023-10-03 20:37:00 +02:00
|
|
|
pfd.cDepthBits = 24;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (i == 3) {
|
|
|
|
if (Params.Bits != 16)
|
2023-10-03 20:37:00 +02:00
|
|
|
pfd.cDepthBits = 16;
|
|
|
|
else
|
|
|
|
continue;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (i == 4) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// try single buffer
|
|
|
|
if (Params.Doublebuffer)
|
|
|
|
pfd.dwFlags &= ~PFD_DOUBLEBUFFER;
|
|
|
|
else
|
|
|
|
continue;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (i == 5) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot create a GL device context", "No suitable format for temporary window.", ELL_ERROR);
|
|
|
|
ReleaseDC(temporary_wnd, HDc);
|
|
|
|
DestroyWindow(temporary_wnd);
|
|
|
|
UnregisterClass(ClassName, lhInstance);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// choose pixelformat
|
|
|
|
PixelFormat = ChoosePixelFormat(HDc, &pfd);
|
|
|
|
if (PixelFormat)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetPixelFormat(HDc, PixelFormat, &pfd);
|
|
|
|
os::Printer::log("Create temporary GL rendering context", ELL_DEBUG);
|
2024-03-20 19:35:52 +01:00
|
|
|
HGLRC hrc = wglCreateContext(HDc);
|
|
|
|
if (!hrc) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot create a temporary GL rendering context.", ELL_ERROR);
|
|
|
|
ReleaseDC(temporary_wnd, HDc);
|
|
|
|
DestroyWindow(temporary_wnd);
|
|
|
|
UnregisterClass(ClassName, lhInstance);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentContext.OpenGLWin32.HDc = HDc;
|
|
|
|
CurrentContext.OpenGLWin32.HRc = hrc;
|
|
|
|
CurrentContext.OpenGLWin32.HWnd = temporary_wnd;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!activateContext(CurrentContext, false)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot activate a temporary GL rendering context.", ELL_ERROR);
|
|
|
|
wglDeleteContext(hrc);
|
|
|
|
ReleaseDC(temporary_wnd, HDc);
|
|
|
|
DestroyWindow(temporary_wnd);
|
|
|
|
UnregisterClass(ClassName, lhInstance);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
core::stringc wglExtensions;
|
|
|
|
#ifdef WGL_ARB_extensions_string
|
|
|
|
PFNWGLGETEXTENSIONSSTRINGARBPROC irrGetExtensionsString = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
|
|
|
|
if (irrGetExtensionsString)
|
|
|
|
wglExtensions = irrGetExtensionsString(HDc);
|
|
|
|
#elif defined(WGL_EXT_extensions_string)
|
|
|
|
PFNWGLGETEXTENSIONSSTRINGEXTPROC irrGetExtensionsString = (PFNWGLGETEXTENSIONSSTRINGEXTPROC)wglGetProcAddress("wglGetExtensionsStringEXT");
|
|
|
|
if (irrGetExtensionsString)
|
|
|
|
wglExtensions = irrGetExtensionsString(HDc);
|
|
|
|
#endif
|
|
|
|
const bool pixel_format_supported = (wglExtensions.find("WGL_ARB_pixel_format") != -1);
|
|
|
|
const bool multi_sample_supported = ((wglExtensions.find("WGL_ARB_multisample") != -1) ||
|
2024-03-20 19:35:52 +01:00
|
|
|
(wglExtensions.find("WGL_EXT_multisample") != -1) || (wglExtensions.find("WGL_3DFX_multisample") != -1));
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _DEBUG
|
|
|
|
os::Printer::log("WGL_extensions", wglExtensions);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Without a GL context we can't call wglGetProcAddress so store this for later
|
2024-03-20 19:35:52 +01:00
|
|
|
FunctionPointers[0] = (void *)wglGetProcAddress("wglCreateContextAttribsARB");
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
#ifdef WGL_ARB_pixel_format
|
|
|
|
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormat_ARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
|
2024-03-20 19:35:52 +01:00
|
|
|
if (pixel_format_supported && wglChoosePixelFormat_ARB) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// This value determines the number of samples used for antialiasing
|
|
|
|
// My experience is that 8 does not show a big
|
|
|
|
// improvement over 4, but 4 shows a big improvement
|
|
|
|
// over 2.
|
|
|
|
|
|
|
|
if (Params.AntiAlias > 32)
|
|
|
|
Params.AntiAlias = 32;
|
|
|
|
|
|
|
|
f32 fAttributes[] = {0.0, 0.0};
|
|
|
|
s32 iAttributes[] =
|
2024-03-20 19:35:52 +01:00
|
|
|
{
|
|
|
|
WGL_DRAW_TO_WINDOW_ARB, 1,
|
|
|
|
WGL_SUPPORT_OPENGL_ARB, 1,
|
|
|
|
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
|
|
|
|
WGL_COLOR_BITS_ARB, (Params.Bits == 32) ? 24 : 15,
|
|
|
|
WGL_ALPHA_BITS_ARB, (Params.Bits == 32) ? 8 : 1,
|
|
|
|
WGL_DEPTH_BITS_ARB, Params.ZBufferBits, // 10,11
|
|
|
|
WGL_STENCIL_BITS_ARB, Params.Stencilbuffer ? 1 : 0,
|
|
|
|
WGL_DOUBLE_BUFFER_ARB, Params.Doublebuffer ? 1 : 0,
|
|
|
|
WGL_STEREO_ARB, Params.Stereobuffer ? 1 : 0,
|
|
|
|
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef WGL_ARB_multisample
|
2024-03-20 19:35:52 +01:00
|
|
|
WGL_SAMPLES_ARB, Params.AntiAlias, // 20,21
|
|
|
|
WGL_SAMPLE_BUFFERS_ARB, (Params.AntiAlias > 0) ? 1 : 0,
|
2023-10-03 20:37:00 +02:00
|
|
|
#elif defined(WGL_EXT_multisample)
|
2024-03-20 19:35:52 +01:00
|
|
|
WGL_SAMPLES_EXT, AntiAlias, // 20,21
|
|
|
|
WGL_SAMPLE_BUFFERS_EXT, (Params.AntiAlias > 0) ? 1 : 0,
|
2023-10-03 20:37:00 +02:00
|
|
|
#elif defined(WGL_3DFX_multisample)
|
2024-03-20 19:35:52 +01:00
|
|
|
WGL_SAMPLES_3DFX, AntiAlias, // 20,21
|
|
|
|
WGL_SAMPLE_BUFFERS_3DFX, (Params.AntiAlias > 0) ? 1 : 0,
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
// WGL_DEPTH_FLOAT_EXT, 1,
|
|
|
|
0, 0, 0, 0};
|
|
|
|
int iAttrSize = sizeof(iAttributes) / sizeof(int);
|
|
|
|
if (!multi_sample_supported) {
|
|
|
|
memmove(&iAttributes[20], &iAttributes[24], sizeof(int) * (iAttrSize - 24));
|
2023-10-03 20:37:00 +02:00
|
|
|
iAttrSize -= 4;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
s32 rv = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
// Try to get an acceptable pixel format
|
2024-03-20 19:35:52 +01:00
|
|
|
do {
|
|
|
|
int pixelFormat = 0;
|
|
|
|
UINT numFormats = 0;
|
|
|
|
const BOOL valid = wglChoosePixelFormat_ARB(HDc, iAttributes, fAttributes, 1, &pixelFormat, &numFormats);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
if (valid && numFormats)
|
|
|
|
rv = pixelFormat;
|
|
|
|
else
|
|
|
|
iAttributes[21] -= 1;
|
2024-03-20 19:35:52 +01:00
|
|
|
} while (rv == 0 && iAttributes[21] > 1);
|
|
|
|
if (rv) {
|
|
|
|
PixelFormat = rv;
|
|
|
|
Params.AntiAlias = iAttributes[21];
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
Params.AntiAlias = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
// this only terminates the temporary HRc
|
|
|
|
destroyContext();
|
|
|
|
destroySurface();
|
|
|
|
terminate();
|
|
|
|
DestroyWindow(temporary_wnd);
|
|
|
|
UnregisterClass(ClassName, lhInstance);
|
|
|
|
|
|
|
|
// now get new window
|
2024-03-20 19:35:52 +01:00
|
|
|
CurrentContext.OpenGLWin32.HWnd = videodata.OpenGLWin32.HWnd;
|
2023-10-03 20:37:00 +02:00
|
|
|
// get hdc
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!(CurrentContext.OpenGLWin32.HDc = GetDC((HWND)videodata.OpenGLWin32.HWnd))) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot create a GL device context.", ELL_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!PrimaryContext.OpenGLWin32.HWnd) {
|
|
|
|
PrimaryContext.OpenGLWin32.HWnd = CurrentContext.OpenGLWin32.HWnd;
|
|
|
|
PrimaryContext.OpenGLWin32.HDc = CurrentContext.OpenGLWin32.HDc;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWGLManager::terminate()
|
|
|
|
{
|
|
|
|
if (CurrentContext.OpenGLWin32.HDc)
|
|
|
|
ReleaseDC((HWND)CurrentContext.OpenGLWin32.HWnd, (HDC)CurrentContext.OpenGLWin32.HDc);
|
|
|
|
if (PrimaryContext.OpenGLWin32.HDc && PrimaryContext.OpenGLWin32.HDc == CurrentContext.OpenGLWin32.HDc)
|
|
|
|
memset(&PrimaryContext, 0, sizeof(PrimaryContext));
|
|
|
|
memset(&CurrentContext, 0, sizeof(CurrentContext));
|
|
|
|
if (libHandle)
|
|
|
|
FreeLibrary(libHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CWGLManager::generateSurface()
|
|
|
|
{
|
|
|
|
HDC HDc = (HDC)CurrentContext.OpenGLWin32.HDc;
|
|
|
|
// search for pixel format the simple way
|
2024-03-20 19:35:52 +01:00
|
|
|
if (PixelFormat == 0 || (!SetPixelFormat(HDc, PixelFormat, &pfd))) {
|
|
|
|
for (u32 i = 0; i < 5; ++i) {
|
|
|
|
if (i == 1) {
|
|
|
|
if (Params.Stencilbuffer) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot create a GL device with stencil buffer, disabling stencil shadows.", ELL_WARNING);
|
|
|
|
Params.Stencilbuffer = false;
|
|
|
|
pfd.cStencilBits = 0;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else
|
2023-10-03 20:37:00 +02:00
|
|
|
continue;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (i == 2) {
|
2023-10-03 20:37:00 +02:00
|
|
|
pfd.cDepthBits = 24;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
if (i == 3) {
|
|
|
|
if (Params.Bits != 16)
|
2023-10-03 20:37:00 +02:00
|
|
|
pfd.cDepthBits = 16;
|
|
|
|
else
|
|
|
|
continue;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (i == 4) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot create a GL device context", "No suitable format.", ELL_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// choose pixelformat
|
|
|
|
PixelFormat = ChoosePixelFormat(HDc, &pfd);
|
|
|
|
if (PixelFormat)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set pixel format
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!SetPixelFormat(HDc, PixelFormat, &pfd)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot set the pixel format.", ELL_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (pfd.cAlphaBits != 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (pfd.cRedBits == 8)
|
|
|
|
ColorFormat = ECF_A8R8G8B8;
|
|
|
|
else
|
|
|
|
ColorFormat = ECF_A1R5G5B5;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (pfd.cRedBits == 8)
|
|
|
|
ColorFormat = ECF_R8G8B8;
|
|
|
|
else
|
|
|
|
ColorFormat = ECF_R5G6B5;
|
|
|
|
}
|
|
|
|
os::Printer::log("Pixel Format", core::stringc(PixelFormat).c_str(), ELL_DEBUG);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWGLManager::destroySurface()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CWGLManager::generateContext()
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
HDC HDc = (HDC)CurrentContext.OpenGLWin32.HDc;
|
2023-10-03 20:37:00 +02:00
|
|
|
HGLRC hrc;
|
|
|
|
// create rendering context
|
|
|
|
#ifdef WGL_ARB_create_context
|
|
|
|
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs_ARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)FunctionPointers[0];
|
2024-03-20 19:35:52 +01:00
|
|
|
if (wglCreateContextAttribs_ARB) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// with 3.0 all available profiles should be usable, higher versions impose restrictions
|
|
|
|
// we need at least 1.1
|
|
|
|
const int iAttribs[] =
|
2024-03-20 19:35:52 +01:00
|
|
|
{
|
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 1,
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
|
|
|
// WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB, // enable to get a debug context (depends on driver if that does anything)
|
|
|
|
0};
|
|
|
|
hrc = wglCreateContextAttribs_ARB(HDc, 0, iAttribs);
|
|
|
|
} else
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
hrc = wglCreateContext(HDc);
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Irrlicht context");
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!hrc) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Cannot create a GL rendering context.", ELL_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set exposed data
|
|
|
|
CurrentContext.OpenGLWin32.HRc = hrc;
|
|
|
|
if (!PrimaryContext.OpenGLWin32.HRc)
|
2024-03-20 19:35:52 +01:00
|
|
|
PrimaryContext.OpenGLWin32.HRc = CurrentContext.OpenGLWin32.HRc;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const SExposedVideoData &CWGLManager::getContext() const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
return CurrentContext;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CWGLManager::activateContext(const SExposedVideoData &videoData, bool restorePrimaryOnZero)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (videoData.OpenGLWin32.HWnd && videoData.OpenGLWin32.HDc && videoData.OpenGLWin32.HRc) {
|
|
|
|
if (!wglMakeCurrent((HDC)videoData.OpenGLWin32.HDc, (HGLRC)videoData.OpenGLWin32.HRc)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Render Context switch failed.");
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
CurrentContext = videoData;
|
|
|
|
} else if (!restorePrimaryOnZero && !videoData.OpenGLWin32.HDc && !videoData.OpenGLWin32.HRc) {
|
|
|
|
if (!wglMakeCurrent((HDC)0, (HGLRC)0)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Render Context reset failed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CurrentContext = videoData;
|
|
|
|
}
|
|
|
|
// set back to main context
|
2024-03-20 19:35:52 +01:00
|
|
|
else if (!videoData.OpenGLWin32.HWnd && CurrentContext.OpenGLWin32.HDc != PrimaryContext.OpenGLWin32.HDc) {
|
|
|
|
if (!wglMakeCurrent((HDC)PrimaryContext.OpenGLWin32.HDc, (HGLRC)PrimaryContext.OpenGLWin32.HRc)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Render Context switch (back to main) failed.");
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
CurrentContext = PrimaryContext;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CWGLManager::destroyContext()
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (CurrentContext.OpenGLWin32.HRc) {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!wglMakeCurrent((HDC)CurrentContext.OpenGLWin32.HDc, 0))
|
|
|
|
os::Printer::log("Release of render context failed.", ELL_WARNING);
|
|
|
|
|
|
|
|
if (!wglDeleteContext((HGLRC)CurrentContext.OpenGLWin32.HRc))
|
|
|
|
os::Printer::log("Deletion of render context failed.", ELL_WARNING);
|
2024-03-20 19:35:52 +01:00
|
|
|
if (PrimaryContext.OpenGLWin32.HRc == CurrentContext.OpenGLWin32.HRc)
|
|
|
|
PrimaryContext.OpenGLWin32.HRc = 0;
|
|
|
|
CurrentContext.OpenGLWin32.HRc = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
void *CWGLManager::getProcAddress(const std::string &procName)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
void *proc = NULL;
|
|
|
|
proc = (void *)wglGetProcAddress(procName.c_str());
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!proc) { // Fallback
|
|
|
|
if (!libHandle)
|
|
|
|
libHandle = LoadLibraryA("opengl32.dll");
|
|
|
|
if (libHandle)
|
2024-03-20 19:35:52 +01:00
|
|
|
proc = (void *)GetProcAddress(libHandle, procName.c_str());
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CWGLManager::swapBuffers()
|
|
|
|
{
|
|
|
|
return SwapBuffers((HDC)CurrentContext.OpenGLWin32.HDc) == TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-03-09 22:34:05 +01:00
|
|
|
|
|
|
|
#endif
|