-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDXUT (original).cpp
More file actions
5166 lines (4516 loc) · 227 KB
/
Copy pathDXUT (original).cpp
File metadata and controls
5166 lines (4516 loc) · 227 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
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
//--------------------------------------------------------------------------------------
// File: DXUT.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "dxstdafx.h"
#define DXUT_MIN_WINDOW_SIZE_X 200
#define DXUT_MIN_WINDOW_SIZE_Y 200
#undef min // use __min instead inside this source file
#undef max // use __max instead inside this source file
//--------------------------------------------------------------------------------------
// Thread safety
//--------------------------------------------------------------------------------------
CRITICAL_SECTION g_cs;
bool g_bThreadSafe = true;
//--------------------------------------------------------------------------------------
// Automatically enters & leaves the CS upon object creation/deletion
//--------------------------------------------------------------------------------------
class DXUTLock
{
public:
inline DXUTLock() { if( g_bThreadSafe ) EnterCriticalSection( &g_cs ); }
inline ~DXUTLock() { if( g_bThreadSafe ) LeaveCriticalSection( &g_cs ); }
};
//--------------------------------------------------------------------------------------
// Helper macros to build member functions that access member variables with thread safety
//--------------------------------------------------------------------------------------
#define SET_ACCESSOR( x, y ) inline void Set##y( x t ) { DXUTLock l; m_state.m_##y = t; };
#define GET_ACCESSOR( x, y ) inline x Get##y() { DXUTLock l; return m_state.m_##y; };
#define GET_SET_ACCESSOR( x, y ) SET_ACCESSOR( x, y ) GET_ACCESSOR( x, y )
#define SETP_ACCESSOR( x, y ) inline void Set##y( x* t ) { DXUTLock l; m_state.m_##y = *t; };
#define GETP_ACCESSOR( x, y ) inline x* Get##y() { DXUTLock l; return &m_state.m_##y; };
#define GETP_SETP_ACCESSOR( x, y ) SETP_ACCESSOR( x, y ) GETP_ACCESSOR( x, y )
//--------------------------------------------------------------------------------------
// Stores timer callback info
//--------------------------------------------------------------------------------------
struct DXUT_TIMER
{
LPDXUTCALLBACKTIMER pCallbackTimer;
void* pCallbackUserContext;
float fTimeoutInSecs;
float fCountdown;
bool bEnabled;
UINT nID;
};
//--------------------------------------------------------------------------------------
// Stores DXUT state and data access is done with thread safety (if g_bThreadSafe==true)
//--------------------------------------------------------------------------------------
class DXUTState
{
protected:
struct STATE
{
IDirect3D9* m_D3D; // the main D3D object
IDirect3DDevice9* m_D3DDevice; // the D3D rendering device
CD3DEnumeration* m_D3DEnumeration; // CD3DEnumeration object
DXUTDeviceSettings* m_CurrentDeviceSettings; // current device settings
D3DSURFACE_DESC m_BackBufferSurfaceDesc; // back buffer surface description
D3DCAPS9 m_Caps; // D3D caps for current device
HWND m_HWNDFocus; // the main app focus window
HWND m_HWNDDeviceFullScreen; // the main app device window in fullscreen mode
HWND m_HWNDDeviceWindowed; // the main app device window in windowed mode
HMONITOR m_AdapterMonitor; // the monitor of the adapter
HMENU m_Menu; // handle to menu
UINT m_FullScreenBackBufferWidthAtModeChange; // back buffer size of fullscreen mode right before switching to windowed mode. Used to restore to same resolution when toggling back to fullscreen
UINT m_FullScreenBackBufferHeightAtModeChange; // back buffer size of fullscreen mode right before switching to windowed mode. Used to restore to same resolution when toggling back to fullscreen
UINT m_WindowBackBufferWidthAtModeChange; // back buffer size of windowed mode right before switching to fullscreen mode. Used to restore to same resolution when toggling back to windowed mode
UINT m_WindowBackBufferHeightAtModeChange; // back buffer size of windowed mode right before switching to fullscreen mode. Used to restore to same resolution when toggling back to windowed mode
DWORD m_WindowedStyleAtModeChange; // window style
WINDOWPLACEMENT m_WindowedPlacement; // record of windowed HWND position/show state/etc
bool m_TopmostWhileWindowed; // if true, the windowed HWND is topmost
bool m_Minimized; // if true, the HWND is minimized
bool m_Maximized; // if true, the HWND is maximized
bool m_MinimizedWhileFullscreen; // if true, the HWND is minimized due to a focus switch away when fullscreen mode
bool m_IgnoreSizeChange; // if true, DXUT won't reset the device upon HWND size change
double m_Time; // current time in seconds
double m_AbsoluteTime; // absolute time in seconds
float m_ElapsedTime; // time elapsed since last frame
HINSTANCE m_HInstance; // handle to the app instance
double m_LastStatsUpdateTime; // last time the stats were updated
DWORD m_LastStatsUpdateFrames; // frames count since last time the stats were updated
float m_FPS; // frames per second
int m_CurrentFrameNumber; // the current frame number
HHOOK m_KeyboardHook; // handle to keyboard hook
bool m_AllowShortcutKeysWhenFullscreen; // if true, when fullscreen enable shortcut keys (Windows keys, StickyKeys shortcut, ToggleKeys shortcut, FilterKeys shortcut)
bool m_AllowShortcutKeysWhenWindowed; // if true, when windowed enable shortcut keys (Windows keys, StickyKeys shortcut, ToggleKeys shortcut, FilterKeys shortcut)
bool m_AllowShortcutKeys; // if true, then shortcut keys are currently disabled (Windows key, etc)
bool m_CallDefWindowProc; // if true, DXUTStaticWndProc will call DefWindowProc for unhandled messages. Applications rendering to a dialog may need to set this to false.
STICKYKEYS m_StartupStickyKeys; // StickyKey settings upon startup so they can be restored later
TOGGLEKEYS m_StartupToggleKeys; // ToggleKey settings upon startup so they can be restored later
FILTERKEYS m_StartupFilterKeys; // FilterKey settings upon startup so they can be restored later
bool m_HandleDefaultHotkeys; // if true, then DXUT will handle some default hotkeys
bool m_HandleAltEnter; // if true, then DXUT will handle Alt-Enter
bool m_ShowMsgBoxOnError; // if true, then msgboxes are displayed upon errors
bool m_NoStats; // if true, then DXUTGetFrameStats() and DXUTGetDeviceStats() will return blank strings
bool m_ClipCursorWhenFullScreen; // if true, then DXUT will keep the cursor from going outside the window when full screen
bool m_ShowCursorWhenFullScreen; // if true, then DXUT will show a cursor when full screen
bool m_ConstantFrameTime; // if true, then elapsed frame time will always be 0.05f seconds which is good for debugging or automated capture
float m_TimePerFrame; // the constant time per frame in seconds, only valid if m_ConstantFrameTime==true
bool m_WireframeMode; // if true, then D3DRS_FILLMODE==D3DFILL_WIREFRAME else D3DRS_FILLMODE==D3DFILL_SOLID
bool m_AutoChangeAdapter; // if true, then the adapter will automatically change if the window is different monitor
bool m_WindowCreatedWithDefaultPositions; // if true, then CW_USEDEFAULT was used and the window should be moved to the right adapter
int m_ExitCode; // the exit code to be returned to the command line
bool m_DXUTInited; // if true, then DXUTInit() has succeeded
bool m_WindowCreated; // if true, then DXUTCreateWindow() or DXUTSetWindow() has succeeded
bool m_DeviceCreated; // if true, then DXUTCreateDevice*() or DXUTSetDevice() has succeeded
bool m_DXUTInitCalled; // if true, then DXUTInit() was called
bool m_WindowCreateCalled; // if true, then DXUTCreateWindow() or DXUTSetWindow() was called
bool m_DeviceCreateCalled; // if true, then DXUTCreateDevice*() or DXUTSetDevice() was called
bool m_DeviceObjectsCreated; // if true, then DeviceCreated callback has been called (if non-NULL)
bool m_DeviceObjectsReset; // if true, then DeviceReset callback has been called (if non-NULL)
bool m_InsideDeviceCallback; // if true, then the framework is inside an app device callback
bool m_InsideMainloop; // if true, then the framework is inside the main loop
bool m_Active; // if true, then the app is the active top level window
bool m_TimePaused; // if true, then time is paused
bool m_RenderingPaused; // if true, then rendering is paused
int m_PauseRenderingCount; // pause rendering ref count
int m_PauseTimeCount; // pause time ref count
bool m_DeviceLost; // if true, then the device is lost and needs to be reset
bool m_NotifyOnMouseMove; // if true, include WM_MOUSEMOVE in mousecallback
bool m_Automation; // if true, automation is enabled
bool m_InSizeMove; // if true, app is inside a WM_ENTERSIZEMOVE
UINT m_TimerLastID; // last ID of the DXUT timer
int m_OverrideAdapterOrdinal; // if != -1, then override to use this adapter ordinal
bool m_OverrideWindowed; // if true, then force to start windowed
bool m_OverrideFullScreen; // if true, then force to start full screen
int m_OverrideStartX; // if != -1, then override to this X position of the window
int m_OverrideStartY; // if != -1, then override to this Y position of the window
int m_OverrideWidth; // if != 0, then override to this width
int m_OverrideHeight; // if != 0, then override to this height
bool m_OverrideForceHAL; // if true, then force to HAL device (failing if one doesn't exist)
bool m_OverrideForceREF; // if true, then force to REF device (failing if one doesn't exist)
bool m_OverrideForcePureHWVP; // if true, then force to use pure HWVP (failing if device doesn't support it)
bool m_OverrideForceHWVP; // if true, then force to use HWVP (failing if device doesn't support it)
bool m_OverrideForceSWVP; // if true, then force to use SWVP
bool m_OverrideConstantFrameTime; // if true, then force to constant frame time
float m_OverrideConstantTimePerFrame; // the constant time per frame in seconds if m_OverrideConstantFrameTime==true
int m_OverrideQuitAfterFrame; // if != 0, then it will force the app to quit after that frame
int m_OverrideForceVsync; // if == 0, then it will force the app to use D3DPRESENT_INTERVAL_IMMEDIATE, if == 1 force use of D3DPRESENT_INTERVAL_DEFAULT
bool m_OverrideRelaunchMCE; // if true, then force relaunch of MCE at exit
LPDXUTCALLBACKISDEVICEACCEPTABLE m_IsDeviceAcceptableFunc; // is device acceptable callback
LPDXUTCALLBACKMODIFYDEVICESETTINGS m_ModifyDeviceSettingsFunc; // modify device settings callback
LPDXUTCALLBACKDEVICECREATED m_DeviceCreatedFunc; // device created callback
LPDXUTCALLBACKDEVICERESET m_DeviceResetFunc; // device reset callback
LPDXUTCALLBACKDEVICELOST m_DeviceLostFunc; // device lost callback
LPDXUTCALLBACKDEVICEDESTROYED m_DeviceDestroyedFunc; // device destroyed callback
LPDXUTCALLBACKFRAMEMOVE m_FrameMoveFunc; // frame move callback
LPDXUTCALLBACKFRAMERENDER m_FrameRenderFunc; // frame render callback
LPDXUTCALLBACKKEYBOARD m_KeyboardFunc; // keyboard callback
LPDXUTCALLBACKMOUSE m_MouseFunc; // mouse callback
LPDXUTCALLBACKMSGPROC m_WindowMsgFunc; // window messages callback
void* m_IsDeviceAcceptableFuncUserContext; // user context for is device acceptable callback
void* m_ModifyDeviceSettingsFuncUserContext; // user context for modify device settings callback
void* m_DeviceCreatedUserContext; // user context for device created callback
void* m_DeviceCreatedFuncUserContext; // user context for device created callback
void* m_DeviceResetFuncUserContext; // user context for device reset callback
void* m_DeviceLostFuncUserContext; // user context for device lost callback
void* m_DeviceDestroyedFuncUserContext; // user context for device destroyed callback
void* m_FrameMoveFuncUserContext; // user context for frame move callback
void* m_FrameRenderFuncUserContext; // user context for frame render callback
void* m_KeyboardFuncUserContext; // user context for keyboard callback
void* m_MouseFuncUserContext; // user context for mouse callback
void* m_WindowMsgFuncUserContext; // user context for window messages callback
bool m_Keys[256]; // array of key state
bool m_MouseButtons[5]; // array of mouse states
CGrowableArray<DXUT_TIMER>* m_TimerList; // list of DXUT_TIMER structs
WCHAR m_StaticFrameStats[256]; // static part of frames stats
WCHAR m_FPSStats[64]; // fps stats
WCHAR m_FrameStats[256]; // frame stats (fps, width, etc)
WCHAR m_DeviceStats[256]; // device stats (description, device type, etc)
WCHAR m_WindowTitle[256]; // window title
};
STATE m_state;
public:
DXUTState() { Create(); }
~DXUTState() { Destroy(); }
void Create()
{
// Make sure these are created before DXUTState so they
// destroyed last because DXUTState cleanup needs them
DXUTGetGlobalResourceCache();
ZeroMemory( &m_state, sizeof(STATE) );
g_bThreadSafe = true;
InitializeCriticalSection( &g_cs );
m_state.m_OverrideStartX = -1;
m_state.m_OverrideStartY = -1;
m_state.m_OverrideAdapterOrdinal = -1;
m_state.m_OverrideForceVsync = -1;
m_state.m_AutoChangeAdapter = true;
m_state.m_ShowMsgBoxOnError = true;
m_state.m_AllowShortcutKeysWhenWindowed = true;
m_state.m_Active = true;
m_state.m_CallDefWindowProc = true;
}
void Destroy()
{
DXUTShutdown();
DeleteCriticalSection( &g_cs );
}
// Macros to define access functions for thread safe access into m_state
GET_SET_ACCESSOR( IDirect3D9*, D3D );
GET_SET_ACCESSOR( IDirect3DDevice9*, D3DDevice );
GET_SET_ACCESSOR( CD3DEnumeration*, D3DEnumeration );
GET_SET_ACCESSOR( DXUTDeviceSettings*, CurrentDeviceSettings );
GETP_SETP_ACCESSOR( D3DSURFACE_DESC, BackBufferSurfaceDesc );
GETP_SETP_ACCESSOR( D3DCAPS9, Caps );
GET_SET_ACCESSOR( HWND, HWNDFocus );
GET_SET_ACCESSOR( HWND, HWNDDeviceFullScreen );
GET_SET_ACCESSOR( HWND, HWNDDeviceWindowed );
GET_SET_ACCESSOR( HMONITOR, AdapterMonitor );
GET_SET_ACCESSOR( HMENU, Menu );
GET_SET_ACCESSOR( UINT, FullScreenBackBufferWidthAtModeChange );
GET_SET_ACCESSOR( UINT, FullScreenBackBufferHeightAtModeChange );
GET_SET_ACCESSOR( UINT, WindowBackBufferWidthAtModeChange );
GET_SET_ACCESSOR( UINT, WindowBackBufferHeightAtModeChange );
GETP_SETP_ACCESSOR( WINDOWPLACEMENT, WindowedPlacement );
GET_SET_ACCESSOR( DWORD, WindowedStyleAtModeChange );
GET_SET_ACCESSOR( bool, TopmostWhileWindowed );
GET_SET_ACCESSOR( bool, Minimized );
GET_SET_ACCESSOR( bool, Maximized );
GET_SET_ACCESSOR( bool, MinimizedWhileFullscreen );
GET_SET_ACCESSOR( bool, IgnoreSizeChange );
GET_SET_ACCESSOR( double, Time );
GET_SET_ACCESSOR( double, AbsoluteTime );
GET_SET_ACCESSOR( float, ElapsedTime );
GET_SET_ACCESSOR( HINSTANCE, HInstance );
GET_SET_ACCESSOR( double, LastStatsUpdateTime );
GET_SET_ACCESSOR( DWORD, LastStatsUpdateFrames );
GET_SET_ACCESSOR( float, FPS );
GET_SET_ACCESSOR( int, CurrentFrameNumber );
GET_SET_ACCESSOR( HHOOK, KeyboardHook );
GET_SET_ACCESSOR( bool, AllowShortcutKeysWhenFullscreen );
GET_SET_ACCESSOR( bool, AllowShortcutKeysWhenWindowed );
GET_SET_ACCESSOR( bool, AllowShortcutKeys );
GET_SET_ACCESSOR( bool, CallDefWindowProc );
GET_SET_ACCESSOR( STICKYKEYS, StartupStickyKeys );
GET_SET_ACCESSOR( TOGGLEKEYS, StartupToggleKeys );
GET_SET_ACCESSOR( FILTERKEYS, StartupFilterKeys );
GET_SET_ACCESSOR( bool, HandleDefaultHotkeys );
GET_SET_ACCESSOR( bool, HandleAltEnter );
GET_SET_ACCESSOR( bool, ShowMsgBoxOnError );
GET_SET_ACCESSOR( bool, NoStats );
GET_SET_ACCESSOR( bool, ClipCursorWhenFullScreen );
GET_SET_ACCESSOR( bool, ShowCursorWhenFullScreen );
GET_SET_ACCESSOR( bool, ConstantFrameTime );
GET_SET_ACCESSOR( float, TimePerFrame );
GET_SET_ACCESSOR( bool, WireframeMode );
GET_SET_ACCESSOR( bool, AutoChangeAdapter );
GET_SET_ACCESSOR( bool, WindowCreatedWithDefaultPositions );
GET_SET_ACCESSOR( int, ExitCode );
GET_SET_ACCESSOR( bool, DXUTInited );
GET_SET_ACCESSOR( bool, WindowCreated );
GET_SET_ACCESSOR( bool, DeviceCreated );
GET_SET_ACCESSOR( bool, DXUTInitCalled );
GET_SET_ACCESSOR( bool, WindowCreateCalled );
GET_SET_ACCESSOR( bool, DeviceCreateCalled );
GET_SET_ACCESSOR( bool, InsideDeviceCallback );
GET_SET_ACCESSOR( bool, InsideMainloop );
GET_SET_ACCESSOR( bool, DeviceObjectsCreated );
GET_SET_ACCESSOR( bool, DeviceObjectsReset );
GET_SET_ACCESSOR( bool, Active );
GET_SET_ACCESSOR( bool, RenderingPaused );
GET_SET_ACCESSOR( bool, TimePaused );
GET_SET_ACCESSOR( int, PauseRenderingCount );
GET_SET_ACCESSOR( int, PauseTimeCount );
GET_SET_ACCESSOR( bool, DeviceLost );
GET_SET_ACCESSOR( bool, NotifyOnMouseMove );
GET_SET_ACCESSOR( bool, Automation );
GET_SET_ACCESSOR( bool, InSizeMove );
GET_SET_ACCESSOR( UINT, TimerLastID );
GET_SET_ACCESSOR( int, OverrideAdapterOrdinal );
GET_SET_ACCESSOR( bool, OverrideWindowed );
GET_SET_ACCESSOR( bool, OverrideFullScreen );
GET_SET_ACCESSOR( int, OverrideStartX );
GET_SET_ACCESSOR( int, OverrideStartY );
GET_SET_ACCESSOR( int, OverrideWidth );
GET_SET_ACCESSOR( int, OverrideHeight );
GET_SET_ACCESSOR( bool, OverrideForceHAL );
GET_SET_ACCESSOR( bool, OverrideForceREF );
GET_SET_ACCESSOR( bool, OverrideForcePureHWVP );
GET_SET_ACCESSOR( bool, OverrideForceHWVP );
GET_SET_ACCESSOR( bool, OverrideForceSWVP );
GET_SET_ACCESSOR( bool, OverrideConstantFrameTime );
GET_SET_ACCESSOR( float, OverrideConstantTimePerFrame );
GET_SET_ACCESSOR( int, OverrideQuitAfterFrame );
GET_SET_ACCESSOR( int, OverrideForceVsync );
GET_SET_ACCESSOR( bool, OverrideRelaunchMCE );
GET_SET_ACCESSOR( LPDXUTCALLBACKISDEVICEACCEPTABLE, IsDeviceAcceptableFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKMODIFYDEVICESETTINGS, ModifyDeviceSettingsFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKDEVICECREATED, DeviceCreatedFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKDEVICERESET, DeviceResetFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKDEVICELOST, DeviceLostFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKDEVICEDESTROYED, DeviceDestroyedFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKFRAMEMOVE, FrameMoveFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKFRAMERENDER, FrameRenderFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKKEYBOARD, KeyboardFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKMOUSE, MouseFunc );
GET_SET_ACCESSOR( LPDXUTCALLBACKMSGPROC, WindowMsgFunc );
GET_SET_ACCESSOR( void*, IsDeviceAcceptableFuncUserContext );
GET_SET_ACCESSOR( void*, ModifyDeviceSettingsFuncUserContext );
GET_SET_ACCESSOR( void*, DeviceCreatedFuncUserContext );
GET_SET_ACCESSOR( void*, DeviceResetFuncUserContext );
GET_SET_ACCESSOR( void*, DeviceLostFuncUserContext );
GET_SET_ACCESSOR( void*, DeviceDestroyedFuncUserContext );
GET_SET_ACCESSOR( void*, FrameMoveFuncUserContext );
GET_SET_ACCESSOR( void*, FrameRenderFuncUserContext );
GET_SET_ACCESSOR( void*, KeyboardFuncUserContext );
GET_SET_ACCESSOR( void*, MouseFuncUserContext );
GET_SET_ACCESSOR( void*, WindowMsgFuncUserContext );
GET_SET_ACCESSOR( CGrowableArray<DXUT_TIMER>*, TimerList );
GET_ACCESSOR( bool*, Keys );
GET_ACCESSOR( bool*, MouseButtons );
GET_ACCESSOR( WCHAR*, StaticFrameStats );
GET_ACCESSOR( WCHAR*, FPSStats );
GET_ACCESSOR( WCHAR*, FrameStats );
GET_ACCESSOR( WCHAR*, DeviceStats );
GET_ACCESSOR( WCHAR*, WindowTitle );
};
//--------------------------------------------------------------------------------------
// Global state class
//--------------------------------------------------------------------------------------
DXUTState& GetDXUTState()
{
// Using an accessor function gives control of the construction order
static DXUTState state;
return state;
}
//--------------------------------------------------------------------------------------
// Internal functions forward declarations
//--------------------------------------------------------------------------------------
typedef IDirect3D9* (WINAPI* LPDIRECT3DCREATE9)(UINT SDKVersion);
typedef DECLSPEC_IMPORT UINT (WINAPI* LPTIMEBEGINPERIOD)( UINT uPeriod );
int DXUTMapButtonToArrayIndex( BYTE vButton );
void DXUTSetProcessorAffinity();
void DXUTParseCommandLine();
CD3DEnumeration* DXUTPrepareEnumerationObject( bool bEnumerate = false );
void DXUTBuildOptimalDeviceSettings( DXUTDeviceSettings* pOptimalDeviceSettings, DXUTDeviceSettings* pDeviceSettingsIn, DXUTMatchOptions* pMatchOptions );
bool DXUTDoesDeviceComboMatchPreserveOptions( CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo, DXUTDeviceSettings* pDeviceSettingsIn, DXUTMatchOptions* pMatchOptions );
float DXUTRankDeviceCombo( CD3DEnumDeviceSettingsCombo* pDeviceSettingsCombo, DXUTDeviceSettings* pDeviceSettingsIn, D3DDISPLAYMODE* pAdapterDesktopDisplayMode );
void DXUTBuildValidDeviceSettings( DXUTDeviceSettings* pDeviceSettings, CD3DEnumDeviceSettingsCombo* pBestDeviceSettingsCombo, DXUTDeviceSettings* pDeviceSettingsIn, DXUTMatchOptions* pMatchOptions );
HRESULT DXUTFindValidResolution( CD3DEnumDeviceSettingsCombo* pBestDeviceSettingsCombo, D3DDISPLAYMODE displayModeIn, D3DDISPLAYMODE* pBestDisplayMode );
HRESULT DXUTFindAdapterFormat( UINT AdapterOrdinal, D3DDEVTYPE DeviceType, D3DFORMAT BackBufferFormat, BOOL Windowed, D3DFORMAT* pAdapterFormat );
HRESULT DXUTChangeDevice( DXUTDeviceSettings* pNewDeviceSettings, IDirect3DDevice9* pd3dDeviceFromApp, bool bForceRecreate, bool bClipWindowToSingleAdapter );
void DXUTUpdateDeviceSettingsWithOverrides( DXUTDeviceSettings* pNewDeviceSettings );
HRESULT DXUTCreate3DEnvironment( IDirect3DDevice9* pd3dDeviceFromApp );
HRESULT DXUTReset3DEnvironment();
void DXUTRender3DEnvironment();
void DXUTCleanup3DEnvironment( bool bReleaseSettings = true );
void DXUTUpdateFrameStats();
void DXUTUpdateDeviceStats( D3DDEVTYPE DeviceType, DWORD BehaviorFlags, D3DADAPTER_IDENTIFIER9* pAdapterIdentifier );
void DXUTUpdateStaticFrameStats();
void DXUTHandleTimers();
bool DXUTIsNextArg( WCHAR*& strCmdLine, WCHAR* strArg );
bool DXUTGetCmdParam( WCHAR*& strCmdLine, WCHAR* strFlag );
void DXUTDisplayErrorMessage( HRESULT hr );
LRESULT CALLBACK DXUTStaticWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
void DXUTCheckForWindowSizeChange();
void DXUTCheckForWindowChangingMonitors();
UINT DXUTColorChannelBits( D3DFORMAT fmt );
UINT DXUTStencilBits( D3DFORMAT fmt );
UINT DXUTDepthBits( D3DFORMAT fmt );
HRESULT DXUTGetAdapterOrdinalFromMonitor( HMONITOR hMonitor, UINT* pAdapterOrdinal );
void DXUTAllowShortcutKeys( bool bAllowKeys );
void DXUTUpdateBackBufferDesc();
void DXUTSetupCursor();
HRESULT DXUTSetDeviceCursor( IDirect3DDevice9* pd3dDevice, HCURSOR hCursor, bool bAddWatermark );
//--------------------------------------------------------------------------------------
// External callback setup functions
//--------------------------------------------------------------------------------------
void DXUTSetCallbackDeviceCreated( LPDXUTCALLBACKDEVICECREATED pCallbackDeviceCreated, void* pUserContext ) { GetDXUTState().SetDeviceCreatedFunc( pCallbackDeviceCreated ); GetDXUTState().SetDeviceCreatedFuncUserContext( pUserContext ); }
void DXUTSetCallbackDeviceReset( LPDXUTCALLBACKDEVICERESET pCallbackDeviceReset, void* pUserContext ) { GetDXUTState().SetDeviceResetFunc( pCallbackDeviceReset ); GetDXUTState().SetDeviceResetFuncUserContext( pUserContext ); }
void DXUTSetCallbackDeviceLost( LPDXUTCALLBACKDEVICELOST pCallbackDeviceLost, void* pUserContext ) { GetDXUTState().SetDeviceLostFunc( pCallbackDeviceLost ); GetDXUTState().SetDeviceLostFuncUserContext( pUserContext ); }
void DXUTSetCallbackDeviceDestroyed( LPDXUTCALLBACKDEVICEDESTROYED pCallbackDeviceDestroyed, void* pUserContext ) { GetDXUTState().SetDeviceDestroyedFunc( pCallbackDeviceDestroyed ); GetDXUTState().SetDeviceDestroyedFuncUserContext( pUserContext ); }
void DXUTSetCallbackDeviceChanging( LPDXUTCALLBACKMODIFYDEVICESETTINGS pCallbackModifyDeviceSettings, void* pUserContext ) { GetDXUTState().SetModifyDeviceSettingsFunc( pCallbackModifyDeviceSettings ); GetDXUTState().SetModifyDeviceSettingsFuncUserContext( pUserContext ); }
void DXUTSetCallbackFrameMove( LPDXUTCALLBACKFRAMEMOVE pCallbackFrameMove, void* pUserContext ) { GetDXUTState().SetFrameMoveFunc( pCallbackFrameMove ); GetDXUTState().SetFrameMoveFuncUserContext( pUserContext ); }
void DXUTSetCallbackFrameRender( LPDXUTCALLBACKFRAMERENDER pCallbackFrameRender, void* pUserContext ) { GetDXUTState().SetFrameRenderFunc( pCallbackFrameRender ); GetDXUTState().SetFrameRenderFuncUserContext( pUserContext ); }
void DXUTSetCallbackKeyboard( LPDXUTCALLBACKKEYBOARD pCallbackKeyboard, void* pUserContext ) { GetDXUTState().SetKeyboardFunc( pCallbackKeyboard ); GetDXUTState().SetKeyboardFuncUserContext( pUserContext ); }
void DXUTSetCallbackMouse( LPDXUTCALLBACKMOUSE pCallbackMouse, bool bIncludeMouseMove, void* pUserContext ) { GetDXUTState().SetMouseFunc( pCallbackMouse ); GetDXUTState().SetNotifyOnMouseMove( bIncludeMouseMove ); GetDXUTState().SetMouseFuncUserContext( pUserContext ); }
void DXUTSetCallbackMsgProc( LPDXUTCALLBACKMSGPROC pCallbackMsgProc, void* pUserContext ) { GetDXUTState().SetWindowMsgFunc( pCallbackMsgProc ); GetDXUTState().SetWindowMsgFuncUserContext( pUserContext ); }
//--------------------------------------------------------------------------------------
// Optionally parses the command line and sets if default hotkeys are handled
//
// Possible command line parameters are:
// -adapter:# forces app to use this adapter # (fails if the adapter doesn't exist)
// -windowed forces app to start windowed
// -fullscreen forces app to start full screen
// -forcehal forces app to use HAL (fails if HAL doesn't exist)
// -forceref forces app to use REF (fails if REF doesn't exist)
// -forcepurehwvp forces app to use pure HWVP (fails if device doesn't support it)
// -forcehwvp forces app to use HWVP (fails if device doesn't support it)
// -forceswvp forces app to use SWVP
// -forcevsync:# if # is 0, forces app to use D3DPRESENT_INTERVAL_IMMEDIATE otherwise force use of D3DPRESENT_INTERVAL_DEFAULT
// -width:# forces app to use # for width. for full screen, it will pick the closest possible supported mode
// -height:# forces app to use # for height. for full screen, it will pick the closest possible supported mode
// -startx:# forces app to use # for the x coord of the window position for windowed mode
// -starty:# forces app to use # for the y coord of the window position for windowed mode
// -constantframetime:# forces app to use constant frame time, where # is the time/frame in seconds
// -quitafterframe:x forces app to quit after # frames
// -noerrormsgboxes prevents the display of message boxes generated by the framework so the application can be run without user interaction
// -nostats prevents the display of the stats
// -relaunchmce re-launches the MCE UI after the app exits
// -automation every CDXUTDialog created will have EnableKeyboardInput(true) called, enabling UI navigation with keyboard
// This is useful when automating application testing.
//
// Hotkeys handled by default are:
// Alt-Enter toggle between full screen & windowed (hotkey always enabled)
// ESC exit app
// F3 toggle HAL/REF
// F8 toggle wire-frame mode
// Pause pause time
//--------------------------------------------------------------------------------------
HRESULT DXUTInit( bool bParseCommandLine, bool bHandleDefaultHotkeys, bool bShowMsgBoxOnError, bool bHandleAltEnter )
{
GetDXUTState().SetDXUTInitCalled( true );
// Not always needed, but lets the app create GDI dialogs
InitCommonControls();
// Save the current sticky/toggle/filter key settings so DXUT can restore them later
STICKYKEYS sk = {sizeof(STICKYKEYS), 0};
SystemParametersInfo(SPI_GETSTICKYKEYS, sizeof(STICKYKEYS), &sk, 0);
GetDXUTState().SetStartupStickyKeys( sk );
TOGGLEKEYS tk = {sizeof(TOGGLEKEYS), 0};
SystemParametersInfo(SPI_GETTOGGLEKEYS, sizeof(TOGGLEKEYS), &tk, 0);
GetDXUTState().SetStartupToggleKeys( tk );
FILTERKEYS fk = {sizeof(FILTERKEYS), 0};
SystemParametersInfo(SPI_GETFILTERKEYS, sizeof(FILTERKEYS), &fk, 0);
GetDXUTState().SetStartupFilterKeys( fk );
// Increase the accuracy of Sleep() without needing to link to winmm.lib
HINSTANCE hInstWinMM = LoadLibrary( L"winmm.dll" );
if( hInstWinMM )
{
LPTIMEBEGINPERIOD pTimeBeginPeriod = (LPTIMEBEGINPERIOD)GetProcAddress( hInstWinMM, "timeBeginPeriod" );
if( NULL != pTimeBeginPeriod )
pTimeBeginPeriod(1);
FreeLibrary(hInstWinMM);
}
GetDXUTState().SetShowMsgBoxOnError( bShowMsgBoxOnError );
GetDXUTState().SetHandleDefaultHotkeys( bHandleDefaultHotkeys );
GetDXUTState().SetHandleAltEnter( bHandleAltEnter );
if( bParseCommandLine )
DXUTParseCommandLine();
// Verify D3DX version
if( !D3DXCheckVersion( D3D_SDK_VERSION, D3DX_SDK_VERSION ) )
{
DXUTDisplayErrorMessage( DXUTERR_INCORRECTVERSION );
return DXUT_ERR( L"D3DXCheckVersion", DXUTERR_INCORRECTVERSION );
}
// Create a Direct3D object if one has not already been created
IDirect3D9* pD3D = DXUTGetD3DObject();
if( pD3D == NULL )
{
// This may fail if DirectX 9 isn't installed
// This may fail if the DirectX headers are out of sync with the installed DirectX DLLs
pD3D = DXUT_Dynamic_Direct3DCreate9( D3D_SDK_VERSION );
GetDXUTState().SetD3D( pD3D );
}
if( pD3D == NULL )
{
// If still NULL, then something went wrong
DXUTDisplayErrorMessage( DXUTERR_NODIRECT3D );
return DXUT_ERR( L"Direct3DCreate9", DXUTERR_NODIRECT3D );
}
// Reset the timer
DXUTGetGlobalTimer()->Reset();
GetDXUTState().SetDXUTInited( true );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Parses the command line for parameters. See DXUTInit() for list
//--------------------------------------------------------------------------------------
void DXUTParseCommandLine()
{
WCHAR* strCmdLine;
WCHAR strFlag[MAX_PATH];
int nNumArgs;
WCHAR** pstrArgList = CommandLineToArgvW( GetCommandLine(), &nNumArgs );
for( int iArg=1; iArg<nNumArgs; iArg++ )
{
strCmdLine = pstrArgList[iArg];
// Handle flag args
if( *strCmdLine == L'/' || *strCmdLine == L'-' )
{
strCmdLine++;
if( DXUTIsNextArg( strCmdLine, L"adapter" ) )
{
if( DXUTGetCmdParam( strCmdLine, strFlag ) )
{
int nAdapter = _wtoi(strFlag);
GetDXUTState().SetOverrideAdapterOrdinal( nAdapter );
continue;
}
}
if( DXUTIsNextArg( strCmdLine, L"windowed" ) )
{
GetDXUTState().SetOverrideWindowed( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"fullscreen" ) )
{
GetDXUTState().SetOverrideFullScreen( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"forcehal" ) )
{
GetDXUTState().SetOverrideForceHAL( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"forceref" ) )
{
GetDXUTState().SetOverrideForceREF( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"forcepurehwvp" ) )
{
GetDXUTState().SetOverrideForcePureHWVP( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"forcehwvp" ) )
{
GetDXUTState().SetOverrideForceHWVP( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"forceswvp" ) )
{
GetDXUTState().SetOverrideForceSWVP( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"forcevsync" ) )
{
if( DXUTGetCmdParam( strCmdLine, strFlag ) )
{
int nOn = _wtoi(strFlag);
GetDXUTState().SetOverrideForceVsync( nOn );
continue;
}
}
if( DXUTIsNextArg( strCmdLine, L"width" ) )
{
if( DXUTGetCmdParam( strCmdLine, strFlag ) )
{
int nWidth = _wtoi(strFlag);
GetDXUTState().SetOverrideWidth( nWidth );
continue;
}
}
if( DXUTIsNextArg( strCmdLine, L"height" ) )
{
if( DXUTGetCmdParam( strCmdLine, strFlag ) )
{
int nHeight = _wtoi(strFlag);
GetDXUTState().SetOverrideHeight( nHeight );
continue;
}
}
if( DXUTIsNextArg( strCmdLine, L"startx" ) )
{
if( DXUTGetCmdParam( strCmdLine, strFlag ) )
{
int nX = _wtoi(strFlag);
GetDXUTState().SetOverrideStartX( nX );
continue;
}
}
if( DXUTIsNextArg( strCmdLine, L"starty" ) )
{
if( DXUTGetCmdParam( strCmdLine, strFlag ) )
{
int nY = _wtoi(strFlag);
GetDXUTState().SetOverrideStartY( nY );
continue;
}
}
if( DXUTIsNextArg( strCmdLine, L"constantframetime" ) )
{
float fTimePerFrame;
if( DXUTGetCmdParam( strCmdLine, strFlag ) )
fTimePerFrame = (float)wcstod( strFlag, NULL );
else
fTimePerFrame = 0.0333f;
GetDXUTState().SetOverrideConstantFrameTime( true );
GetDXUTState().SetOverrideConstantTimePerFrame( fTimePerFrame );
DXUTSetConstantFrameTime( true, fTimePerFrame );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"quitafterframe" ) )
{
if( DXUTGetCmdParam( strCmdLine, strFlag ) )
{
int nFrame = _wtoi(strFlag);
GetDXUTState().SetOverrideQuitAfterFrame( nFrame );
continue;
}
}
if( DXUTIsNextArg( strCmdLine, L"noerrormsgboxes" ) )
{
GetDXUTState().SetShowMsgBoxOnError( false );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"nostats" ) )
{
GetDXUTState().SetNoStats( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"relaunchmce" ) )
{
GetDXUTState().SetOverrideRelaunchMCE( true );
continue;
}
if( DXUTIsNextArg( strCmdLine, L"automation" ) )
{
GetDXUTState().SetAutomation( true );
continue;
}
}
// Unrecognized flag
StringCchCopy( strFlag, 256, strCmdLine );
WCHAR* strSpace = strFlag;
while (*strSpace && (*strSpace > L' '))
strSpace++;
*strSpace = 0;
DXUTOutputDebugString( L"Unrecognized flag: %s", strFlag );
strCmdLine += wcslen(strFlag);
}
}
//--------------------------------------------------------------------------------------
// Helper function for DXUTParseCommandLine
//--------------------------------------------------------------------------------------
bool DXUTIsNextArg( WCHAR*& strCmdLine, WCHAR* strArg )
{
int nArgLen = (int) wcslen(strArg);
int nCmdLen = (int) wcslen(strCmdLine);
if( nCmdLen >= nArgLen &&
_wcsnicmp( strCmdLine, strArg, nArgLen ) == 0 &&
(strCmdLine[nArgLen] == 0 || strCmdLine[nArgLen] == L':') )
{
strCmdLine += nArgLen;
return true;
}
return false;
}
//--------------------------------------------------------------------------------------
// Helper function for DXUTParseCommandLine. Updates strCmdLine and strFlag
// Example: if strCmdLine=="-width:1024 -forceref"
// then after: strCmdLine==" -forceref" and strFlag=="1024"
//--------------------------------------------------------------------------------------
bool DXUTGetCmdParam( WCHAR*& strCmdLine, WCHAR* strFlag )
{
if( *strCmdLine == L':' )
{
strCmdLine++; // Skip ':'
// Place NULL terminator in strFlag after current token
StringCchCopy( strFlag, 256, strCmdLine );
WCHAR* strSpace = strFlag;
while (*strSpace && (*strSpace > L' '))
strSpace++;
*strSpace = 0;
// Update strCmdLine
strCmdLine += wcslen(strFlag);
return true;
}
else
{
strFlag[0] = 0;
return false;
}
}
//--------------------------------------------------------------------------------------
// Creates a window with the specified window title, icon, menu, and
// starting position. If DXUTInit() has not already been called, it will
// call it with the default parameters. Instead of calling this, you can
// call DXUTSetWindow() to use an existing window.
//--------------------------------------------------------------------------------------
HRESULT DXUTCreateWindow( const WCHAR* strWindowTitle, HINSTANCE hInstance,
HICON hIcon, HMENU hMenu, int x, int y )
{
HRESULT hr;
// Not allowed to call this from inside the device callbacks
if( GetDXUTState().GetInsideDeviceCallback() )
return DXUT_ERR_MSGBOX( L"DXUTCreateWindow", E_FAIL );
GetDXUTState().SetWindowCreateCalled( true );
if( !GetDXUTState().GetDXUTInited() )
{
// If DXUTInit() was already called and failed, then fail.
// DXUTInit() must first succeed for this function to succeed
if( GetDXUTState().GetDXUTInitCalled() )
return E_FAIL;
// If DXUTInit() hasn't been called, then automatically call it
// with default params
hr = DXUTInit();
if( FAILED(hr) )
return hr;
}
if( DXUTGetHWNDFocus() == NULL )
{
if( hInstance == NULL )
hInstance = (HINSTANCE)GetModuleHandle(NULL);
GetDXUTState().SetHInstance( hInstance );
WCHAR szExePath[MAX_PATH];
GetModuleFileName( NULL, szExePath, MAX_PATH );
if( hIcon == NULL ) // If the icon is NULL, then use the first one found in the exe
hIcon = ExtractIcon( hInstance, szExePath, 0 );
// Register the windows class
WNDCLASS wndClass;
wndClass.style = CS_DBLCLKS;
wndClass.lpfnWndProc = DXUTStaticWndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = hIcon;
wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = L"Direct3DWindowClass";
if( !RegisterClass( &wndClass ) )
{
DWORD dwError = GetLastError();
if( dwError != ERROR_CLASS_ALREADY_EXISTS )
return DXUT_ERR_MSGBOX( L"RegisterClass", HRESULT_FROM_WIN32(dwError) );
}
RECT rc;
// Override the window's initial & size position if there were cmd line args
if( GetDXUTState().GetOverrideStartX() != -1 )
x = GetDXUTState().GetOverrideStartX();
if( GetDXUTState().GetOverrideStartY() != -1 )
y = GetDXUTState().GetOverrideStartY();
GetDXUTState().SetWindowCreatedWithDefaultPositions( false );
if( x == CW_USEDEFAULT && y == CW_USEDEFAULT )
GetDXUTState().SetWindowCreatedWithDefaultPositions( true );
// Find the window's initial size, but it might be changed later
int nDefaultWidth = 640;
int nDefaultHeight = 480;
if( GetDXUTState().GetOverrideWidth() != 0 )
nDefaultWidth = GetDXUTState().GetOverrideWidth();
if( GetDXUTState().GetOverrideHeight() != 0 )
nDefaultHeight = GetDXUTState().GetOverrideHeight();
SetRect( &rc, 0, 0, nDefaultWidth, nDefaultHeight );
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, ( hMenu != NULL ) ? true : false );
WCHAR* strCachedWindowTitle = GetDXUTState().GetWindowTitle();
StringCchCopy( strCachedWindowTitle, 256, strWindowTitle );
// Create the render window
HWND hWnd = CreateWindow( L"Direct3DWindowClass", strWindowTitle, WS_OVERLAPPEDWINDOW,
x, y, (rc.right-rc.left), (rc.bottom-rc.top), 0,
hMenu, hInstance, 0 );
if( hWnd == NULL )
{
DWORD dwError = GetLastError();
return DXUT_ERR_MSGBOX( L"CreateWindow", HRESULT_FROM_WIN32(dwError) );
}
GetDXUTState().SetWindowCreated( true );
GetDXUTState().SetHWNDFocus( hWnd );
GetDXUTState().SetHWNDDeviceFullScreen( hWnd );
GetDXUTState().SetHWNDDeviceWindowed( hWnd );
}
return S_OK;
}
//--------------------------------------------------------------------------------------
// Sets a previously created window for the framework to use. If DXUTInit()
// has not already been called, it will call it with the default parameters.
// Instead of calling this, you can call DXUTCreateWindow() to create a new window.
//--------------------------------------------------------------------------------------
HRESULT DXUTSetWindow( HWND hWndFocus, HWND hWndDeviceFullScreen, HWND hWndDeviceWindowed, bool bHandleMessages )
{
HRESULT hr;
// Not allowed to call this from inside the device callbacks
if( GetDXUTState().GetInsideDeviceCallback() )
return DXUT_ERR_MSGBOX( L"DXUTCreateWindow", E_FAIL );
GetDXUTState().SetWindowCreateCalled( true );
// To avoid confusion, we do not allow any HWND to be NULL here. The
// caller must pass in valid HWND for all three parameters. The same
// HWND may be used for more than one parameter.
if( hWndFocus == NULL || hWndDeviceFullScreen == NULL || hWndDeviceWindowed == NULL )
return DXUT_ERR_MSGBOX( L"DXUTSetWindow", E_INVALIDARG );
// If subclassing the window, set the pointer to the local window procedure
if( bHandleMessages )
{
// Switch window procedures
#ifdef _WIN64
LONG_PTR nResult = SetWindowLongPtr( hWndFocus, GWLP_WNDPROC, (LONG_PTR)DXUTStaticWndProc );
#else
LONG_PTR nResult = SetWindowLongPtr( hWndFocus, GWLP_WNDPROC, (LONG)(LONG_PTR)DXUTStaticWndProc );
#endif
DWORD dwError = GetLastError();
if( nResult == 0 )
return DXUT_ERR_MSGBOX( L"SetWindowLongPtr", HRESULT_FROM_WIN32(dwError) );
}
if( !GetDXUTState().GetDXUTInited() )
{
// If DXUTInit() was already called and failed, then fail.
// DXUTInit() must first succeed for this function to succeed
if( GetDXUTState().GetDXUTInitCalled() )
return E_FAIL;
// If DXUTInit() hasn't been called, then automatically call it
// with default params
hr = DXUTInit();
if( FAILED(hr) )
return hr;
}
WCHAR* strCachedWindowTitle = GetDXUTState().GetWindowTitle();
GetWindowText( hWndFocus, strCachedWindowTitle, 255 );
strCachedWindowTitle[255] = 0;
HINSTANCE hInstance = (HINSTANCE) (LONG_PTR) GetWindowLongPtr( hWndFocus, GWLP_HINSTANCE );
GetDXUTState().SetHInstance( hInstance );
GetDXUTState().SetWindowCreatedWithDefaultPositions( false );
GetDXUTState().SetWindowCreated( true );
GetDXUTState().SetHWNDFocus( hWndFocus );
GetDXUTState().SetHWNDDeviceFullScreen( hWndDeviceFullScreen );
GetDXUTState().SetHWNDDeviceWindowed( hWndDeviceWindowed );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Creates a Direct3D device. If DXUTCreateWindow() or DXUTSetWindow() has not already
// been called, it will call DXUTCreateWindow() with the default parameters.
// Instead of calling this, you can call DXUTSetDevice() or DXUTCreateDeviceFromSettings()
//--------------------------------------------------------------------------------------
HRESULT DXUTCreateDevice( UINT AdapterOrdinal, bool bWindowed,
int nSuggestedWidth, int nSuggestedHeight,
LPDXUTCALLBACKISDEVICEACCEPTABLE pCallbackIsDeviceAcceptable,
LPDXUTCALLBACKMODIFYDEVICESETTINGS pCallbackModifyDeviceSettings,
void* pUserContext )
{
HRESULT hr;
// Not allowed to call this from inside the device callbacks
if( GetDXUTState().GetInsideDeviceCallback() )
return DXUT_ERR_MSGBOX( L"DXUTCreateWindow", E_FAIL );
// Record the function arguments in the global state
GetDXUTState().SetIsDeviceAcceptableFunc( pCallbackIsDeviceAcceptable );
GetDXUTState().SetModifyDeviceSettingsFunc( pCallbackModifyDeviceSettings );
GetDXUTState().SetIsDeviceAcceptableFuncUserContext( pUserContext );
GetDXUTState().SetModifyDeviceSettingsFuncUserContext( pUserContext );
GetDXUTState().SetDeviceCreateCalled( true );
// If DXUTCreateWindow() or DXUTSetWindow() has not already been called,
// then call DXUTCreateWindow() with the default parameters.
if( !GetDXUTState().GetWindowCreated() )
{
// If DXUTCreateWindow() or DXUTSetWindow() was already called and failed, then fail.
// DXUTCreateWindow() or DXUTSetWindow() must first succeed for this function to succeed
if( GetDXUTState().GetWindowCreateCalled() )
return E_FAIL;
// If DXUTCreateWindow() or DXUTSetWindow() hasn't been called, then
// automatically call DXUTCreateWindow() with default params
hr = DXUTCreateWindow();
if( FAILED(hr) )
return hr;
}
// Force an enumeration with the new IsDeviceAcceptable callback
DXUTPrepareEnumerationObject( true );
DXUTMatchOptions matchOptions;
matchOptions.eAdapterOrdinal = DXUTMT_PRESERVE_INPUT;
matchOptions.eDeviceType = DXUTMT_IGNORE_INPUT;
matchOptions.eWindowed = DXUTMT_PRESERVE_INPUT;
matchOptions.eAdapterFormat = DXUTMT_IGNORE_INPUT;
matchOptions.eVertexProcessing = DXUTMT_IGNORE_INPUT;
if( bWindowed || (nSuggestedWidth != 0 && nSuggestedHeight != 0) )
matchOptions.eResolution = DXUTMT_CLOSEST_TO_INPUT;
else
matchOptions.eResolution = DXUTMT_IGNORE_INPUT;
matchOptions.eBackBufferFormat = DXUTMT_IGNORE_INPUT;
matchOptions.eBackBufferCount = DXUTMT_IGNORE_INPUT;
matchOptions.eMultiSample = DXUTMT_IGNORE_INPUT;
matchOptions.eSwapEffect = DXUTMT_IGNORE_INPUT;
matchOptions.eDepthFormat = DXUTMT_IGNORE_INPUT;
matchOptions.eStencilFormat = DXUTMT_IGNORE_INPUT;
matchOptions.ePresentFlags = DXUTMT_IGNORE_INPUT;
matchOptions.eRefreshRate = DXUTMT_IGNORE_INPUT;
matchOptions.ePresentInterval = DXUTMT_IGNORE_INPUT;