-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApplication.cpp
More file actions
787 lines (682 loc) · 18.4 KB
/
Application.cpp
File metadata and controls
787 lines (682 loc) · 18.4 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
==========================================================================
contains application class
class Application
==========================================================================
*/
#define NOMINMAX
#include "Application.h"
#include "CoreEngine.h"
namespace DifferentialArts
{
/*
============================================================
Application class for win32
============================================================
*/
void Application::MsgBox(LPCTSTR text, LPCTSTR title)
{
MessageBox(NULL, text, title, MB_OK);
}
void Application::MsgBoxAndQuit(LPCTSTR text, LPCTSTR title)
{
MessageBox(NULL, text, title, MB_OK);
this->end();
}
void Application::end(void)
{
this->mlog->LogInfo(LString("Application::end() was called"));
this->winMessage = false;
PostQuitMessage(0);
}
bool Application::isRunning()
{
return this->winMessage;
}
float Application::GetMouseSensitivity(void)
{
return this->mouseSensitivity;
}
void Application::WindowRefresh(void)
{
ShowWindow(this->m_hWnd, SW_SHOWNORMAL); // Show the window
UpdateWindow(this->m_hWnd); // Draw the window
SetFocus(this->m_hWnd);
}
void Application::SetMouseSensitivity(float m)
{
if(m > 0.1f)
{
m = 0.1f;
} else if(m<=0.001f)
{
m = 0.001f;
}
this->mouseSensitivity = m;
}
bool Application::isActivated(void)
{
return this->active;
}
bool Application::UpdateGameWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, float fov,
double zNear, double zFar)
{
if(mbCEGUIUpdateInputInjection)
{
switch(msg)
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
if (!HIWORD(wParam)) // Check Minimization State
{
this->active=true; // Program Is Active
if(this->pause)
{
this->pause = false;
myGlobalGame->mSoundSystem->ResumeAllSounds();
}
}
else
{
this->active=false; // Program Is No Longer Active
if(!this->pause)
{
this->pause = true;
this->Mouse.Show();
myGlobalGame->mSoundSystem->StopAllSounds();
}
}
return false; // Return To The Message Loop
}
case WM_SYSCOMMAND: // Intercept System Commands
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return false; // Prevent From Happening
}
break; // Exit
case WM_CREATE:
break;
case WM_MOUSEMOVE:
if(!this->m_bFullScreen)
{
//this->Mouse.x = LOWORD(lParam);
//this->Mouse.y = HIWORD(lParam);
this->Mouse.x = int((float(LOWORD(lParam))/float(this->m_rRect.right)) * this->m_rWindowRect.right);
this->Mouse.y = int((float(HIWORD(lParam))/float(this->m_rRect.bottom)) * this->m_rWindowRect.bottom);
} else {
this->Mouse.x = LOWORD(lParam);
this->Mouse.y = HIWORD(lParam);
}
//POINT pt;
//GetCursorPos(&pt);
//this->Mouse.x = pt.x;
//this->Mouse.y = pt.y;
break;
case WM_KEYDOWN:
this->mKeyWParam = wParam;
this->mKeyLParam = lParam;
/*
switch(wParam)
{
case RX_LSHIFT:
CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::LeftShift);
break;
case RX_RSHIFT:
CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::RightShift);
break;
}*/
break;
case WM_CHAR:
this->characterReleased = wParam;
break;
case WM_SIZE: // If the window is resized
if(!this->isFullScreen())
{
this->SizeOpenGLScreen(this->width = LOWORD(lParam), this->height = HIWORD(lParam), fov, zNear, zFar);// LoWord=Width, HiWord=Height
GetClientRect(hWnd, &this->getRect()); // Get the window rectangle
}
if(wParam == SIZE_MINIMIZED)
{
myGlobalGame->mbEnginePaused = true;
} else if(wParam == SIZE_RESTORED)
{
myGlobalGame->mbEnginePaused = false;
}
break;
case WM_MOVE:
this->windowPosition.x = (int)LOWORD(lParam);
this->windowPosition.y = (int)HIWORD(lParam);
break;
case WM_LBUTTONDOWN:
this->Mouse.LeftButton.SetState(MOUSE_DOWN);
break;
case WM_LBUTTONUP:
this->Mouse.LeftButton.SetState(MOUSE_UP);
this->Mouse.LeftButton.SetRelease(true);
break;
case WM_RBUTTONDOWN:
this->Mouse.RightButton.SetState(MOUSE_DOWN);
break;
case WM_RBUTTONUP:
this->Mouse.RightButton.SetState(MOUSE_UP);
this->Mouse.RightButton.SetRelease(true);
break;
case WM_MBUTTONDOWN:
this->Mouse.MiddleButton.SetState(MOUSE_DOWN);
break;
case WM_MBUTTONUP:
this->Mouse.MiddleButton.SetState(MOUSE_UP);
this->Mouse.MiddleButton.SetRelease(true);
break;
case WM_MOUSEWHEEL:
this->Mouse.mouseWheel = (short)HIWORD(wParam);
break;
/*
case WM_DESTROY:
this->mlog.LogSuccessText(LString("WM_DESTROY case confirmed"));
this->end();
return false;
break;
*/
case WM_CLOSE:
this->mlog->LogSuccessText(LString("WM_CLOSE case confirmed"));
this->end();
return false;
break;
case WM_QUIT:
this->mlog->LogSuccessText(LString("WM_QUIT case confirmed"));
this->end();
return false;
break;
default:
/*
if(wParam == RX_LSHIFT)
{
CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::LeftShift);
} else {
if(CEGUI::System::getSingletonPtr() != 0)
{
CEGUI::System::getSingleton().injectKeyUp(CEGUI::Key::LeftShift);
}
}
if(wParam == RX_RSHIFT)
{
CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::RightShift);
} else {
if(CEGUI::System::getSingletonPtr() != 0)
{
CEGUI::System::getSingleton().injectKeyUp(CEGUI::Key::RightShift);
}
} */
break;
}
} else {
switch(msg)
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
if (!HIWORD(wParam)) // Check Minimization State
{
this->active=true; // Program Is Active
if(this->pause)
{
this->pause = false;
myGlobalGame->mSoundSystem->ResumeAllSounds();
}
}
else
{
this->active=false; // Program Is No Longer Active
if(!this->pause)
{
this->pause = true;
this->Mouse.Show();
myGlobalGame->mSoundSystem->StopAllSounds();
}
}
return false; // Return To The Message Loop
}
case WM_SYSCOMMAND: // Intercept System Commands
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return false; // Prevent From Happening
}
break; // Exit
case WM_CREATE:
break;
case WM_MOUSEMOVE:
if(!this->m_bFullScreen)
{
//this->Mouse.x = LOWORD(lParam);
//this->Mouse.y = HIWORD(lParam);
this->Mouse.x = int((float(LOWORD(lParam))/float(this->m_rRect.right)) * this->m_rWindowRect.right);
this->Mouse.y = int((float(HIWORD(lParam))/float(this->m_rRect.bottom)) * this->m_rWindowRect.bottom);
} else {
this->Mouse.x = LOWORD(lParam);
this->Mouse.y = HIWORD(lParam);
}
//POINT pt;
//GetCursorPos(&pt);
//this->Mouse.x = pt.x;
//this->Mouse.y = pt.y;
break;
case WM_KEYDOWN:
this->mKeyWParam = wParam;
this->mKeyLParam = lParam;
break;
case WM_CHAR:
this->characterReleased = wParam;
break;
case WM_SIZE: // If the window is resized
if(!this->isFullScreen())
{
this->SizeOpenGLScreen(this->width = LOWORD(lParam), this->height = HIWORD(lParam), fov, zNear, zFar);// LoWord=Width, HiWord=Height
GetClientRect(hWnd, &this->getRect()); // Get the window rectangle
}
if(wParam == SIZE_MINIMIZED)
{
myGlobalGame->mbEnginePaused = true;
} else if(wParam == SIZE_RESTORED)
{
myGlobalGame->mbEnginePaused = false;
}
break;
case WM_MOVE:
this->windowPosition.x = (int)LOWORD(lParam);
this->windowPosition.y = (int)HIWORD(lParam);
break;
case WM_LBUTTONDOWN:
this->Mouse.LeftButton.SetState(MOUSE_DOWN);
break;
case WM_LBUTTONUP:
this->Mouse.LeftButton.SetState(MOUSE_UP);
this->Mouse.LeftButton.SetRelease(true);
break;
case WM_RBUTTONDOWN:
this->Mouse.RightButton.SetState(MOUSE_DOWN);
break;
case WM_RBUTTONUP:
this->Mouse.RightButton.SetState(MOUSE_UP);
this->Mouse.RightButton.SetRelease(true);
break;
case WM_MBUTTONDOWN:
this->Mouse.MiddleButton.SetState(MOUSE_DOWN);
break;
case WM_MBUTTONUP:
this->Mouse.MiddleButton.SetState(MOUSE_UP);
this->Mouse.MiddleButton.SetRelease(true);
break;
case WM_MOUSEWHEEL:
this->Mouse.mouseWheel = (short)HIWORD(wParam);
break;
/*
case WM_DESTROY:
this->mlog.LogSuccessText(LString("WM_DESTROY case confirmed"));
this->end();
return false;
break;
*/
case WM_CLOSE:
this->mlog->LogSuccessText(LString("WM_CLOSE case confirmed"));
this->end();
return false;
break;
case WM_QUIT:
this->mlog->LogSuccessText(LString("WM_QUIT case confirmed"));
this->end();
return false;
break;
default:
break;
}
}
return true;
}
void Application::SizeOpenGLScreen(int w, int h, float fov, double zNear, double zFar)
{
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float)w/(float)h, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
bool Application::bSetupPixelFormat(HDC hdc)
{
mlog->LogInfo(LString("Setup of pixel format started..."));
PIXELFORMATDESCRIPTOR pfd = {0};
int pixelformat;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.dwLayerMask = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = this->mColorBits;
pfd.cDepthBits = this->mDepthBits; //i think if i set this to zero, openGL will use the correct value
pfd.cAccumBits = 0;
pfd.cStencilBits = this->mStencilBits;
pfd.cAlphaBits = this->mAlphaBits;
mlog->LogInfo(LString("Choose pixel format started... "));
// This gets us a pixel format that best matches the one passed in from the device
if ( (pixelformat = ChoosePixelFormat(hdc, &pfd)) == FALSE )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
mlog->LogError(LString("ChoosePixelFormat has failed"));
return false;
}
mlog->LogSuccessText(LString("Choose pixel format is successful"));
mlog->LogInfo(LString("SetPixel format started... "));
// This sets the pixel format that we extracted from above
if (SetPixelFormat(hdc, pixelformat, &pfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
mlog->LogError(LString("SetPixelFormat has failed"));
return false;
}
mlog->LogSuccessText(LString("SetPixel format is successful"));
this->mPFD = pfd;
mlog->LogSuccessText(LString("Setup of pixel format has succeeded!"));
return true;
}
void Application::OverrideDeviceSettings(int refreshRate, unsigned char colorBits,
unsigned char depthBits, unsigned char stencilBits,
unsigned char alphaBits)
{
this->mColorBits = colorBits;
this->mRefreshRate = refreshRate;
this->mDepthBits = depthBits;
this->mStencilBits = stencilBits;
this->mAlphaBits = alphaBits;
}
void Application::ChangeToFullScreen()
{
mlog->LogInfo(LString("Change to fullscreen has started..."));
DEVMODE dmSettings;
memset(&dmSettings,0,sizeof(DEVMODE));
if(!EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&dmSettings))
{
MessageBox(NULL, "Could Not Enum Display Settings", "Error", MB_OK);
return;
}
dmSettings.dmSize = sizeof (DEVMODE);
dmSettings.dmPelsWidth = this->width;
dmSettings.dmPelsHeight = this->height;
dmSettings.dmBitsPerPel = this->mColorBits;
dmSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
dmSettings.dmDisplayFrequency = this->mRefreshRate;
int result = ChangeDisplaySettings(&dmSettings,CDS_FULLSCREEN);
if(result != DISP_CHANGE_SUCCESSFUL)
{
MessageBox(NULL, "Display Mode Not Compatible", "Error", MB_OK);
mlog->LogError(LString("Display mode not compatible for fullscreen"));
PostQuitMessage(0);
}
mlog->LogSuccessText(LString("Change to Fullscreen has succeeded!"));
}
void Application::init_app(Console* pLog)
{
this->mlog = pLog;
}
void Application::setHINSTANCE(HINSTANCE hInstance)
{
this->m_hInstance = hInstance;
}
HINSTANCE Application::getHInstance(void)
{
return this->m_hInstance;
}
HDC Application::getDC(void)
{
return this->m_hDC;
}
HWND Application::getHandle(void)
{
return this->m_hWnd;
}
int Application::GetFrameRate(void)
{
return this->m_FPS;
}
RECT Application::getRect(void)
{
return this->m_rRect;
}
RECT Application::getWindowRect(void)
{
return this->m_rWindowRect;
}
void Application::ResetMouseRelease(void)
{
this->Mouse.LeftButton.SetRelease(false);
this->Mouse.RightButton.SetRelease(false);
this->Mouse.MiddleButton.SetRelease(false);
}
unsigned int Application::getRefreshRate(void)
{
return this->mRefreshRate;
}
unsigned int Application::getColorDepth(void)
{
return this->mColorBits;
}
unsigned int Application::getDepthBits(void)
{
return this->mDepthBits;
}
unsigned int Application::getStencilBits(void)
{
return this->mStencilBits;
}
unsigned int Application::getAlphaBits(void)
{
return this->mAlphaBits;
}
bool Application::keyReleased_wParam(unsigned int key, WPARAM wParam)
{
if(wParam == key)
{
return true;
}
return false;
}
bool Application::keyReleased(unsigned int key)
{
if(this->mKeyWParam == key)
{
this->mKeyWParam = 0;
return true;
}
return false;
}
bool Application::keyPressed(unsigned int key)
{
if(GetKeyState(key) & 0x80)
{
return true;
}
/*
if(this->mKeyWParam == key)
{
return true;
}*/
return false;
}
void Application::CenterMouse(void)
{
this->Mouse.CenterScreen(this->windowPosition, this->getDimensions() , this->systemResolution);
}
DIM Application::getDimensions()
{
DIM myDim;
myDim.x = width;
myDim.y = height;
return myDim;
}
DIM Application::getSystemResolution(void)
{
return this->systemResolution;
}
DIM Application::getCenter(void)
{
DIM ret;
ret = this->Mouse.getCenter(this->windowPosition, this->getDimensions() , this->systemResolution);
this->centerScreen = ret;
return ret;
}
DIM Application::getLastCenter(void)
{
return this->centerScreen;
}
RX_API void Application::ToggleFullScreen(void)
{
PostMessage(this->m_hWnd, (WM_USER+1), 0, 0);
//this->DestroyWindowObject();
}
HWND Application::createWindow(void)
{
HWND rhWnd = CreateWindow( CLASSNAME,
this->mWindowName,
this->dwStyle,
0,
0,
this->getDimensions().x,
this->getDimensions().y,
NULL,
NULL,
this->m_hInstance,
NULL);
return rhWnd;
}
LPCTSTR Application::getWindowName(void)
{
return this->mWindowName;
}
bool Application::isFullScreen(void)
{
bool ret;
if(this->m_bFullScreen <= 0)
{
ret = false;
} else {
ret = true;
}
return ret;
}
char Application::getAlphaNumeric(void)
{
int i;
for( i = 65; i <= 90; i++)
{
if(this->keyReleased(i))
{
return i;
}
}
for(i = 48; i <= 57; i++)
{
if(this->keyReleased(i))
{
return i;
}
}
if(this->keyReleased(32))
{
return ' ';
}
if(this->keyReleased(47))
{
return '/';
}
return 0;
}
void Application::InitializeFromPixelFormatForMultiSample(const int pf)
{
PIXELFORMATDESCRIPTOR pfd = {0};
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = this->mColorBits;
pfd.cDepthBits = this->mDepthBits;
this->m_hDC = GetDC(this->m_hWnd);
SetPixelFormat(this->m_hDC, pf, &pfd);
this->m_hRC = wglCreateContext(this->m_hDC);
wglMakeCurrent(this->m_hDC, this->m_hRC);
}
HGLRC Application::getGLRenderContext(void)
{
return this->m_hRC;
}
void Application::DestroyWindowObject(void)
{
if(this->m_hWnd)
{
if (this->m_hDC)
{
wglMakeCurrent(m_hDC, NULL);
if (m_hRC)
{
wglDeleteContext(m_hRC);
}
ReleaseDC(m_hWnd, m_hDC);
m_hDC = 0;
}
DestroyWindow(m_hWnd);
m_hWnd = 0;
}
if(m_bFullScreen)
{
ChangeDisplaySettings(NULL, 0);
Mouse.Show();
}
}
void Application::UpdateKeyReleased(void)
{
this->characterReleased = 0;
}
unsigned int Application::getLastKeyReleased(void)
{
return this->characterReleased;
}
PIXELFORMATDESCRIPTOR Application::getPixelFormatDescriptor(void)
{
return this->mPFD;
}
bool Application::WGLisExtensionSupported(const char *extension)
{
const size_t extlen = strlen(extension);
const char *supported = NULL;
// Try To Use wglGetExtensionStringARB On Current DC, If Possible
PROC wglGetExtString = wglGetProcAddress("wglGetExtensionsStringARB");
if (wglGetExtString)
supported = ((char*(__stdcall*)(HDC))wglGetExtString)(wglGetCurrentDC());
// If That Failed, Try Standard Opengl Extensions String
//if (supported == NULL)
// supported = (char*)glGetString(GL_EXTENSIONS);
// If That Failed Too, Must Be No Extensions Supported
if (supported == NULL)
return false;
// Begin Examination At Start Of String, Increment By 1 On False Match
for (const char* p = supported; ; p++)
{
// Advance p Up To The Next Possible Match
p = strstr(p, extension);
if (p == NULL)
return false; // No Match
// Make Sure That Match Is At The Start Of The String Or That
// The Previous Char Is A Space, Or Else We Could Accidentally
// Match "wglFunkywglExtension" With "wglExtension"
// Also, Make Sure That The Following Character Is Space Or NULL
// Or Else "wglExtensionTwo" Might Match "wglExtension"
if ((p==supported || p[-1]==' ') && (p[extlen]=='\0' || p[extlen]==' '))
return true; // Match
}
}
}