Skip to content

Instantly share code, notes, and snippets.

@halferty
Created March 29, 2015 08:03
Show Gist options
  • Save halferty/5d612fb7cc624d0e0110 to your computer and use it in GitHub Desktop.
Save halferty/5d612fb7cc624d0e0110 to your computer and use it in GitHub Desktop.
OpenGL win32 multi-window example
#include "stdafx.h"
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#define MAX_LOADSTRING 100
HINSTANCE hInstance;
HWND hWnd, hWnd2;
struct WindowData {
HWND window;
HDC deviceContext;
HGLRC renderContext;
};
LONG WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static PAINTSTRUCT ps;
int pf;
PIXELFORMATDESCRIPTOR pfd;
HDC deviceContext;
HGLRC renderContext;
struct WindowData * windowData = (struct WindowData *) GetWindowLongPtr(hWnd, 0);
switch (uMsg) {
case WM_NCCREATE:
deviceContext = GetDC(hWnd);
renderContext = wglCreateContext(deviceContext);
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pf = ChoosePixelFormat(deviceContext, &pfd);
SetPixelFormat(deviceContext, pf, &pfd);
DescribePixelFormat(deviceContext, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
ReleaseDC(hWnd, deviceContext);
renderContext = wglCreateContext(deviceContext);
wglMakeCurrent(deviceContext, renderContext);
windowData = (struct WindowData *) malloc(sizeof(struct WindowData));
windowData->window = hWnd;
windowData->deviceContext = deviceContext;
windowData->renderContext = renderContext;
SetWindowLongPtr(hWnd, 0, (LONG)windowData);
return TRUE;
case WM_ACTIVATE:
wglMakeCurrent(windowData->deviceContext, windowData->renderContext);
break;
case WM_PAINT:
wglMakeCurrent(windowData->deviceContext, windowData->renderContext);
glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); glVertex2i(0, 1); glVertex2i(-1, -1); glVertex2i(1, -1); glEnd(); glFlush();
wglMakeCurrent(NULL, NULL);
BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps);
return 0;
case WM_SIZE:
glViewport(0, 0, LOWORD(lParam), HIWORD(lParam)); PostMessage(hWnd, WM_PAINT, 0, 0); return 0;
case WM_CHAR:
if (wParam == 27) { PostQuitMessage(0); break; }
else { return 0; }
case WM_DESTROY:
ReleaseDC(hWnd, windowData->deviceContext);
wglDeleteContext(windowData->renderContext);
return 0;
case WM_CLOSE:
PostQuitMessage(0); return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) {
MSG msg;
WNDCLASS wc;
int pf;
PIXELFORMATDESCRIPTOR pfd;
hInstance = GetModuleHandle(NULL);
wc.style = CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(struct WindowData *);
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = L"OpenGL2";
RegisterClass(&wc);
hWnd = CreateWindow(L"OpenGL2", L"Hi there", WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
hWnd2 = CreateWindow(L"OpenGL2", L"Hi there", WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 110, 110, 640, 480, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
ShowWindow(hWnd2, nCmdShow);
while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
wglMakeCurrent(NULL, NULL);
DestroyWindow(hWnd);
return msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment