summaryrefslogtreecommitdiff
path: root/source/tests/win32/01-window/02_multi_window.cpp
blob: e83f1a38caa9d085c50229da2e1800d3378deaad (plain)
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
#include "config.h"

#if _run_app == _multi_window

#include <windows.h>
#include <GL/GfxDevice.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 WinMain(_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 = "OpenGL2";

	RegisterClass(&wc); 
	hWnd = CreateWindow("OpenGL2", "Hi there", WS_VSCROLL|WS_TILEDWINDOW, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
	hWnd2 = CreateWindow("OpenGL2", "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;
}

#endif // _run_app == _multi_window