-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
229 lines (194 loc) ยท 5.57 KB
/
Game.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
/*=======================================================================================================
ๆฆใ็ฅ๏ผใฒใผใ ๅ
จไฝใฎๆตใใๆฑใๅฆ็
ไฝๆๆฅ๏ผ
ๆดๆฐๆฅ๏ผ
ๅถไฝ่
๏ผ
=======================================================================================================*/
//---------------ใคใณใฏใซใผใ-----------------------
#include "Game.h"
#include "TitleScene.h"
#include "PlayScene.h"
#include "ClearScene.h"
#include "GameOverScene.h"
#include "Camera.h"
//----------------ใฐใญใผใใซๅคๆฐ----------------
GAME_SCENE g_gameScene;//็พๅจใฎใฒใผใ ใทใผใณ
LPDIRECT3DDEVICE9 g_pDevice; //Direct3Dใใใคในใชใใธใงใฏใ
Input* g_pInput; //ๅ
ฅๅๅฆ็ใชใใธใงใฏใ
Camera* g_pCamera;
//โโโโโโโโโโโโโโโโโโโโโโ
// ใณใณในใใฉใฏใฟ
//โโโโโโโโโโโโโโโโโโโโโโ
Game::Game()
{
//ๅใทใผใณใชใใธใงใฏใใฎ็ๆ
scene[SC_TITLE] = new TitleScene; //ใฟใคใใซใทใผใณ
scene[SC_PLAY] = new PlayScene; //ใใฌใคใทใผใณ
scene[SC_CLEAR] = new ClearScene; //ใฏใชใขใทใผใณ
scene[SC_GAME_OVER] = new GameOverScene;//ใฒใผใ ใชใผใใผใทใผใณ
//ๆๅใฏใใฌใคใทใผใณ
g_gameScene = SC_PLAY;
//ใซใกใฉ
g_pCamera = new Camera;
}
//โโโโโโโโโโโโโโโโโโโโโโ
// ใในใใฉใฏใฟ
//โโโโโโโโโโโโโโโโโโโโโโ
Game::~Game()
{
SAFE_DELETE(g_pCamera);
for (int i = 0; i < SC_MAX; i++)
{
delete scene[i];
}
//Direct3D่งฃๆพ
SAFE_RELEASE(g_pDevice);
SAFE_RELEASE(pD3d);
}
//โโโโโโโโโโโโโโโโโโโโโโ
// Direct3Dๅๆๅ
//โโโโโโโโโโโโโโโโโโโโโโ
HRESULT Game::InitD3d(HWND hWnd)
{
// ใDirect3Dใใชใใธใงใฏใใฎไฝๆ
if (NULL == (pD3d = Direct3DCreate9(D3D_SDK_VERSION)))
{
MessageBox(0, "Direct3Dใฎไฝๆใซๅคฑๆใใพใใ", "", MB_OK);
return E_FAIL;
}
// ใDIRECT3Dใใใคในใใชใใธใงใฏใใฎไฝๆ
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
if (FAILED(pD3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pDevice)))
{
MessageBox(0, "HALใขใผใใงDIRECT3Dใใใคในใไฝๆใงใใพใใ\nREFใขใผใใงๅ่ฉฆ่กใใพใ", NULL, MB_OK);
if (FAILED(pD3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pDevice)))
{
MessageBox(0, "DIRECT3Dใใใคในใฎไฝๆใซๅคฑๆใใพใใ", NULL, MB_OK);
return E_FAIL;
}
}
//if (FAILED(g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE)))
//{
// MessageBox(0, "ใฉใคใใฎ่จญๅฎใซๅคฑๆใใพใใ", "ใจใฉใผ", MB_OK);
// return E_FAIL;
//}
//if (FAILED(g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)))
//{
// MessageBox(0, "ใซใชใณใฐใขใผใใฎ่จญๅฎใซๅคฑๆใใพใใ", "ใจใฉใผ", MB_OK);
// return E_FAIL;
//}
if (FAILED(g_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE) ||
g_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA) ||
g_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)))
{
MessageBox(0, "ใขใซใใกใใฌใณใใฎ่จญๅฎใซๅคฑๆใใพใใ", "ใจใฉใผ", MB_OK);
return E_FAIL;
}
if (FAILED(g_pDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2)))
{
MessageBox(0, "ใใฏในใใฃในใใผใใฎ่จญๅฎใซๅคฑๆใใพใใ", "ใจใฉใผ", MB_OK);
return E_FAIL;
}
if (FAILED(InitLight()))
{
return E_FAIL;
}
return S_OK;
}
//โโโโโโโโโโโโโโโโโโโโโโ
// ่ก็ชๅคๅฎ
//โโโโโโโโโโโโโโโโโโโโโโ
HRESULT Game::Hit()
{
if (FAILED(scene[g_gameScene]->Hit()))
{
return E_FAIL;
}
return S_OK;
}
//โโโโโโโโโโโโโโโโโโโโโโ
// ่ชญใฟ่พผใฟๅฆ็
//โโโโโโโโโโโโโโโโโโโโโโ
HRESULT Game::Load()
{
for (int i = 0; i < SC_MAX; i++)
{
if (FAILED(scene[i]->Load()))
{
return E_FAIL;
}
}
return S_OK;
}
//โโโโโโโโโโโโโโโโโโโโโโ
// ๆดๆฐๅฆ็
//โโโโโโโโโโโโโโโโโโโโโโ
HRESULT Game::Update()
{
if (FAILED(g_pCamera->Update()))
{
return E_FAIL;
}
g_pInput->Update();
if (FAILED(scene[g_gameScene]->Update()))
{
return E_FAIL;
}
return S_OK;
}
//โโโโโโโโโโโโโโโโโโโโโโ
// ๆ็ปๅฆ็
//โโโโโโโโโโโโโโโโโโโโโโ
HRESULT Game::Render()
{
//็ป้ขใใฏใชใข
g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0, 255, 255), 1.0f, 0);
//ๆ็ป้ๅง
if (SUCCEEDED(g_pDevice->BeginScene()))
{
//ใฒใผใ ็ป้ขใฎๆ็ป
if (FAILED(scene[g_gameScene]->Render()))
{
return E_FAIL;
}
//ๆ็ป็ตไบ
g_pDevice->EndScene();
}
//ใใชใใ
g_pDevice->Present(NULL, NULL, NULL, NULL);
return S_OK;
}
HRESULT Game::InitLight()
{
D3DLIGHT9 light;
ZeroMemory(&light, sizeof(D3DLIGHT9));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Direction = D3DXVECTOR3(1.0f, -1.0f, 1.0f);
light.Diffuse.r = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.b = 1.0f;
light.Ambient.r = 0.4f;
light.Ambient.g = 0.4f;
light.Ambient.b = 0.4f;
if (FAILED(g_pDevice->SetLight(0, &light)))
{
MessageBox(0, "ใฉใคใใใปใใใงใใพใใใงใใ", "Game", MB_OK);
return E_FAIL;
}
if (FAILED(g_pDevice->LightEnable(0, TRUE)))
{
MessageBox(0, "ใฉใคใใๆๅนใซใงใใพใใใงใใ", "Game", MB_OK);
return E_FAIL;
}
return S_OK;
}