-
Notifications
You must be signed in to change notification settings - Fork 0
/
GutWin32.cpp
397 lines (335 loc) · 9.19 KB
/
GutWin32.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "stdafx.h"
#include "Gut.h"
#include "GutWin32def.h"
#include "GutWin32.hpp"
#if _GUT_WIN32_OO_
HRESULT IGutEx_Create_3DWnd(IN const char *_wndName, IN int _wndPosX, IN int _wndPosY, IN int _wndWidth, IN int _wndHeight, IN bool _bIsFullScreen, OUT Gut3DWnd **_ppGutWnd, OUT int &_wndIdx)
{
if (!_bIsFullScreen)
{
return (Gut3DWndMgrWin32::Instance())->CreateWnd(_wndName, _wndPosX, _wndPosY, _wndWidth, _wndHeight, GutWndMode::GutWnd_Normal, _ppGutWnd, _wndIdx);
}else
{
return (Gut3DWndMgrWin32::Instance())->CreateWnd(_wndName, _wndPosX, _wndPosY, _wndWidth, _wndHeight, GutWndMode::GutWnd_FullScreen, _ppGutWnd, _wndIdx);
}
}
HRESULT IGutEx_Destroy_3DWnd(IN int _wndIdx)
{
return (Gut3DWndMgrWin32::Instance())->ReleaseWnd(_wndIdx);
}
Gut3DWndMgrWin32 *Gut3DWndMgrWin32::Instance()
{
if(!pGut3DWndMgrWin32)
{
pGut3DWndMgrWin32 = new Gut3DWndMgrWin32();
}
return pGut3DWndMgrWin32;
}
Gut3DWndMgrWin32::Gut3DWndMgrWin32()
{
m_DefaultWndPosX = 100;
m_DefaultWndPosY = 100;
m_DefaultWndWidth = 800;
m_DefaultWndHeight = 600;
m_WndCount = 0;
m_ppWndBuff = new Gut3DWnd *[GUT_WND_MAXNUM];
memset(m_ppWndBuff, 0, sizeof(m_ppWndBuff[0]));
}
Gut3DWndMgrWin32::~Gut3DWndMgrWin32()
{
if(m_ppWndBuff) for(int i = 0; i < m_WndCount; ++i) if(m_ppWndBuff[i]) delete m_ppWndBuff[i];
delete m_ppWndBuff;
}
Gut3DWnd *Gut3DWndMgrWin32::GetGutWnd(int wndIdx)
{
return m_ppWndBuff[wndIdx];
}
Gut3DWnd *Gut3DWndMgrWin32::GetGutWnd(HWND hWnd)
{
map<HWND, int>::iterator it = m_mapWnd2Index.find(hWnd);
if (m_mapWnd2Index.end() != it)
{
return m_ppWndBuff[it->second];
}else
{
return NULL;
}
}
Gut3DWndMgrWin32 *pGut3DWndMgrWin32 = NULL;
#endif
#if _GUT_WIN32_OP_
static bool g_bMsgLoopActive = false;
static HWND g_hWnd = NULL;
static HINSTANCE g_hInstance = NULL;
static HANDLE g_ThreadHandle = NULL;
static unsigned long g_ThreadID = 0;
static int g_iWindowPosX = 100;
static int g_iWindowPosY = 100;
static int g_iWindowWidth = 512;
static int g_iWindowHeight = 512;
void GutSetWindowHandleWin32(HWND hWnd)
{
g_hWnd = hWnd;
}
HWND GutGetWindowHandleWin32(void)
{
return g_hWnd;
}
HINSTANCE GutGetWindowInstanceWin32(void)
{
return g_hInstance;
}
void GutGetWindowSize(int &w, int &h)
{
w = g_iWindowWidth;
h = g_iWindowHeight;
}
void GutGetMouseState(int &x, int &y, int button[3])
{
POINT p;
GetCursorPos(&p);
ScreenToClient(g_hWnd, &p);
x = p.x;
y = p.y;
button[0] = GetKeyState(VK_LBUTTON) & 0x80 ? 1 : 0;
button[1] = GetKeyState(VK_MBUTTON) & 0x80 ? 1 : 0;
button[2] = GetKeyState(VK_RBUTTON) & 0x80 ? 1 : 0;
}
// `窗口信息处理函数`
static LRESULT WINAPI WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// `检查信息种类`
switch (message)
{
case WM_DESTROY:
{
/*
考虑到某些OpenGL程序可能被作为某些图形引擎的外挂插件,固此时如果发送PostQuitMessage消息将意味着释放整个进程空间,
导致主体程序崩溃. by _mTy 2014.03.21
*/
//PostQuitMessage(0);
g_bMsgLoopActive = false;
break;
}
case WM_SIZE: // `窗口大小被改变`
{
int w = LOWORD(lParam);
int h = HIWORD(lParam);
g_iWindowWidth = w;
g_iWindowHeight = h;
if ( g_GutCallBack.OnSize && GutGetGraphicsDeviceType()!= GUT_UNKNOWN )
{
// `有设置窗口大小改变的处理函数的话, 就去调用它.`
g_GutCallBack.OnSize(w, h);
}
break;
}
case WM_PAINT: // `窗口需要重画`
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
if ( g_GutCallBack.OnPaint )
{
// `有设置重绘信息处理函数的话, 就去调用它.`
g_GutCallBack.OnPaint();
}
break;
}
default: // `其它信息都直接交给内置的信息处理函数去做`
{
return DefWindowProc(hwnd, message, wParam, lParam);
break;
}
}
return 0;
} // WndProc
void GutCloseWindow(void)
{
/*
考虑到某些OpenGL程序可能被作为某些图形引擎的外挂插件,固此时如果发送PostQuitMessage消息将意味着释放整个进程空间,
导致主体程序崩溃. by _mTy 2014.03.21
*/
//PostQuitMessage(0);
if (g_hWnd && IsWindow(g_hWnd))
{
DestroyWindow(g_hWnd);
}
}
// `检查Windows操作系统传来的信息`
bool GutProcessMessage(void)
{
if (false == g_bMsgLoopActive)
{
// `收到关闭窗口的信息会返回false来结束循环.`
return false;
}
MSG msg;
// `看看是否任何信息`
if ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
{
// `有信息的话, 就把它送给信息处理函数.`
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return true;
}
bool GutFullScreen(int width, int height)
{
char *title = "fullscreen";
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = 32; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
return false;
}
WNDCLASS window_class;
memset(&window_class, 0, sizeof(WNDCLASS));
// `设置窗口类型`
window_class.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
// `设置信息处理函数为WndProc`
window_class.lpfnWndProc = WndProc;
// `获得目前运行的程序`
window_class.hInstance = GetModuleHandle(NULL);
// `使用默认的鼠标`
window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
// `背景设为黑色, 其实是否设置没有区别.`
window_class.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
// `给定一个注册窗口时使用的名字`
window_class.lpszClassName = title;
// `向操作系统注册一个新的窗口`
if (RegisterClass(&window_class) == 0)
return 0;
DWORD dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
DWORD dwStyle=WS_POPUP; // Windows Style
ShowCursor(FALSE); // Hide Mouse Pointer
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
g_iWindowPosX = g_iWindowPosY = 0;
g_iWindowWidth = width;
g_iWindowHeight = height;
HWND window_handle = CreateWindowEx(
dwExStyle,
"fullscreen",
title,
dwStyle |
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN,
0, 0,
WindowRect.right-WindowRect.left,
WindowRect.bottom-WindowRect.top,
NULL,
NULL,
window_class.hInstance,
NULL
);
if (window_handle == NULL)
return false;
// `记录下用来代表窗口和目前运行程序的对象, 以后可以再用到.`
g_hWnd = window_handle;
g_hInstance = window_class.hInstance;
// `显示窗口`
ShowWindow(window_handle, SW_SHOWNORMAL);
SetActiveWindow(window_handle);
SetForegroundWindow(window_handle);
SetFocus(window_handle);
g_bMsgLoopActive = true;
return true;
}
// `打开窗口`
// `x, y = 窗口左上角的位置`
// `width, height = 窗口大小`
// `title = 窗口标题栏所显示的文字`
bool GutCreateWindow(int x, int y, int width, int height, const char *title)
{
static bool registered = false;
WNDCLASS window_class;
memset(&window_class, 0, sizeof(WNDCLASS));
// `设置窗口类型`
window_class.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
// `设置信息处理函数为WndProc`
window_class.lpfnWndProc = WndProc;
// `获得目前运行的程序`
window_class.hInstance = GetModuleHandle(NULL);
// `使用默认的鼠标`
window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
// `背景设为黑色, 其实是否设置没有区别.`
window_class.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
// `给定一个注册窗口时使用的名字`
window_class.lpszClassName = title;
// `向操作系统注册一个新的窗口`
if ( !registered )
{
if (RegisterClass(&window_class) == 0)
return false;
registered = true;
}
DWORD window_style;
if ( g_GutCallBack.OnSize )
{
// `如果有处理窗口缩放的信息, 才容许鼠标改变窗口大小.`
window_style = WS_OVERLAPPEDWINDOW;
}
else
{
// `如果是否处理窗口缩放的信息, 不容许鼠标改变窗口大小.`
window_style = WS_BORDER | WS_SYSMENU;
}
if ( x<0 || y<0 )
{
// 使用默认值
x = g_iWindowPosX;
y = g_iWindowPosY;
}
if ( width<0 || height < 0 )
{
// 使用默认值
width = g_iWindowWidth;
height = g_iWindowHeight;
}
// `计算窗口的边线会占掉的点数, 必要时要增加窗口大小.`
RECT window_rect;
SetRect(&window_rect, x, y, x+width, y+height);
AdjustWindowRect(&window_rect, window_style, FALSE);
g_iWindowPosX = x;
g_iWindowPosY = y;
g_iWindowWidth = width;
g_iWindowHeight = height;
// `准备工作完成, 打开一个窗口.`
HWND window_handle = CreateWindowEx(
WS_EX_APPWINDOW,
title,
title,
window_style,
window_rect.left, // x
window_rect.top, // y
window_rect.right - window_rect.left, // width
window_rect.bottom - window_rect.top, // height
NULL,
NULL,
window_class.hInstance,
NULL
);
if (window_handle == NULL)
return false;
// `记录下用来代表窗口和目前运行程序的对象, 以后可以再用到.`
g_hWnd = window_handle;
g_hInstance = window_class.hInstance;
// `显示窗口`
ShowWindow(window_handle, SW_SHOWNORMAL);
SetActiveWindow(window_handle);
g_bMsgLoopActive = true;
return true;
}
#endif