-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMain.cpp
More file actions
279 lines (255 loc) · 6.88 KB
/
Main.cpp
File metadata and controls
279 lines (255 loc) · 6.88 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 2020, Vitali Baumtrok.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <gl/GL.h>
#define ERR_NONE 0
#define ERR_REGCLS 1
#define ERR_CRWIN_AWR 2
#define ERR_CRWIN 3
#define ERR_DC 4
#define ERR_CPF 5
#define ERR_SPF 6
#define ERR_RC 7
struct {
int code;
LPCWSTR message;
} err = { ERR_NONE, nullptr };
struct {
LPCWSTR className;
LPCSTR classNameChar;
LPCWSTR title;
HWND hndl;
HDC deviceContext;
HGLRC renderContext;
int prevX, prevY, width, height, prevWidth, prevHeight, resX, resY;
bool fullscreen;
} window = { L"OpenGL", "OpenGL", L"OpenGL Example", nullptr, nullptr, nullptr, 0, 0, 0, 0, 0, 0, 640, 480, false };
static void
draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2i(0, 1);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2i(-1, -1);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2i(1, -1);
glEnd();
glFlush();
}
static void
setFullscreen(bool fullscreen)
{
DWORD style = GetWindowLong(window.hndl, GWL_STYLE);
if (fullscreen)
{
RECT rect;
MONITORINFO mi = { sizeof(mi) };
GetWindowRect(window.hndl, &rect);
window.prevX = rect.left;
window.prevY = rect.top;
window.prevWidth = rect.right - rect.left;
window.prevHeight = rect.bottom - rect.top;
GetMonitorInfo(MonitorFromWindow(window.hndl, MONITOR_DEFAULTTOPRIMARY), &mi);
SetWindowLong(window.hndl, GWL_STYLE, style & ~WS_OVERLAPPEDWINDOW);
SetWindowPos(window.hndl, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOOWNERZORDER | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
}
else
{
MONITORINFO mi = { sizeof(mi) };
UINT flags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_SHOWWINDOW;
GetMonitorInfo(MonitorFromWindow(window.hndl, MONITOR_DEFAULTTOPRIMARY), &mi);
SetWindowLong(window.hndl, GWL_STYLE, style | WS_OVERLAPPEDWINDOW);
SetWindowPos(window.hndl, HWND_NOTOPMOST, window.prevX, window.prevY, window.prevWidth, window.prevHeight, flags);
}
}
static LRESULT CALLBACK
wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;
switch (message)
{
case WM_PAINT:
PAINTSTRUCT ps;
draw();
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_SIZE:
glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
break;
case WM_KEYDOWN:
/* ESC */
if (wParam == 27)
PostMessage(hWnd, WM_CLOSE, 0, 0);
/* F11 */
else if (wParam == 122)
setFullscreen(window.fullscreen = !window.fullscreen);
break;
case WM_CLOSE:
wglMakeCurrent(window.deviceContext, NULL);
wglDeleteContext(window.renderContext);
ReleaseDC(hWnd, window.deviceContext);
DestroyWindow(hWnd);
/* stop event queue thread */
PostQuitMessage(0);
break;
default:
result = DefWindowProc(hWnd, message, wParam, lParam);
}
return result;
}
static void
registerClass(HINSTANCE instance)
{
WNDCLASSEXW wcex;
memset(&wcex, 0, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = (WNDPROC)wndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = instance;
wcex.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = window.className;
wcex.hIconSm = NULL;
if (!RegisterClassExW(&wcex))
{
err.code = ERR_REGCLS;
err.message = L"RegisterClassExW failed: Can not register window class.";
}
}
static void
createWindow(HINSTANCE instance)
{
if (err.code == ERR_NONE)
{
DWORD style = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
RECT rect = { 0, 0, window.resX, window.resY };
if (AdjustWindowRect(&rect, style, false))
{
/* compute window size including border */
window.width = rect.right - rect.left;
window.height = rect.bottom - rect.top;
window.hndl = CreateWindowW(window.className, window.title, style, 0, 0, window.width, window.height, nullptr, nullptr, instance, nullptr);
if (!window.hndl)
{
err.code = ERR_CRWIN;
err.message = L"CreateWindowW failed: Can not create window.";
}
}
else
{
err.code = ERR_CRWIN_AWR;
err.message = L"AdjustWindowRect failed: Can not create window.";
}
}
}
static void
centerWindow()
{
if (err.code == ERR_NONE)
{
RECT rect;
MONITORINFO mi = { sizeof(mi) };
GetMonitorInfo(MonitorFromWindow(window.hndl, MONITOR_DEFAULTTONEAREST), &mi);
int x = (mi.rcMonitor.right - mi.rcMonitor.left - window.width) / 2;
int y = (mi.rcMonitor.bottom - mi.rcMonitor.top - window.height) / 2;
SetWindowPos(window.hndl, 0, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}
}
static void
createContext()
{
if (err.code == ERR_NONE)
{
window.deviceContext = GetDC(window.hndl);
if (window.deviceContext)
{
int pixelFormat;
PIXELFORMATDESCRIPTOR pixelFormatDesc;
/* initialize bits to 0 */
memset(&pixelFormatDesc, 0, sizeof(PIXELFORMATDESCRIPTOR));
pixelFormatDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelFormatDesc.nVersion = 1;
pixelFormatDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pixelFormatDesc.iPixelType = PFD_TYPE_RGBA;
pixelFormatDesc.cColorBits = 32;
pixelFormatDesc.cAlphaBits = 8;
pixelFormatDesc.cDepthBits = 24;
pixelFormat = ChoosePixelFormat(window.deviceContext, &pixelFormatDesc);
if (pixelFormat)
{
if (SetPixelFormat(window.deviceContext, pixelFormat, &pixelFormatDesc))
{
window.renderContext = wglCreateContext(window.deviceContext);
if (!window.renderContext)
{
err.code = ERR_RC;
err.message = L"wglCreateContext failed: Can not create render context.";
}
}
else
{
err.code = ERR_SPF;
err.message = L"SetPixelFormat failed: Can not create render context.";
}
}
else
{
err.code = ERR_CPF;
err.message = L"ChoosePixelFormat failed: Can not create render context.";
}
}
else
{
err.code = ERR_DC;
err.message = L"GetDC failed: Can not create device context.";
}
}
}
int APIENTRY
wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
registerClass(hInstance);
createWindow(hInstance);
createContext();
if (err.code == ERR_NONE)
{
wglMakeCurrent(window.deviceContext, window.renderContext);
ShowWindow(window.hndl, nCmdShow);
centerWindow();
UpdateWindow(window.hndl);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
wchar_t *title = new wchar_t[10];
swprintf_s(title, 10, L"Error %d", err.code);
MessageBox(NULL, err.message, title, MB_OK);
}
return err.code;
}