forked from nvpro-samples/nvpro_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_win32.cpp
1262 lines (1111 loc) · 38.7 KB
/
main_win32.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
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------------
Copyright (c) 2013, NVIDIA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Neither the name of its contributors may be used to endorse
or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
feedback to [email protected] (Tristan Lorach)
*/ //--------------------------------------------------------------------
#include <GL/glew.h>
#include <GL/wglew.h>
#include "main.h"
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <windows.h>
#include <windowsx.h>
#include "resources.h"
extern "C" { _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; }
HINSTANCE g_hInstance = 0;
LPSTR g_lpCmdLine;
int g_nCmdShow;
std::vector<NVPWindow *> g_windows;
MSG uMsg;
struct WINinternal
{
NVPWindow * m_win;
HDC m_hDC;
HGLRC m_hRC;
HWND m_hWnd;
HWND m_hWndDummy;
bool m_iconified;
bool m_visible;
WINinternal (NVPWindow *win)
: m_win(win)
, m_hDC(NULL)
, m_hRC(NULL)
, m_hWnd(NULL)
, m_hWndDummy(NULL)
, m_iconified(false)
, m_visible(true)
{
}
bool create(const char* title, int width, int height);
bool initBase(const NVPWindow::ContextFlags * cflags, NVPWindow* sourcewindow);
};
//------------------------------------------------------------------------------
// Toggles
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#ifdef _DEBUG
static void APIENTRY myOpenGLCallback( GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const GLvoid* userParam)
{
NVPWindow* window = (NVPWindow*)userParam;
GLenum filter = window->m_debugFilter;
GLenum severitycmp = severity;
// minor fixup for filtering so notification becomes lowest priority
if (GL_DEBUG_SEVERITY_NOTIFICATION == filter){
filter = GL_DEBUG_SEVERITY_LOW_ARB+1;
}
if (GL_DEBUG_SEVERITY_NOTIFICATION == severitycmp){
severitycmp = GL_DEBUG_SEVERITY_LOW_ARB+1;
}
if (!filter|| severitycmp <= filter )
{
//static std::map<GLuint, bool> ignoreMap;
//if(ignoreMap[id] == true)
// return;
char *strSource = "0";
char *strType = strSource;
switch(source)
{
case GL_DEBUG_SOURCE_API_ARB:
strSource = "API";
break;
case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
strSource = "WINDOWS";
break;
case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
strSource = "SHADER COMP.";
break;
case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
strSource = "3RD PARTY";
break;
case GL_DEBUG_SOURCE_APPLICATION_ARB:
strSource = "APP";
break;
case GL_DEBUG_SOURCE_OTHER_ARB:
strSource = "OTHER";
break;
}
switch(type)
{
case GL_DEBUG_TYPE_ERROR_ARB:
strType = "ERROR";
break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
strType = "Deprecated";
break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
strType = "Undefined";
break;
case GL_DEBUG_TYPE_PORTABILITY_ARB:
strType = "Portability";
break;
case GL_DEBUG_TYPE_PERFORMANCE_ARB:
strType = "Performance";
break;
case GL_DEBUG_TYPE_OTHER_ARB:
strType = "Other";
break;
}
switch(severity)
{
case GL_DEBUG_SEVERITY_HIGH_ARB:
LOGE("ARB_debug : %s High - %s - %s : %s\n", window->m_debugTitle.c_str(), strSource, strType, message);
break;
case GL_DEBUG_SEVERITY_MEDIUM_ARB:
LOGW("ARB_debug : %s Medium - %s - %s : %s\n", window->m_debugTitle.c_str(), strSource, strType, message);
break;
case GL_DEBUG_SEVERITY_LOW_ARB:
LOGI("ARB_debug : %s Low - %s - %s : %s\n", window->m_debugTitle.c_str(), strSource, strType, message);
break;
default:
//LOGI("ARB_debug : comment - %s - %s : %s\n", strSource, strType, message);
break;
}
}
}
//------------------------------------------------------------------------------
void checkGL( char* msg )
{
GLenum errCode;
//const GLubyte* errString;
errCode = glGetError();
if (errCode != GL_NO_ERROR) {
//printf ( "%s, ERROR: %s\n", msg, gluErrorString(errCode) );
LOGE("%s, ERROR: 0x%x\n", msg, errCode );
}
}
#endif
//------------------------------------------------------------------------------
bool WINinternal::initBase(const NVPWindow::ContextFlags* cflags, NVPWindow* sourcewindow)
{
GLuint PixelFormat;
NVPWindow::ContextFlags settings;
if (cflags){
settings = *cflags;
}
PIXELFORMATDESCRIPTOR pfd;
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 = 32;
pfd.cDepthBits = settings.depth;
pfd.cStencilBits = settings.stencil;
if( settings.stereo )
{
pfd.dwFlags |= PFD_STEREO;
}
if(settings.MSAA > 1)
{
m_hDC = GetDC(m_hWndDummy);
PixelFormat = ChoosePixelFormat( m_hDC, &pfd );
SetPixelFormat( m_hDC, PixelFormat, &pfd);
m_hRC = wglCreateContext( m_hDC );
wglMakeCurrent( m_hDC, m_hRC );
glewInit();
ReleaseDC(m_hWndDummy, m_hDC);
m_hDC = GetDC( m_hWnd );
int attri[] = {
WGL_DRAW_TO_WINDOW_ARB, true,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_SUPPORT_OPENGL_ARB, true,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_DOUBLE_BUFFER_ARB, true,
WGL_DEPTH_BITS_ARB, settings.depth,
WGL_STENCIL_BITS_ARB, settings.stencil,
WGL_SAMPLE_BUFFERS_ARB, 1,
WGL_SAMPLES_ARB,
settings.MSAA,
0,0
};
GLuint nfmts;
int fmt;
if(!wglChoosePixelFormatARB( m_hDC, attri, NULL, 1, &fmt, &nfmts )){
wglDeleteContext(m_hRC);
return false;
}
wglDeleteContext(m_hRC);
DestroyWindow(m_hWndDummy);
m_hWndDummy = NULL;
if(!SetPixelFormat( m_hDC, fmt, &pfd))
return false;
} else {
m_hDC = GetDC( m_hWnd );
PixelFormat = ChoosePixelFormat( m_hDC, &pfd );
SetPixelFormat( m_hDC, PixelFormat, &pfd);
}
m_hRC = wglCreateContext( m_hDC );
wglMakeCurrent( m_hDC, m_hRC );
// calling glewinit NOW because the inside glew, there is mistake to fix...
// This is the joy of using Core. The query glGetString(GL_EXTENSIONS) is deprecated from the Core profile.
// You need to use glGetStringi(GL_EXTENSIONS, <index>) instead. Sounds like a "bug" in GLEW.
glewInit();
#define GLCOMPAT
if(!wglCreateContextAttribsARB)
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
if(wglCreateContextAttribsARB)
{
HGLRC hRC = NULL;
std::vector<int> attribList;
#define ADDATTRIB(a,b) { attribList.push_back(a); attribList.push_back(b); }
int maj= settings.major;
int min= settings.minor;
ADDATTRIB(WGL_CONTEXT_MAJOR_VERSION_ARB, maj)
ADDATTRIB(WGL_CONTEXT_MINOR_VERSION_ARB, min)
if(settings.core)
ADDATTRIB(WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB)
else
ADDATTRIB(WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB)
int ctxtflags = 0;
if(settings.debug)
ctxtflags |= WGL_CONTEXT_DEBUG_BIT_ARB;
if(settings.robust)
ctxtflags |= WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB;
if(settings.forward) // use it if you want errors when compat options still used
ctxtflags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
ADDATTRIB(WGL_CONTEXT_FLAGS_ARB, ctxtflags);
ADDATTRIB(0, 0)
int *p = &(attribList[0]);
if (!(hRC = wglCreateContextAttribsARB(m_hDC, 0, p )))
{
LOGE("wglCreateContextAttribsARB() failed for OpenGL context.\n");
return false;
}
if (!wglMakeCurrent(m_hDC, hRC)) {
LOGE("wglMakeCurrent() failed for OpenGL context.\n");
} else {
wglDeleteContext( m_hRC );
m_hRC = hRC;
#ifdef _DEBUG
if(!__glewDebugMessageCallbackARB)
{
__glewDebugMessageCallbackARB = (PFNGLDEBUGMESSAGECALLBACKARBPROC)wglGetProcAddress("glDebugMessageCallbackARB");
__glewDebugMessageControlARB = (PFNGLDEBUGMESSAGECONTROLARBPROC) wglGetProcAddress("glDebugMessageControlARB");
}
if(__glewDebugMessageCallbackARB)
{
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
glDebugMessageCallbackARB(myOpenGLCallback, sourcewindow);
}
#endif
}
}
glewInit();
LOGOK("Loaded Glew\n");
LOGOK("initialized OpenGL basis\n");
return true;
}
static int getKeyMods()
{
int mods = 0;
if (GetKeyState(VK_SHIFT) & (1 << 31))
mods |= NVPWindow::KMOD_SHIFT;
if (GetKeyState(VK_CONTROL) & (1 << 31))
mods |= NVPWindow::KMOD_CONTROL;
if (GetKeyState(VK_MENU) & (1 << 31))
mods |= NVPWindow::KMOD_ALT;
if ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & (1 << 31))
mods |= NVPWindow::KMOD_SUPER;
return mods;
}
// Translates a Windows key to the corresponding GLFW key
//
#define INTERNAL_KEY_INVALID -2
static int translateKey(WPARAM wParam, LPARAM lParam)
{
// Check for numeric keypad keys
// NOTE: This way we always force "NumLock = ON", which is intentional since
// the returned key code should correspond to a physical location.
if ((HIWORD(lParam) & 0x100) == 0)
{
switch (MapVirtualKey(HIWORD(lParam) & 0xFF, 1))
{
case VK_INSERT: return NVPWindow::KEY_KP_0;
case VK_END: return NVPWindow::KEY_KP_1;
case VK_DOWN: return NVPWindow::KEY_KP_2;
case VK_NEXT: return NVPWindow::KEY_KP_3;
case VK_LEFT: return NVPWindow::KEY_KP_4;
case VK_CLEAR: return NVPWindow::KEY_KP_5;
case VK_RIGHT: return NVPWindow::KEY_KP_6;
case VK_HOME: return NVPWindow::KEY_KP_7;
case VK_UP: return NVPWindow::KEY_KP_8;
case VK_PRIOR: return NVPWindow::KEY_KP_9;
case VK_DIVIDE: return NVPWindow::KEY_KP_DIVIDE;
case VK_MULTIPLY: return NVPWindow::KEY_KP_MULTIPLY;
case VK_SUBTRACT: return NVPWindow::KEY_KP_SUBTRACT;
case VK_ADD: return NVPWindow::KEY_KP_ADD;
case VK_DELETE: return NVPWindow::KEY_KP_DECIMAL;
default: break;
}
}
// Check which key was pressed or released
switch (wParam)
{
// The SHIFT keys require special handling
case VK_SHIFT:
{
// Compare scan code for this key with that of VK_RSHIFT in
// order to determine which shift key was pressed (left or
// right)
const DWORD scancode = MapVirtualKey(VK_RSHIFT, 0);
if ((DWORD) ((lParam & 0x01ff0000) >> 16) == scancode)
return NVPWindow::KEY_RIGHT_SHIFT;
return NVPWindow::KEY_LEFT_SHIFT;
}
// The CTRL keys require special handling
case VK_CONTROL:
{
MSG next;
DWORD time;
// Is this an extended key (i.e. right key)?
if (lParam & 0x01000000)
return NVPWindow::KEY_RIGHT_CONTROL;
// Here is a trick: "Alt Gr" sends LCTRL, then RALT. We only
// want the RALT message, so we try to see if the next message
// is a RALT message. In that case, this is a false LCTRL!
time = GetMessageTime();
if (PeekMessage(&next, NULL, 0, 0, PM_NOREMOVE))
{
if (next.message == WM_KEYDOWN ||
next.message == WM_SYSKEYDOWN ||
next.message == WM_KEYUP ||
next.message == WM_SYSKEYUP)
{
if (next.wParam == VK_MENU &&
(next.lParam & 0x01000000) &&
next.time == time)
{
// Next message is a RALT down message, which
// means that this is not a proper LCTRL message
return INTERNAL_KEY_INVALID;
}
}
}
return NVPWindow::KEY_LEFT_CONTROL;
}
// The ALT keys require special handling
case VK_MENU:
{
// Is this an extended key (i.e. right key)?
if (lParam & 0x01000000)
return NVPWindow::KEY_RIGHT_ALT;
return NVPWindow::KEY_LEFT_ALT;
}
// The ENTER keys require special handling
case VK_RETURN:
{
// Is this an extended key (i.e. right key)?
if (lParam & 0x01000000)
return NVPWindow::KEY_KP_ENTER;
return NVPWindow::KEY_ENTER;
}
// Funcion keys (non-printable keys)
case VK_ESCAPE: return NVPWindow::KEY_ESCAPE;
case VK_TAB: return NVPWindow::KEY_TAB;
case VK_BACK: return NVPWindow::KEY_BACKSPACE;
case VK_HOME: return NVPWindow::KEY_HOME;
case VK_END: return NVPWindow::KEY_END;
case VK_PRIOR: return NVPWindow::KEY_PAGE_UP;
case VK_NEXT: return NVPWindow::KEY_PAGE_DOWN;
case VK_INSERT: return NVPWindow::KEY_INSERT;
case VK_DELETE: return NVPWindow::KEY_DELETE;
case VK_LEFT: return NVPWindow::KEY_LEFT;
case VK_UP: return NVPWindow::KEY_UP;
case VK_RIGHT: return NVPWindow::KEY_RIGHT;
case VK_DOWN: return NVPWindow::KEY_DOWN;
case VK_F1: return NVPWindow::KEY_F1;
case VK_F2: return NVPWindow::KEY_F2;
case VK_F3: return NVPWindow::KEY_F3;
case VK_F4: return NVPWindow::KEY_F4;
case VK_F5: return NVPWindow::KEY_F5;
case VK_F6: return NVPWindow::KEY_F6;
case VK_F7: return NVPWindow::KEY_F7;
case VK_F8: return NVPWindow::KEY_F8;
case VK_F9: return NVPWindow::KEY_F9;
case VK_F10: return NVPWindow::KEY_F10;
case VK_F11: return NVPWindow::KEY_F11;
case VK_F12: return NVPWindow::KEY_F12;
case VK_F13: return NVPWindow::KEY_F13;
case VK_F14: return NVPWindow::KEY_F14;
case VK_F15: return NVPWindow::KEY_F15;
case VK_F16: return NVPWindow::KEY_F16;
case VK_F17: return NVPWindow::KEY_F17;
case VK_F18: return NVPWindow::KEY_F18;
case VK_F19: return NVPWindow::KEY_F19;
case VK_F20: return NVPWindow::KEY_F20;
case VK_F21: return NVPWindow::KEY_F21;
case VK_F22: return NVPWindow::KEY_F22;
case VK_F23: return NVPWindow::KEY_F23;
case VK_F24: return NVPWindow::KEY_F24;
case VK_NUMLOCK: return NVPWindow::KEY_NUM_LOCK;
case VK_CAPITAL: return NVPWindow::KEY_CAPS_LOCK;
case VK_SNAPSHOT: return NVPWindow::KEY_PRINT_SCREEN;
case VK_SCROLL: return NVPWindow::KEY_SCROLL_LOCK;
case VK_PAUSE: return NVPWindow::KEY_PAUSE;
case VK_LWIN: return NVPWindow::KEY_LEFT_SUPER;
case VK_RWIN: return NVPWindow::KEY_RIGHT_SUPER;
case VK_APPS: return NVPWindow::KEY_MENU;
// Numeric keypad
case VK_NUMPAD0: return NVPWindow::KEY_KP_0;
case VK_NUMPAD1: return NVPWindow::KEY_KP_1;
case VK_NUMPAD2: return NVPWindow::KEY_KP_2;
case VK_NUMPAD3: return NVPWindow::KEY_KP_3;
case VK_NUMPAD4: return NVPWindow::KEY_KP_4;
case VK_NUMPAD5: return NVPWindow::KEY_KP_5;
case VK_NUMPAD6: return NVPWindow::KEY_KP_6;
case VK_NUMPAD7: return NVPWindow::KEY_KP_7;
case VK_NUMPAD8: return NVPWindow::KEY_KP_8;
case VK_NUMPAD9: return NVPWindow::KEY_KP_9;
case VK_DIVIDE: return NVPWindow::KEY_KP_DIVIDE;
case VK_MULTIPLY: return NVPWindow::KEY_KP_MULTIPLY;
case VK_SUBTRACT: return NVPWindow::KEY_KP_SUBTRACT;
case VK_ADD: return NVPWindow::KEY_KP_ADD;
case VK_DECIMAL: return NVPWindow::KEY_KP_DECIMAL;
// Printable keys are mapped according to US layout
case VK_SPACE: return NVPWindow::KEY_SPACE;
case 0x30: return NVPWindow::KEY_0;
case 0x31: return NVPWindow::KEY_1;
case 0x32: return NVPWindow::KEY_2;
case 0x33: return NVPWindow::KEY_3;
case 0x34: return NVPWindow::KEY_4;
case 0x35: return NVPWindow::KEY_5;
case 0x36: return NVPWindow::KEY_6;
case 0x37: return NVPWindow::KEY_7;
case 0x38: return NVPWindow::KEY_8;
case 0x39: return NVPWindow::KEY_9;
case 0x41: return NVPWindow::KEY_A;
case 0x42: return NVPWindow::KEY_B;
case 0x43: return NVPWindow::KEY_C;
case 0x44: return NVPWindow::KEY_D;
case 0x45: return NVPWindow::KEY_E;
case 0x46: return NVPWindow::KEY_F;
case 0x47: return NVPWindow::KEY_G;
case 0x48: return NVPWindow::KEY_H;
case 0x49: return NVPWindow::KEY_I;
case 0x4A: return NVPWindow::KEY_J;
case 0x4B: return NVPWindow::KEY_K;
case 0x4C: return NVPWindow::KEY_L;
case 0x4D: return NVPWindow::KEY_M;
case 0x4E: return NVPWindow::KEY_N;
case 0x4F: return NVPWindow::KEY_O;
case 0x50: return NVPWindow::KEY_P;
case 0x51: return NVPWindow::KEY_Q;
case 0x52: return NVPWindow::KEY_R;
case 0x53: return NVPWindow::KEY_S;
case 0x54: return NVPWindow::KEY_T;
case 0x55: return NVPWindow::KEY_U;
case 0x56: return NVPWindow::KEY_V;
case 0x57: return NVPWindow::KEY_W;
case 0x58: return NVPWindow::KEY_X;
case 0x59: return NVPWindow::KEY_Y;
case 0x5A: return NVPWindow::KEY_Z;
case 0xBD: return NVPWindow::KEY_MINUS;
case 0xBB: return NVPWindow::KEY_EQUAL;
case 0xDB: return NVPWindow::KEY_LEFT_BRACKET;
case 0xDD: return NVPWindow::KEY_RIGHT_BRACKET;
case 0xDC: return NVPWindow::KEY_BACKSLASH;
case 0xBA: return NVPWindow::KEY_SEMICOLON;
case 0xDE: return NVPWindow::KEY_APOSTROPHE;
case 0xC0: return NVPWindow::KEY_GRAVE_ACCENT;
case 0xBC: return NVPWindow::KEY_COMMA;
case 0xBE: return NVPWindow::KEY_PERIOD;
case 0xBF: return NVPWindow::KEY_SLASH;
case 0xDF: return NVPWindow::KEY_WORLD_1;
case 0xE2: return NVPWindow::KEY_WORLD_2;
default: break;
}
// No matching translation was found
return NVPWindow::KEY_UNKNOWN;
}
//------------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND m_hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
bool bRes = false;
// get back the correct window
int index = (int)GetWindowLongPtr(m_hWnd, GWLP_USERDATA);
NVPWindow *pWin = NULL;
if(g_windows.size() > 0)
pWin = g_windows[index];
//
// Pass the messages to our UI, first
//
if(!bRes) switch( msg )
{
case WM_ACTIVATE:
if(pWin->m_internal)
pWin->m_internal->m_iconified = HIWORD(wParam) ? true : false;
break;
case WM_SHOWWINDOW:
if(pWin->m_internal)
pWin->m_internal->m_visible = wParam ? true : false;
break;
case WM_PAINT:
pWin->postRedisplay();
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
{
pWin->postRedisplay();
const int scancode = (lParam >> 16) & 0xff;
const int key = translateKey(wParam, lParam);
if (key == INTERNAL_KEY_INVALID)
break;
pWin->setMods(getKeyMods());
pWin->keyboard( (NVPWindow::KeyCode)key,NVPWindow::BUTTON_PRESS, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
}
case WM_KEYUP:
case WM_SYSKEYUP:
{
pWin->postRedisplay();
const int scancode = (lParam >> 16) & 0xff;
const int key = translateKey(wParam, lParam);
if (key == INTERNAL_KEY_INVALID)
break;
pWin->setMods(getKeyMods());
pWin->keyboard( (NVPWindow::KeyCode)key,NVPWindow::BUTTON_RELEASE, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
}
case WM_CHAR:
case WM_SYSCHAR:
{
unsigned int key = (unsigned int)wParam;
if (key < 32 || (key > 126 && key < 160))
break;
pWin->keyboardchar(key, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
}
break;
case WM_MOUSEWHEEL:
pWin->mousewheel((short)HIWORD(wParam));
break;
case WM_LBUTTONDBLCLK:
pWin->setCurMouse(GET_X_LPARAM(lParam), GET_Y_LPARAM (lParam));
pWin->mouse(NVPWindow::MOUSE_BUTTON_LEFT, NVPWindow::BUTTON_REPEAT, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
case WM_LBUTTONDOWN:
pWin->setCurMouse( GET_X_LPARAM(lParam), GET_Y_LPARAM (lParam));
pWin->mouse(NVPWindow::MOUSE_BUTTON_LEFT, NVPWindow::BUTTON_PRESS, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
case WM_LBUTTONUP:
pWin->setCurMouse( GET_X_LPARAM(lParam), GET_Y_LPARAM (lParam));
pWin->mouse(NVPWindow::MOUSE_BUTTON_LEFT, NVPWindow::BUTTON_RELEASE, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
case WM_RBUTTONDOWN:
pWin->setCurMouse( GET_X_LPARAM(lParam), GET_Y_LPARAM (lParam));
pWin->mouse(NVPWindow::MOUSE_BUTTON_RIGHT, NVPWindow::BUTTON_PRESS, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
case WM_RBUTTONUP:
pWin->setCurMouse( GET_X_LPARAM(lParam), GET_Y_LPARAM (lParam));
pWin->mouse(NVPWindow::MOUSE_BUTTON_RIGHT, NVPWindow::BUTTON_RELEASE, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
case WM_MBUTTONDOWN:
pWin->setCurMouse( GET_X_LPARAM(lParam), GET_Y_LPARAM (lParam));
pWin->mouse(NVPWindow::MOUSE_BUTTON_MIDDLE, NVPWindow::BUTTON_PRESS, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
case WM_MBUTTONUP:
pWin->setCurMouse( GET_X_LPARAM(lParam), GET_Y_LPARAM (lParam));
pWin->mouse(NVPWindow::MOUSE_BUTTON_MIDDLE, NVPWindow::BUTTON_RELEASE, pWin->getMods(), pWin->getCurX(), pWin->getCurY());
break;
case WM_MOUSEMOVE:
pWin->setCurMouse( GET_X_LPARAM(lParam), GET_Y_LPARAM (lParam));
pWin->motion(pWin->getCurX(), pWin->getCurY());
break;
case WM_SIZE:
pWin->setWinSz(LOWORD(lParam), HIWORD(lParam));
pWin->reshape(LOWORD(lParam), HIWORD(lParam));
break;
case WM_CLOSE:
pWin->shutdown();
PostQuitMessage(0);
break;
case WM_DESTROY:
pWin->shutdown();
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( m_hWnd, msg, wParam, lParam );
}
//------------------------------------------------------------------------------
bool WINinternal::create(const char* title, int width, int height)
{
WNDCLASSEX winClass;
winClass.lpszClassName = "MY_WINDOWS_CLASS";
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = WindowProc;
winClass.hInstance = g_hInstance;
winClass.hIcon = LoadIcon(g_hInstance, (LPCTSTR)IDI_OPENGL_ICON);
winClass.hIconSm = LoadIcon(g_hInstance, (LPCTSTR)IDI_OPENGL_ICON);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
if(!RegisterClassEx(&winClass) )
return false;
DWORD style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX;
DWORD styleEx = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
RECT rect = { 0, 0, width, height };
AdjustWindowRectEx(&rect, style,
FALSE, styleEx);
m_hWnd = CreateWindowEx( styleEx, "MY_WINDOWS_CLASS",
title ? title : "Viewer",
style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL,
g_hInstance, (LPVOID)NULL );
winClass.lpszClassName = "DUMMY";
winClass.lpfnWndProc = DefWindowProc;
if(!RegisterClassEx(&winClass) )
return false;
m_hWndDummy = CreateWindowEx( NULL, "DUMMY",
"Dummy",
WS_OVERLAPPEDWINDOW, 0, 0, 10, 10, NULL, NULL,
g_hInstance, NULL );
if( m_hWnd == NULL )
return false;
return true;
}
bool NVPWindow::activate(int width, int height, const char* title, const ContextFlags* cflags, int invisible)
{
// FIXME invisibile handling!
return create(title,cflags,width,height);
}
void NVPWindow::deactivate()
{
// FIXME should remove from g_windows
}
bool NVPWindow::create(const char* title, const ContextFlags* cflags, int width, int height)
{
m_winSz[0] = width;
m_winSz[1] = height;
m_internal = new WINinternal(this);
m_debugTitle = title ? title:"Sample";
if (m_internal->create(m_debugTitle.c_str(), width,height))
{
// Keep track of the windows
g_windows.push_back(this);
SetWindowLongPtr(m_internal->m_hWnd, GWLP_USERDATA, g_windows.size()-1 );
UpdateWindow( m_internal->m_hWnd );
// Initialize the very base of OpenGL
if(m_internal->initBase(cflags, this))
if(init()){
// showwindow will trigger resize/paint events, that must not be called prior
// sample init
ShowWindow( m_internal->m_hWnd, g_nCmdShow );
return true;
}
}
delete m_internal;
m_internal = NULL;
return false;
}
//---------------------------------------------------------------------------
// Post a QUIT
void NVPWindow::postQuit()
{
PostQuitMessage(0);
}
void NVPWindow::swapBuffers()
{
SwapBuffers( m_internal->m_hDC );
}
void NVPWindow::setTitle( const char* title )
{
SetWindowTextA(m_internal->m_hWnd, title);
}
void NVPWindow::maximize()
{
ShowWindow(m_internal->m_hWnd, SW_MAXIMIZE);
}
void NVPWindow::restore()
{
ShowWindow(m_internal->m_hWnd, SW_RESTORE);
}
void NVPWindow::minimize()
{
ShowWindow(m_internal->m_hWnd, SW_MINIMIZE);
}
bool NVPWindow::isOpen()
{
return m_internal->m_visible && !m_internal->m_iconified;
}
void NVPWindow::makeContextCurrent()
{
wglMakeCurrent(m_internal->m_hDC,m_internal->m_hRC);
}
void NVPWindow::makeContextNonCurrent()
{
wglMakeCurrent(0,0);
}
void NVPWindow::swapInterval(int i)
{
wglSwapIntervalEXT(i);
}
//---------------------------------------------------------------------------
// Message pump
bool NVPWindow::sysPollEvents(bool bLoop)
{
bool bContinue;
do {
if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &uMsg );
DispatchMessage( &uMsg );
}
else
{
for(int i=0; i<g_windows.size(); i++)
{
NVPWindow *pWin = g_windows[i];
if(pWin->m_renderCnt > 0)
{
pWin->m_renderCnt--;
pWin->display();
}
}
}
bContinue = uMsg.message != WM_QUIT;
} while( bContinue && bLoop );
return bContinue;
}
// from GLFW 3.0
static int stringInExtensionString(const char* string, const char* exts)
{
const GLubyte* extensions = (const GLubyte*) exts;
const GLubyte* start;
GLubyte* where;
GLubyte* terminator;
// It takes a bit of care to be fool-proof about parsing the
// OpenGL extensions string. Don't be fooled by sub-strings,
// etc.
start = extensions;
for (;;)
{
where = (GLubyte*) strstr((const char*) start, string);
if (!where)
return GL_FALSE;
terminator = where + strlen(string);
if (where == start || *(where - 1) == ' ')
{
if (*terminator == ' ' || *terminator == '\0')
break;
}
start = terminator;
}
return GL_TRUE;
}
int NVPWindow::sysExtensionSupported( const char* name )
{
// we are not using the glew query, as glew will only report
// those extension it knows about, not what the actual driver may support
int i;
GLint count;
// Check if extension is in the modern OpenGL extensions string list
// This should be safe to use since GL 3.0 is around for a long time :)
glGetIntegerv(GL_NUM_EXTENSIONS, &count);
for (i = 0; i < count; i++)
{
const char* en = (const char*) glGetStringi(GL_EXTENSIONS, i);
if (!en)
{
return GL_FALSE;
}
if (strcmp(en, name) == 0)
return GL_TRUE;
}
// Check platform specifc gets
const char* exts = NULL;
NVPWindow* win = g_windows[0];
if (WGLEW_ARB_extensions_string){
exts = wglGetExtensionsStringARB(win->m_internal->m_hDC);
}
if (!exts && WGLEW_EXT_extensions_string){
exts = wglGetExtensionsStringEXT();
}
if (!exts) {
return FALSE;
}
return stringInExtensionString(name,exts);
}
NVPWindow::NVPproc NVPWindow::sysGetProcAddress( const char* name )
{
return (NVPWindow::NVPproc)wglGetProcAddress(name);
}
void NVPWindow::sysWaitEvents()
{
WaitMessage();
sysPollEvents(false);
}
static double s_frequency;
double NVPWindow::sysGetTime()
{
LARGE_INTEGER time;
if (QueryPerformanceCounter(&time)){
return (double(time.QuadPart) / s_frequency);
}
return 0;
}
void NVPWindow::sysSleep(double seconds)
{
Sleep(DWORD(seconds * 1000.0));
}
void NVPWindow::sysInit()
{
}
void NVPWindow::sysDeinit()
{
}
static std::string s_path;
std::string NVPWindow::sysExePath()
{
return s_path;
}
static const WORD MAX_CONSOLE_LINES = 500;
using namespace std;
void NVPWindow::sysVisibleConsole()
{
int hConHandle;
long lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
// allocate a console for this app
AllocConsole();
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
&coninfo);
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
coninfo.dwSize);