-
-
Save zid/4833808913f46fa52af30ca6930a1439 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void gl_context_init(HWND hwnd) | |
{ | |
HDC h; | |
HGLRC tgl, gl; | |
HWND fake_window; | |
unsigned int nformat; | |
int pf; | |
WNDCLASS wc = { | |
.style = CS_OWNDC, | |
.lpfnWndProc = DefWindowProcA, | |
.hInstance = GetModuleHandle(0), | |
.lpszClassName = "dummy" | |
}; | |
PIXELFORMATDESCRIPTOR pfd = { | |
sizeof(PIXELFORMATDESCRIPTOR), | |
1, | |
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, | |
PFD_TYPE_RGBA, | |
32, | |
0, 0, 0, | |
0, 0, 0, | |
0, 0, | |
0, 0, 0, 0, 0, | |
24, 0, 0, 0, 0, 0, 0, 0 | |
}; | |
const int attrib[] = { | |
WGL_CONTEXT_MAJOR_VERSION_ARB, 4, | |
WGL_CONTEXT_MINOR_VERSION_ARB, 5, | |
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, | |
0, 0 | |
}; | |
int pixel_format[] = | |
{ | |
WGL_DRAW_TO_WINDOW_ARB, 1, | |
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, | |
WGL_SUPPORT_OPENGL_ARB, 1, | |
WGL_COLOR_BITS_ARB, 32, | |
WGL_DEPTH_BITS_ARB, 24, | |
WGL_ALPHA_BITS_ARB, 8, | |
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, | |
WGL_SAMPLE_BUFFERS_ARB, 1, | |
WGL_SAMPLES_ARB, 16, | |
0, 0 | |
}; | |
/* Create a fake window */ | |
RegisterClass(&wc); | |
fake_window = CreateWindowEx(0, wc.lpszClassName, "dummy window", 0, 0, 0, 0, 0, 0, 0, wc.hInstance, 0); | |
/* Create a fake DC from fake window */ | |
h = GetDC(fake_window); | |
nformat = ChoosePixelFormat(h, &pfd); | |
/* Set a similar pixel format to what we want, and create a fake opengl context */ | |
SetPixelFormat(h, nformat, &pfd); | |
tgl = wglCreateContext(h); | |
wglMakeCurrent(h, tgl); | |
/* Using the fake context, get the function pointers for the extended functions */ | |
wglCreateContextAttribsARB = (void *)wglGetProcAddress("wglCreateContextAttribsARB"); | |
wglChoosePixelFormatARB = (void *)wglGetProcAddress("wglChoosePixelFormatARB"); | |
/* Destroy the fake window/dc/context */ | |
wglMakeCurrent(h, 0); | |
wglDeleteContext(tgl); | |
ReleaseDC(fake_window, h); | |
DestroyWindow(fake_window); | |
UnregisterClass(wc.lpszClassName, wc.hInstance); | |
/* Now the DC of the real window */ | |
h = GetDC(hwnd); | |
/* Set the real pixel format */ | |
wglChoosePixelFormatARB(h, pixel_format, 0, 1, &pf, &nformat); | |
if(!nformat) | |
{ | |
fprintf(stderr, "Fatal: Pixel format unsupported"); | |
exit(0); | |
} | |
DescribePixelFormat(h, pf, sizeof pfd, &pfd); | |
if(!SetPixelFormat(h, pf, &pfd)) | |
{ | |
fprintf(stderr, "Fatal: Could not set pixel format"); | |
exit(0); | |
} | |
/* Create the real context */ | |
gl = wglCreateContextAttribsARB(h, 0, attrib); | |
if(!gl) | |
{ | |
fprintf(stderr, "Fatal: Couldn't create OpenGL context"); | |
exit(0); | |
} | |
wglMakeCurrent(NULL, NULL); | |
wglDeleteContext(tgl); | |
wglMakeCurrent(h, gl); | |
ReleaseDC(hwnd, h); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment