Original Post
#include <windows.h>
HWND hWnd = 0;
BOOL done = FALSE;
LRESULT WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
/* Ignore paint messages */
case WM_PAINT:
case WM_ERASEBKGND:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
/* Creates a GL context attached to the window owned
by the other thread and paints the client area blue */
DWORD GLThread(LPVOID lpParams) {
PIXELFORMATDESCRIPTOR pfd = {0};
HDC hDC = GetDC(hWnd);
HGLRC hRC = 0;
/* Set absolute minimum format attributes; i.e. select default mode */
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
while (!done) {
glClear(GL_COLOR_BUFFER_BIT);
SwapBuffers(hDC);
/* Yield to other threads */
Sleep(0);
}
return 0;
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd) {
WNDCLASSEX wcx = {0};
HANDLE hThread = 0;
DWORD iThread = 0;
DWORD wait = 0;
MSG msg = {0};
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wcx.lpfnWindowProc = WinProc;
wcx.hInstance = hInstance;
wcx.lpszClassName = "MTGL";
/* No background brush */
hWnd = CreateWindowEx(WS_EX_APPWINDOW, wcx.lpszClassName, "Untitled",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 320,
240, 0, 0, hInstance, 0);
ShowWindow(hWnd, nShowCmd);
/* Start OpenGL */
hThread = CreateThread(0, 0, GLThread, 0, &iThread);
while (!done) {
if (GetMessage(&msg, 0, 0) > 0) {
DispatchMessage(&msg);
} else {
done = TRUE;
}
}
return (INT)msg.wParam;
}