-
Notifications
You must be signed in to change notification settings - Fork 1
/
homework 4.cpp
1939 lines (1619 loc) · 57.7 KB
/
homework 4.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
//--------------------------------------------------------------------------------------
// File: lecture 8.cpp
//
// This application demonstrates texturing
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "groundwork.h"
#include "explosion.h"
#include "Font.h"
#include "sound.h"
#include <sstream>
/*
TEAM MEMBERS:
Ediberto Cruz: EC
Salvador Hernandez: SH
Maria Loza: ML
Ana Peña: AP
Features Completed:
*****11/3/16*************************************
Maze Design - Ana created the bitmap from scratch
Skybox Implementation (Sphere) -Ediberto added the sky sphere
Shooting Mechanic - Salvador upgraded the shooting to have multiple bullets at once
Fire Multiple Shots
Collision Detection with walls - Maria upgraded the walls. The camera no longer passes through the walls.
Addition of Heads Up Display(HUD) -Maria added the HUD and the ability to change the color.
HUD displays current color
Crosshairs in center of screen - Salvador added the crosshair to the frame/HUD texture.
New wall textures -Ediberto looked up and added the new textures.
Addition of Enemies
*************************************************
2nd Milestone Completed:
-----11/17/16------------------------------------
Enemy now moves from 3 different waypoints and doesn't run to into any walls. -Maria and Ana
Enemy now changes color after a certain amount of time -SH
Added Explosions when enemy is hit -SH & -EC
Added sounds when firing bullet -SH & -EC
3rd Milestone Completed:
-----12/3/16--------------------------------------
Enemy now shoots at the player if they are different colors upon detection. -ML
Program will be over if player has no more lives. -ML
Added movement for the rest of the enemies. -ML & AP
Player does not die upon collision if they're the same color. -SH
Bullet color detection with enemy -SH
Added new sounds for gunshots and enemy hit -SH
Added reload mechanic -SH
Added player and enemy life -SH
*/
//--------------------------------------------------------------------------------------
// Structures
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Global Variables
//--------------------------------------------------------------------------------------
HINSTANCE g_hInst = NULL;
HWND g_hWnd = NULL;
D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL;
D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;
ID3D11Device* g_pd3dDevice = NULL;
ID3D11DeviceContext* g_pImmediateContext = NULL;
IDXGISwapChain* g_pSwapChain = NULL;
ID3D11RenderTargetView* g_pRenderTargetView = NULL;
ID3D11Texture2D* g_pDepthStencil = NULL;
ID3D11DepthStencilView* g_pDepthStencilView = NULL;
ID3D11VertexShader* g_pVertexShader = NULL;
ID3D11PixelShader* g_pPixelShader = NULL;
ID3D11PixelShader* g_pPixelShader_frame = NULL; //pixel shader for the frame -ML
ID3D11PixelShader* g_pPixelShader_enemies = NULL; //pixel shader for the enemies -SH
ID3D11PixelShader* g_pPixelShader_bullets = NULL; //pixel shader for the bullets -SH
ID3D11InputLayout* g_pVertexLayout = NULL;
ID3D11Buffer* g_pVertexBuffer = NULL;
ID3D11Buffer* g_pVertexBuffer_sky = NULL;
//states for turning off and on the depth buffer
ID3D11BlendState* g_BlendState;//transperancy
ID3D11DepthStencilState *ds_on, *ds_off;
int skyCount; //-EC
ID3D11Buffer* g_pCBuffer = NULL;
ID3D11ShaderResourceView* g_pTextureRV = NULL;//to create a new texture
ID3D11ShaderResourceView* g_pTextureRV1 = NULL;//to create a new texture
ID3D11ShaderResourceView* g_pTexture_enemy_bullet = NULL;//enemy bullet -SH
ID3D11ShaderResourceView* g_pTextureRV_frame = NULL; //border frame -ML
ID3D11ShaderResourceView* doorExit = NULL;
XMMATRIX doorMatrix;
ID3D11ShaderResourceView* g_pTextureRV_crosshairs = NULL; //crosshairs -SH
ID3D11ShaderResourceView* g_pTexture = NULL;
ID3D11SamplerState* g_pSamplerLinear = NULL;
XMMATRIX g_World;
XMMATRIX g_View;
XMMATRIX g_Projection;
XMFLOAT4 g_vMeshColor(0.7f, 0.7f, 0.7f, 1.0f);
camera cam;
level level1;
vector<billboard*> enemies;
vector<bullet*> bullets;
vector<bullet*> enBullets;
//-----enemy movement and their wall collison -ML && AP -----//
float speeding;
float elapsed2;
float distance1;
XMFLOAT3 direction;
XMFLOAT3 startA, startB, startC, startD, startE;
XMFLOAT3 endA, endB, endC, endD, endE;
int timeTOwin = 0;
int nextA, nextB, nextC, nextD, nextE;
bool backwardsA = false, backwardsB = false, backwardsC = false, backwardsD = false, backwardsE = false;
int index;
float idleTime;
//-----------------------------------------------------------
//needed for color change -ML
bool static currColorCheck = false;
XMFLOAT4 frameColor(0, 0, 1, 0);
//-----ememy movement and collison -ML & AP -----------------
//needed for bullet color change -SH
bool static spacebar = false;
XMFLOAT4 bulletColor(0, 0, 1, 0);
//needed for enemy color change -SH
XMFLOAT4 enemyColor(0, 0, 1, 0);
//FOR EXPLOSION -SH & -EC
/////////////////////////////
explosion_handler explosionhandler;
//FOR Font -SH & -EC
/////////////////////////////
Font font;
//Keeps track of enemies killed -SH
////////////////////////////////////
int enemiesKilled = 0;
static int ammoClip = 10;
enum enemyName {
A, B, C, D, E
};
struct waypoint {
int ID;
XMFLOAT3 position; //where its going
};
vector<vector<waypoint*>> graph; //2D arrayish column, row
waypoint* createWaypoint(XMFLOAT3 in)
{
waypoint* result = new waypoint();
result->ID = graph.size();
result->position = in;
graph.push_back(vector<waypoint*>());
return result;
}
XMFLOAT3 b(-12, 0, 8); //A2 1
XMFLOAT3 c(-12, 0, 4); //A3 2
XMFLOAT3 d(-10, 0, 32); //B1 3
XMFLOAT3 e(-26, 0, 32); //B2 4
XMFLOAT3 f(-26, 0, 36); //B3 5
XMFLOAT3 g(-6, 0, 18); //C1 6
XMFLOAT3 h(-22, 0, 18); //C2 7
XMFLOAT3 i(-22, 0, 13); //C3 8
XMFLOAT3 j(3, 0, 14); //D1 9
XMFLOAT3 k(2, 0, 18); //D2 10
XMFLOAT3 l(-3, 0, 18); //D3 11
XMFLOAT3 m(16, 0, 12); //E1 12
XMFLOAT3 n(16, 0, 26); //E2 13
XMFLOAT3 o(12, 0, 26); //E3 14
void initGraph() {
waypoint* w_a = createWaypoint(XMFLOAT3(-5, 0, 8)); //A1 0
waypoint* w_b = createWaypoint(b);
waypoint* w_c = createWaypoint(c);
waypoint* w_d = createWaypoint(d);
waypoint* w_e = createWaypoint(e);
waypoint* w_f = createWaypoint(f);
waypoint* w_g = createWaypoint(g);
waypoint* w_h = createWaypoint(h);
waypoint* w_i = createWaypoint(i);
waypoint* w_j = createWaypoint(j);
waypoint* w_k = createWaypoint(k);
waypoint* w_l = createWaypoint(l);
waypoint* w_m = createWaypoint(m);
waypoint* w_n = createWaypoint(n);
waypoint* w_o = createWaypoint(o);
graph[w_a->ID].push_back(w_a);
graph[w_a->ID].push_back(w_b);
graph[w_a->ID].push_back(w_c);
graph[w_d->ID].push_back(w_d);
graph[w_d->ID].push_back(w_e);
graph[w_d->ID].push_back(w_f);
graph[w_g->ID].push_back(w_g);
graph[w_g->ID].push_back(w_h);
graph[w_g->ID].push_back(w_i);
graph[w_j->ID].push_back(w_j);
graph[w_j->ID].push_back(w_k);
graph[w_j->ID].push_back(w_l);
graph[w_m->ID].push_back(w_m);
graph[w_m->ID].push_back(w_n);
graph[w_m->ID].push_back(w_o);
}
void createEnemies() {
for (int i = 0; i < 5; i++)
{
enemies.push_back(new billboard());
}
enemies[0]->position = XMFLOAT3(-5, 0, 8); //front A
enemies[0]->activation = ACTIVE;
enemies[0]->cooldown = 0;
enemies[1]->position = XMFLOAT3(-10, 0, 32); //back B
enemies[1]->activation = ACTIVE;
enemies[1]->cooldown = 0;
enemies[2]->position = XMFLOAT3(-6, 0, 18); //middle left C
enemies[2]->activation = ACTIVE;
enemies[2]->cooldown = 0;
enemies[3]->position = XMFLOAT3(3, 0, 14); //middle right D
enemies[3]->activation = ACTIVE;
enemies[3]->activation = ACTIVE;
enemies[4]->position = XMFLOAT3(16, 0, 12); //far left E
enemies[4]->activation = ACTIVE;
enemies[4]->cooldown = 0;
}
bool active(enemyName enName) {
if (enName == A) {
if (enemies[0]->activation == ACTIVE)
return true;
}
else if (enName == B) {
if (enemies[1]->activation == ACTIVE)
return true;
}
else if (enName == C) {
if (enemies[2]->activation == ACTIVE)
return true;
}
else if (enName == D) {
if (enemies[3]->activation == ACTIVE)
return true;
}
else if (enName == E) {
if (enemies[4]->activation == ACTIVE)
return true;
}
else {
return false;
}
}
XMFLOAT3 getNextWaypoint(enemyName enName) {
XMFLOAT3 wayPoint;
if (enName == A) {
wayPoint = graph[0][nextA]->position;
nextA++;
if (nextA > 2) {
nextA = 2;
backwardsA = true;
}
}
else if (enName == B) {
wayPoint = graph[3][nextB]->position;
nextB++;
if (nextB > 2) {
nextB = 2;
backwardsB = true;
}
}
else if (enName == C) {
wayPoint = graph[6][nextC]->position;
nextC++;
if (nextC > 2) {
nextC = 2;
backwardsC = true;
}
}
else if (enName == D) {
wayPoint = graph[9][nextD]->position;
nextD++;
if (nextD > 2) {
nextD = 2;
backwardsD = true;
}
}
else if (enName == E) {
wayPoint = graph[12][nextE]->position;
nextE++;
if (nextE > 2) {
nextE = 2;
backwardsE = true;
}
}
return wayPoint;
}
XMFLOAT3 getBeforeWaypoint(enemyName enName) {
XMFLOAT3 wayPoint;
if (enName == A) {
nextA--;
wayPoint = graph[0][nextA]->position;
if (nextA < 1) {
nextA = 1;
backwardsA = false;
}
}
else if (enName == B) {
nextB--;
wayPoint = graph[3][nextB]->position;
if (nextB < 1) {
nextB = 1;
backwardsB = false;
}
}
else if (enName == C) {
nextC--;
wayPoint = graph[6][nextC]->position;
if (nextC < 1) {
nextC = 1;
backwardsC = false;
}
}
else if (enName == D) {
nextD--;
wayPoint = graph[9][nextD]->position;
if (nextD < 1) {
nextD = 1;
backwardsD = false;
}
}
else if (enName == E) {
nextE--;
wayPoint = graph[12][nextE]->position;
if (nextE < 1) {
nextE = 1;
backwardsE = false;
}
}
return wayPoint;
}
void preWalking(enemyName enName) {
if (enName == A) {
if (!backwardsA) {
startA = getNextWaypoint(A);
endA = getNextWaypoint(A);
}
}
else if (enName == B) {
if (!backwardsB) {
startB = getNextWaypoint(B);
endB = getNextWaypoint(B);
}
}
else if (enName == C) {
if (!backwardsC) {
startC = getNextWaypoint(C);
endC = getNextWaypoint(C);
}
}
else if (enName == D) {
if (!backwardsD) {
startD = getNextWaypoint(D);
endD = getNextWaypoint(D);
}
}
else if (enName == E) {
if (!backwardsE) {
startE = getNextWaypoint(E);
endE = getNextWaypoint(E);
}
}
}
void startWalking(enemyName enName) {
bool tempBack;
XMFLOAT3 start, end1;
if (enName == A) {
tempBack = backwardsA;
index = 0;
start = startA;
end1 = endA;
}
else if (enName == B) {
tempBack = backwardsB;
index = 1;
start = startB;
end1 = endB;
}
else if (enName == C) {
tempBack = backwardsC;
index = 2;
start = startC;
end1 = endC;
}
else if (enName == D) {
tempBack = backwardsD;
index = 3;
start = startD;
end1 = endD;
}
else if (enName == E) {
tempBack = backwardsE;
index = 4;
start = startE;
end1 = endE;
}
speeding = 2;
elapsed2 = 0.01f;
distance1 = sqrt(pow(end1.x - start.x, 2) + pow(end1.y - start.y, 2) + pow(end1.z - start.z, 2));
direction = XMFLOAT3((end1.x - start.x) / distance1, (end1.y - start.y) / distance1, (end1.z - start.z) / distance1);
enemies[index]->position.x += direction.x * speeding * elapsed2;
enemies[index]->position.y += direction.y * speeding * elapsed2;
enemies[index]->position.z += direction.z * speeding * elapsed2;
float currDistance = sqrt(pow(enemies[index]->position.x - start.x, 2) + pow(enemies[index]->position.y - start.y, 2) + pow(enemies[index]->position.z - start.z, 2));
if (currDistance >= distance1) {
if (idleTime >= 0.0 && idleTime <= 3.0) {
enemies[index]->position.x = end1.x;
enemies[index]->position.y = end1.y;
enemies[index]->position.z = end1.z;
idleTime += 0.01;
}
else {
idleTime = 0.0;
if (!tempBack) {
if (enName == A) {
startA = end1;
endA = getNextWaypoint(enName);
}
else if (enName == B) {
startB = end1;
endB = getNextWaypoint(enName);
}
else if (enName == C) {
startC = end1;
endC = getNextWaypoint(enName);
}
else if (enName == D) {
startD = end1;
endD = getNextWaypoint(enName);
}
else if (enName == E) {
startE = end1;
endE = getNextWaypoint(enName);
}
}
else {
if (enName == A) {
startA = end1;
endA = getBeforeWaypoint(enName);
}
else if (enName == B) {
startB = end1;
endB = getBeforeWaypoint(enName);
}
else if (enName == C) {
startC = end1;
endC = getBeforeWaypoint(enName);
}
else if (enName == D) {
startD = end1;
endD = getBeforeWaypoint(enName);
}
else if (enName == E) {
startE = end1;
endE = getBeforeWaypoint(enName);
}
}
}
}
}
//----------------------------------------------------------
//--------------------------------------------------------------------------------------
// Forward declarations
//--------------------------------------------------------------------------------------
HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow);
HRESULT InitDevice();
void CleanupDevice();
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void Render();
//--------------------------------------------------------------------------------------
// Entry point to the program. Initializes everything and goes into a message processing
// loop. Idle time is used to render the scene.
//--------------------------------------------------------------------------------------
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
if (FAILED(InitWindow(hInstance, nCmdShow)))
return 0;
if (FAILED(InitDevice()))
{
CleanupDevice();
return 0;
}
//-----for enemy movement and collision -ML & AP----------
initGraph();
createEnemies();
if (active(A)) {
preWalking(A); //starts walking if enemy is active
}
if (active(B)) {
preWalking(B);
}
if (active(C)) {
preWalking(C); //starts walking if enemy is active
}
if (active(D)) {
preWalking(D);
}
if (active(E)) {
preWalking(E);
}
//--------------------------------------------------------
// Main message loop
MSG msg = { 0 };
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
}
}
CleanupDevice();
return (int)msg.wParam;
}
//--------------------------------------------------------------------------------------
// Register class and create window
//--------------------------------------------------------------------------------------
HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow)
{
// Register class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TUTORIAL1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"TutorialWindowClass";
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_TUTORIAL1);
if (!RegisterClassEx(&wcex))
return E_FAIL;
// Create window
g_hInst = hInstance;
RECT rc = { 0, 0, 1366, 768 };
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
g_hWnd = CreateWindow(L"TutorialWindowClass", L"Direct3D 11 Tutorial 7", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
NULL);
if (!g_hWnd)
return E_FAIL;
ShowWindow(g_hWnd, nCmdShow);
return S_OK;
}
//--------------------------------------------------------------------------------------
// Helper for compiling shaders with D3DX11
//--------------------------------------------------------------------------------------
HRESULT CompileShaderFromFile(WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut)
{
HRESULT hr = S_OK;
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
// Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
// Setting this flag improves the shader debugging experience, but still allows
// the shaders to be optimized and to run exactly the way they will run in
// the release configuration of this program.
dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif
ID3DBlob* pErrorBlob;
hr = D3DX11CompileFromFile(szFileName, NULL, NULL, szEntryPoint, szShaderModel,
dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL);
if (FAILED(hr))
{
if (pErrorBlob != NULL)
OutputDebugStringA((char*)pErrorBlob->GetBufferPointer());
if (pErrorBlob) pErrorBlob->Release();
return hr;
}
if (pErrorBlob) pErrorBlob->Release();
return S_OK;
}
//--------------------------------------------------------------------------------------
// Create Direct3D device and swap chain
//--------------------------------------------------------------------------------------
HRESULT InitDevice()
{
HRESULT hr = S_OK;
RECT rc;
GetClientRect(g_hWnd, &rc);
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;
UINT createDeviceFlags = 0;
#ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT numDriverTypes = ARRAYSIZE(driverTypes);
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Width = width;
sd.BufferDesc.Height = height;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = g_hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
{
g_driverType = driverTypes[driverTypeIndex];
hr = D3D11CreateDeviceAndSwapChain(NULL, g_driverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext);
if (SUCCEEDED(hr))
break;
}
if (FAILED(hr))
return hr;
// Create a render target view
ID3D11Texture2D* pBackBuffer = NULL;
hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
if (FAILED(hr))
return hr;
hr = g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);
pBackBuffer->Release();
if (FAILED(hr))
return hr;
// Create depth stencil texture
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_R32_TYPELESS;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
hr = g_pd3dDevice->CreateTexture2D(&descDepth, NULL, &g_pDepthStencil);
if (FAILED(hr))
return hr;
// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory(&descDSV, sizeof(descDSV));
descDSV.Format = DXGI_FORMAT_D32_FLOAT;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = g_pd3dDevice->CreateDepthStencilView(g_pDepthStencil, &descDSV, &g_pDepthStencilView);
if (FAILED(hr))
return hr;
// Setup the viewport
D3D11_VIEWPORT vp;
vp.Width = (FLOAT)width;
vp.Height = (FLOAT)height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
g_pImmediateContext->RSSetViewports(1, &vp);
// Compile the vertex shader
ID3DBlob* pVSBlob = NULL;
hr = CompileShaderFromFile(L"shader.fx", "VS", "vs_4_0", &pVSBlob);
if (FAILED(hr))
{
MessageBox(NULL,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}
// Create the vertex shader
hr = g_pd3dDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &g_pVertexShader);
if (FAILED(hr))
{
pVSBlob->Release();
return hr;
}
// Define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = ARRAYSIZE(layout);
// Create the input layout
hr = g_pd3dDevice->CreateInputLayout(layout, numElements, pVSBlob->GetBufferPointer(),
pVSBlob->GetBufferSize(), &g_pVertexLayout);
pVSBlob->Release();
if (FAILED(hr))
return hr;
// Set the input layout
g_pImmediateContext->IASetInputLayout(g_pVertexLayout);
// Compile the pixel shader
ID3DBlob* pPSBlob = NULL;
hr = CompileShaderFromFile(L"shader.fx", "PS", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(NULL,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}
// Create the pixel shader
hr = g_pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShader);
pPSBlob->Release();
if (FAILED(hr))
return hr;
//================================================== compile PS_frame shader -ML ===============================================================//
pPSBlob = NULL;
hr = CompileShaderFromFile(L"shader.fx", "PS_frame", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(NULL,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}
// Create the pixel shader named PS_frame
hr = g_pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShader_frame);
pPSBlob->Release();
if (FAILED(hr))
return hr;
//================================================================================================================================================//
//================================================== compile PS_enemies shader -SH ===============================================================//
pPSBlob = NULL;
hr = CompileShaderFromFile(L"shader.fx", "PS_enemies", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(NULL,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}
// Create the pixel shader named PS_frame
hr = g_pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShader_enemies);
pPSBlob->Release();
if (FAILED(hr))
return hr;
//================================================================================================================================================//
//================================================== compile PS_bullet shader -SH ===============================================================//
pPSBlob = NULL;
hr = CompileShaderFromFile(L"shader.fx", "PS_bullets", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(NULL,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}
// Create the pixel shader named PS_frame
hr = g_pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShader_bullets);
pPSBlob->Release();
if (FAILED(hr))
return hr;
//================================================================================================================================================//
//create skybox vertex buffer
SimpleVertex vertices_skybox[] =
{
//top
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.25f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(0.5f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(0.5f, 0.33f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.25f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(0.5f, 0.33f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.25f, 0.33f) },
//bottom
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(0.5f, 0.66f) },
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.25f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(0.5f, 0.66f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(0.5f, 0.66f) },
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.25f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.25f, 0.66f) },
//left
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.25f, 0.33f) },
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.25f, 0.66f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.25f, 0.66f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.25f, 0.33f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.25f, 0.66f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.25f, 0.33f) },
//right
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },
//back
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.0f, 1.0f) },
//front
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },
};
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(SimpleVertex) * 36;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = vertices_skybox;
hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pVertexBuffer_sky);
if (FAILED(hr))
return hr;
// Create vertex buffer
SimpleVertex vertices[] =
{
{ XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT2(1.0f, 0.0f) }
};
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(SimpleVertex) * 12;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = vertices;
hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pVertexBuffer);
if (FAILED(hr))
return hr;
// Set vertex buffer
UINT stride = sizeof(SimpleVertex);
UINT offset = 0;
g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);
// Set primitive topology
g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Create the constant buffers
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(ConstantBuffer);
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = 0;
hr = g_pd3dDevice->CreateBuffer(&bd, NULL, &g_pCBuffer);
if (FAILED(hr))
return hr;
// Load the Texture
hr = D3DX11CreateShaderResourceViewFromFile(g_pd3dDevice, L"skb3.jpg", NULL, NULL, &g_pTextureRV, NULL);
if (FAILED(hr))
return hr;
// Load the Texture