Debian Codegraph @

examples/deprecated_library/cpp_winapi_library_tester/main.cpp

raw symbols SCIP
1 /*
2 Copyright 2013-2017 Jan Adamec, Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14
15 This is a MS Windows test project for our library in src/deprecated_library/.
16 It uses library API (exposed in castleengine.h), and uses a compiled dynamic
17 library with the engine.
18
19 Using the engine units directly gives you a complete object-oriented API
20 in ObjectPascal to do everything :) The C library API offers only a small
21 subset of engine functionality. The library is useful to access the engine
22 from other programming languages.
23
24 HOW TO RUN THIS: prior to running this project, compile and copy the shared
25 library (in src/deprecated_library/) to a place where it can be loaded, it means
26 copy castleengine.dll to this project folder, or anywhere on $PATH.
27
28 You will also need other dynamic libraries (zlib1.dll, libpng.dll, ogg.dll,
29 OpenAL32.dll, ...) that are shipped with Castle Game Engine.
30 See https://castle-engine.io/compiling_from_source.php#section_Windows .
31 */
32
33 #if !defined(WIN32_LEAN_AND_MEAN)
34 #define WIN32_LEAN_AND_MEAN
35 #endif
36
37 #include <windows.h>
38 #include <windowsx.h>
39 #include <Commdlg.h>
40 #include <GL/gl.h>
41 #include <sstream>
42 #include <stdexcept>
43
44 #include "../../../src/deprecated_library/castleengine.h"
45
46 #if defined(_DEBUG)
47 #include <crtdbg.h>
48 #endif
49
50 //-----------------------------------------------------------------------------
51 // Globals.
52 //-----------------------------------------------------------------------------
53
54 HWND g_hWnd = NULL;
55 HINSTANCE g_hInstance;
56 HDC g_hDC = NULL;
57 HGLRC g_hRC = NULL;
58 int g_windowWidth;
59 int g_windowHeight;
60 bool g_isFullScreen;
61 bool g_hasFocus;
62
63 //-----------------------------------------------------------------------------
64 int g_nViewpointCount = 0;
65 int g_nCurrentViewpoint;
66
67 //-----------------------------------------------------------------------------
68 // Function Prototypes.
69 //-----------------------------------------------------------------------------
70
71 void Cleanup();
72 HWND CreateAppWindow(const WNDCLASSEX &wcl, const char *pszTitle);
73 bool Init();
74 void InitGL();
75 void ToggleFullScreen();
76 void OnIdle();
77 void ShowOpenFileDialog();
78 LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
79
80 //-----------------------------------------------------------------------------
81 // Functions.
82 //-----------------------------------------------------------------------------
83
84 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
85 {
86 #if defined _DEBUG
87 _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
88 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
89 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
90 #endif
91
92 MSG msg = {0};
93 WNDCLASSEX wcl = {0};
94
95 wcl.cbSize = sizeof(wcl);
96 wcl.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
97 wcl.lpfnWndProc = WindowProc;
98 wcl.cbClsExtra = 0;
99 wcl.cbWndExtra = 0;
100 wcl.hInstance = g_hInstance = hInstance;
101 wcl.hIcon = LoadIcon(0, IDI_APPLICATION);
102 wcl.hCursor = LoadCursor(0, IDC_ARROW);
103 wcl.hbrBackground = 0;
104 wcl.lpszMenuName = 0;
105 wcl.lpszClassName = "GL3WindowClass";
106 wcl.hIconSm = 0;
107 if (!RegisterClassEx(&wcl))
108 return 0;
109
110 g_hWnd = CreateAppWindow(wcl, "CastleLib Test App");
111 if (g_hWnd==NULL)
112 return 0;
113
114 if (!Init()) // Init OpenGL context and our library
115 return 0;
116
117 ShowWindow(g_hWnd, nShowCmd);
118 UpdateWindow(g_hWnd);
119
120 while (true)
121 {
122 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
123 {
124 if (msg.message == WM_QUIT)
125 break;
126
127 TranslateMessage(&msg);
128 DispatchMessage(&msg);
129 }
130
131 if (msg.message == WM_QUIT)
132 break;
133
134 if (g_hasFocus)
135 OnIdle();
136 else
137 WaitMessage();
138 }
139 return (int)msg.wParam;
140 }
141
142 //-----------------------------------------------------------------------------
143 LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
144 {
145 switch (msg)
146 {
147 case WM_ACTIVATE:
148 switch (wParam)
149 {
150 default:
151 break;
152
153 case WA_ACTIVE:
154 case WA_CLICKACTIVE:
155 g_hasFocus = true;
156 break;
157
158 case WA_INACTIVE:
159 if (g_isFullScreen)
160 ShowWindow(hWnd, SW_MINIMIZE);
161 g_hasFocus = false;
162 break;
163 }
164 break;
165
166 case WM_ERASEBKGND:
167 if (IsWindowEnabled(hWnd))
168 {
169 wglMakeCurrent(g_hDC, g_hRC);
170 CGE_Render(); // do not draw in WM_PAINT (inspired in CastleWindow)
171 SwapBuffers(g_hDC);
172 }
173 return TRUE;
174
175 case WM_LBUTTONDOWN:
176 case WM_RBUTTONDOWN:
177 CGE_MouseDown(GET_X_LPARAM(lParam), g_windowHeight-1-GET_Y_LPARAM(lParam), msg==WM_LBUTTONDOWN, 0);
178 SetCapture(hWnd);
179 break;
180
181 case WM_MOUSEMOVE:
182 CGE_Motion(GET_X_LPARAM(lParam), g_windowHeight-1-GET_Y_LPARAM(lParam), 0);
183 break;
184
185 case WM_LBUTTONUP:
186 case WM_RBUTTONUP:
187 CGE_MouseUp(GET_X_LPARAM(lParam), g_windowHeight-1-GET_Y_LPARAM(lParam),
188 msg==WM_LBUTTONUP, 0, true);
189 if (GetCapture()==hWnd)
190 ReleaseCapture();
191 break;
192
193 case WM_MOUSEWHEEL:
194 CGE_MouseWheel(GET_WHEEL_DELTA_WPARAM(wParam), true);
195 break;
196
197 case WM_CHAR:
198 switch (wParam)
199 {
200 case VK_ESCAPE:
201 PostMessage(hWnd, WM_CLOSE, 0, 0);
202 break;
203
204 case 's':
205 CGE_SaveScreenshotToFile("screenshot.jpg");
206 break;
207
208 case 'f':
209 CGE_SetNavigationType(ecgenavFly);
210 break;
211
212 case 'e':
213 CGE_SetNavigationType(ecgenavExamine);
214 break;
215
216 case 'o':
217 {
218 int nVal = CGE_GetVariableInt(ecgevarEffectSSAO);
219 if (nVal < 0) nVal = 0;
220 CGE_SetVariableInt(ecgevarEffectSSAO, 1-nVal);
221 }
222 break;
223
224 case 'h':
225 {
226 int nVal = CGE_GetVariableInt(ecgevarWalkHeadBobbing);
227 if (nVal < 0) nVal = 0;
228 CGE_SetVariableInt(ecgevarWalkHeadBobbing, 1-nVal);
229 }
230 break;
231
232 default:
233 break;
234 }
235 break;
236
237 case WM_KEYDOWN:
238 switch (wParam)
239 {
240 case VK_PRIOR: // PageUp - move to previous viewpoint
241 if (g_nViewpointCount > 0)
242 {
243 if (g_nCurrentViewpoint > 0)
244 g_nCurrentViewpoint--;
245 else
246 g_nCurrentViewpoint = g_nViewpointCount-1;
247 CGE_MoveToViewpoint(g_nCurrentViewpoint, true);
248 }
249 break;
250
251 case VK_NEXT: // PageDown - move to next viewpoint
252 if (g_nViewpointCount > 0)
253 {
254 if (g_nCurrentViewpoint < g_nViewpointCount-1)
255 g_nCurrentViewpoint++;
256 else
257 g_nCurrentViewpoint = 0;
258 CGE_MoveToViewpoint(g_nCurrentViewpoint, true);
259 }
260 break;
261 default:
262 break;
263 }
264 break;
265
266 case WM_DESTROY:
267 Cleanup();
268 PostQuitMessage(0);
269 return 0;
270
271 case WM_SIZE:
272 g_windowWidth = LOWORD(lParam);
273 g_windowHeight = HIWORD(lParam);
274 CGE_Resize(g_windowWidth, g_windowHeight);
275 break;
276
277 case WM_SYSKEYDOWN:
278 if (wParam == VK_RETURN) // Alt + Enter
279 ToggleFullScreen();
280 break;
281
282 default:
283 break;
284 }
285
286 return DefWindowProc(hWnd, msg, wParam, lParam);
287 }
288
289 //-----------------------------------------------------------------------------
290 void Cleanup()
291 {
292 if (g_hDC)
293 {
294 if (g_hRC)
295 {
296 wglMakeCurrent(g_hDC, NULL);
297 wglDeleteContext(g_hRC);
298 g_hRC = NULL;
299 }
300
301 ReleaseDC(g_hWnd, g_hDC);
302 g_hDC = NULL;
303 }
304 }
305
306 //-----------------------------------------------------------------------------
307 HWND CreateAppWindow(const WNDCLASSEX &wcl, const char *pszTitle)
308 {
309 DWORD wndExStyle = WS_EX_OVERLAPPEDWINDOW;
310 DWORD wndStyle = WS_OVERLAPPEDWINDOW/* | WS_CLIPCHILDREN | WS_CLIPSIBLINGS*/;
311
312 HWND hWnd = CreateWindowEx(wndExStyle, wcl.lpszClassName, pszTitle,
313 wndStyle, 0, 0, 0, 0, 0, 0, wcl.hInstance, 0);
314 if (hWnd)
315 {
316 // Create a window that is centered on the desktop. It's exactly 1/4 the
317 // size of the desktop.
318 int screenWidth = GetSystemMetrics(SM_CXSCREEN);
319 int screenHeight = GetSystemMetrics(SM_CYSCREEN);
320 int halfScreenWidth = screenWidth / 2;
321 int halfScreenHeight = screenHeight / 2;
322 int left = (screenWidth - halfScreenWidth) / 2;
323 int top = (screenHeight - halfScreenHeight) / 2;
324 RECT rc = {0};
325
326 SetRect(&rc, left, top, left + halfScreenWidth, top + halfScreenHeight);
327 AdjustWindowRectEx(&rc, wndStyle, FALSE, wndExStyle);
328 MoveWindow(hWnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE);
329
330 GetClientRect(hWnd, &rc);
331 g_windowWidth = rc.right - rc.left;
332 g_windowHeight = rc.bottom - rc.top;
333 }
334
335 return hWnd;
336 }
337
338 //-----------------------------------------------------------------------------
339 int __cdecl OpenGlLibraryCallback(int eCode, int iParam1, int iParam2, const char *szParam)
340 {
341 switch (eCode)
342 {
343 case ecgelibNeedsDisplay:
344 InvalidateRect(g_hWnd, NULL, TRUE);
345 return 1;
346
347 case ecgelibSetMouseCursor:
348 {
349 HCURSOR hNewCur = NULL;
350 switch (iParam1)
351 {
352 case ecgecursorWait: hNewCur = LoadCursor(NULL, IDC_WAIT); break;
353 case ecgecursorHand: hNewCur = LoadCursor(NULL, IDC_HAND); break;
354 case ecgecursorText: hNewCur = LoadCursor(NULL, IDC_IBEAM); break;
355 default: hNewCur = LoadCursor(NULL, IDC_ARROW);
356 }
357 SetCursor(hNewCur);
358 }
359 return 1;
360
361 case ecgelibNavigationTypeChanged:
362 // TODO: update nav. buttons
363 return 1;
364 }
365 return 0;
366 }
367
368 //-----------------------------------------------------------------------------
369 bool Init()
370 {
371 try
372 {
373 InitGL();
374
375 char applicationConfigDirectory[1000];
376 int bytes = GetModuleFileName(NULL, applicationConfigDirectory, 1000);
377
378 CGE_LoadLibrary();
379 // Note: the log output is in
380 // c:/Users/<username>/AppData/Local/cpp_winapi_library_tester/cpp_winapi_library_tester.log
381 // See https://castle-engine.io/manual_log.php
382 CGE_Initialize(applicationConfigDirectory);
383 CGE_Open(ecgeofLog, g_windowWidth, g_windowHeight, 96);
384 CGE_SetLibraryCallbackProc(OpenGlLibraryCallback);
385 CGE_SetUserInterface(true);
386 //CGE_LoadSceneFromFile("c:\\projects\\humanoid_stand.wrl");
387 ShowOpenFileDialog();
388 return true;
389 }
390 catch (const std::exception &e)
391 {
392 std::ostringstream msg;
393
394 msg << "Application initialization failed!" << std::endl << std::endl;
395 msg << e.what();
396
397 MessageBox(g_hWnd, msg.str().c_str(), "Error", MB_ICONERROR);
398 return false;
399 }
400 }
401
402 //-----------------------------------------------------------------------------
403 void InitGL()
404 {
405 if (!(g_hDC = GetDC(g_hWnd)))
406 throw std::runtime_error("GetDC() failed.");
407
408 // Create and set a pixel format for the window.
409 PIXELFORMATDESCRIPTOR pfd = {0};
410 pfd.nSize = sizeof(pfd);
411 pfd.nVersion = 1;
412 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
413 pfd.iPixelType = PFD_TYPE_RGBA;
414 pfd.cColorBits = 24;
415 pfd.cDepthBits = 24;
416 pfd.iLayerType = PFD_MAIN_PLANE;
417
418 bool bMsInitSuccess = false;
419 /*if (WGL_ARB_multisample && WGL_ARB_pixel_format && wglChoosePixelFormatARB)
420 {
421 float fVisualAttr[2];
422 int *pVisualAttrList = LGlMsCreateOpenGLContextAttrList(DoubleBuffered, RGBA, MultiSampling);
423 fVisualAttr[0] = fVisualAttr[1] = 0;
424 unsigned uiReturnedFormats = 0;
425 int pf;
426 bMsInitSuccess = wglChoosePixelFormatARB(g_hDC, (GLint*)pVisualAttrList, fVisualAttr, 1, &pf, &uiReturnedFormats);
427 free(pVisualAttrList);
428
429 if (bMsInitSuccess && uiReturnedFormats >= 1)
430 SetPixelFormat(g_hDC, pf, nil);
431 else
432 bMsInitSuccess = false;
433 }*/
434
435 if (!bMsInitSuccess)
436 {
437 int pf = ChoosePixelFormat(g_hDC, &pfd);
438 if (!SetPixelFormat(g_hDC, pf, &pfd))
439 throw std::runtime_error("SetPixelFormat() failed.");
440 }
441
442 g_hRC = wglCreateContext(g_hDC);
443
444 if (!wglMakeCurrent(g_hDC, g_hRC))
445 throw std::runtime_error("wglMakeCurrent() failed for OpenGL context.");
446
447 /*
448 // Display the OpenGL version string in the window title bar.
449 const char *pszGLVersion = reinterpret_cast<const char *>(glGetString(GL_VERSION));
450 std::ostringstream text;
451 text << "GL_VERSION: " << pszGLVersion;
452 SetWindowText(g_hWnd, text.str().c_str());*/
453 }
454
455 //-----------------------------------------------------------------------------
456 void ToggleFullScreen()
457 {
458 static DWORD savedExStyle;
459 static DWORD savedStyle;
460 static RECT rcSaved;
461
462 g_isFullScreen = !g_isFullScreen;
463
464 if (g_isFullScreen)
465 {
466 // Moving to full screen mode.
467 savedExStyle = GetWindowLong(g_hWnd, GWL_EXSTYLE);
468 savedStyle = GetWindowLong(g_hWnd, GWL_STYLE);
469 GetWindowRect(g_hWnd, &rcSaved);
470
471 SetWindowLong(g_hWnd, GWL_EXSTYLE, 0);
472 SetWindowLong(g_hWnd, GWL_STYLE, WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
473 SetWindowPos(g_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
474
475 g_windowWidth = GetSystemMetrics(SM_CXSCREEN);
476 g_windowHeight = GetSystemMetrics(SM_CYSCREEN);
477
478 SetWindowPos(g_hWnd, HWND_TOPMOST, 0, 0, g_windowWidth, g_windowHeight, SWP_SHOWWINDOW);
479 }
480 else
481 {
482 // Moving back to windowed mode.
483 SetWindowLong(g_hWnd, GWL_EXSTYLE, savedExStyle);
484 SetWindowLong(g_hWnd, GWL_STYLE, savedStyle);
485 SetWindowPos(g_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
486
487 g_windowWidth = rcSaved.right - rcSaved.left;
488 g_windowHeight = rcSaved.bottom - rcSaved.top;
489
490 SetWindowPos(g_hWnd, HWND_NOTOPMOST, rcSaved.left, rcSaved.top, g_windowWidth, g_windowHeight, SWP_SHOWWINDOW);
491 }
492 CGE_Resize(g_windowWidth, g_windowHeight);
493 }
494
495 //-----------------------------------------------------------------------------
496 void OnIdle()
497 {
498 CGE_Update();
499 }
500
501 //-----------------------------------------------------------------------------
502 void ShowOpenFileDialog()
503 {
504 char szFile[MAX_PATH];
505 szFile[0] = 0;
506
507 OPENFILENAME ofn;
508 ZeroMemory(&ofn, sizeof(OPENFILENAME));
509 ofn.lStructSize = sizeof(OPENFILENAME);
510 ofn.hwndOwner = g_hWnd;
511 ofn.lpstrFile = szFile;
512 ofn.nMaxFile = MAX_PATH;
513 ofn.lpstrFilter = "VRML files (*.wrl)\0*.WRL\0";
514 ofn.nFilterIndex = 0;
515 ofn.lpstrFileTitle = NULL;
516 ofn.nMaxFileTitle = 0;
517 ofn.lpstrInitialDir = NULL;
518 ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
519 if (GetOpenFileName(&ofn))
520 {
521 CGE_LoadSceneFromFile(szFile);
522
523 // show viewpoints available
524 std::string sViewpointList;
525 int nCount = CGE_GetViewpointsCount();
526 for (int i = 0; i < nCount; i++)
527 {
528 char sName[512];
529 CGE_GetViewpointName(i, sName, 512);
530 sViewpointList += sName;
531 sViewpointList += "\n";
532 }
533 MessageBox(g_hWnd, sViewpointList.c_str(), "Viewpoints found", 0);
534
535 /*char sGlInfo[1024];
536 CGE_GetOpenGLInformation(sGlInfo, 1024);
537 MessageBox(g_hWnd, sGlInfo, "GL Info", 0);*/
538
539 InvalidateRect(g_hWnd, NULL, TRUE);
540 g_nViewpointCount = nCount;
541 g_nCurrentViewpoint = 0;
542 }
543 }