forked from abartlett139/Omni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.cpp
259 lines (209 loc) · 6.6 KB
/
Graphics.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
#include "Graphics.h"
#include "GameWorld.h"
#include "UIWrappers.h"
#include "MainMenu.h"
#include "Input.h"
D3DMATERIAL9 D3D::InitMtrl( D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p )
{
D3DMATERIAL9 mtrl;
mtrl.Ambient = a;
mtrl.Diffuse = d;
mtrl.Specular = s;
mtrl.Emissive = e;
mtrl.Power = p;
return mtrl;
}
D3DLIGHT9 D3D::InitDirectionalLight( D3DXVECTOR3 * direction, D3DXCOLOR * color )
{
D3DLIGHT9 light;
ZeroMemory( &light, sizeof( light ) );
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = *color;
light.Diffuse = *color;
light.Specular = *color;
light.Direction = *direction;
return light;
}
D3DLIGHT9 D3D::InitSpotLight( D3DXVECTOR3 * position, D3DXVECTOR3 * direction, D3DXCOLOR * color )
{
D3DLIGHT9 light;
ZeroMemory( &light, sizeof( D3DLIGHT9 ) );
light.Type = D3DLIGHT_SPOT;
light.Diffuse = *color;
light.Specular = *color;
light.Position = *position;
light.Direction = *direction;
light.Range = 1000.0f;
light.Attenuation0 = 0.0f;
light.Attenuation1 = 0.125f;
light.Attenuation2 = 0.0f;
light.Phi = D3DXToRadian( 40.0f );
light.Theta = D3DXToRadian( 20.0f );
light.Falloff = 1.0f;
return light;
}
IDirect3DTexture9 * D3D::LoadTexture(char * fileName)
{
D3DXIMAGE_INFO imageInfo;
IDirect3DTexture9* texture = NULL;
D3DXGetImageInfoFromFile(fileName, &imageInfo);
D3DXCreateTextureFromFileEx(Device, fileName, imageInfo.Width, imageInfo.Height, 0, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_FILTER_POINT, D3DX_FILTER_POINT, 0, &imageInfo, NULL, &texture);
return texture;
}
D3DLIGHT9 D3D::InitPointLight( D3DXVECTOR3 * position, D3DXCOLOR * color )
{
D3DLIGHT9 light;
ZeroMemory( &light, sizeof( D3DLIGHT9 ) );
light.Type = D3DLIGHT_POINT;
light.Ambient = *color * 0.5f;
light.Diffuse = *color * 0.5f;
light.Specular = *color;
light.Position = *position;
light.Range = 100.0f;
light.Attenuation0 = 0.0f;
light.Attenuation1 = 0.125f;
light.Attenuation2 = 0.0f;
return light;
}
float D3D::Lerp( float a, float b, float t )
{
return a - (a*t) + (b*t);
}
DWORD D3D::FtoDw( float f )
{
return *((DWORD*)&f);
}
bool Graphics::Render( )
{
m_CurrentState->Render( );
return true;
}
void Graphics::Update( )
{
m_CurrentState->Update( );
}
Graphics::Graphics( )
{
m_MainMenu = NULL, m_GameWorld = NULL, m_CurrentState = NULL, m_PreviousState = NULL;
m_D3DInterface = NULL;
tex = NULL;
}
Graphics::~Graphics( )
{
Shutdown( );
}
bool Graphics::Initialized( int height, int width, HINSTANCE hInstance )
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)D3D::MsgProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor( 0, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = 0;
wc.lpszClassName = "D3DApp";
if( !RegisterClass( &wc ) )
return false;
HWND hWnd = CreateWindow( "D3DApp", "Omni", WS_OVERLAPPEDWINDOW | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX, 100, 100, width, height, 0, 0, hInstance, 0 );
if( !hWnd )
return false;
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
HRESULT hr = 0;
D3DCAPS9 caps;
m_D3DInterface = Direct3DCreate9( D3D_SDK_VERSION );
m_D3DInterface->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps );
if( !m_D3DInterface )
return false;
int vp = 0;
if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
ZeroMemory( &d3dpp, sizeof( d3dpp ) ); //clear structure for use
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.hDeviceWindow = hWnd;
d3dpp.Windowed = TRUE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
hr = m_D3DInterface->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, vp, &d3dpp, &Device );
if( FAILED( hr ) )
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
hr = m_D3DInterface->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, vp, &d3dpp, &Device );
if( FAILED( hr ) )
{
m_D3DInterface->Release( );
return false;
}
}
m_MainMenu = new MainMenu();
m_GameWorld = new GameWorld();
m_Story = new Story();
m_Credits = new Credits();
m_Options = new Options();
//m_Story->Init();
//m_GameWorld->Init( );
//m_MainMenu->Init( );
m_CurrentState = m_MainMenu;
m_MainMenu->Enter();
//load image iformation
D3DXIMAGE_INFO ImageInfo;
D3DXGetImageInfoFromFile( "textures/cursor.png", &ImageInfo );
Device->CreateOffscreenPlainSurface( ImageInfo.Height, ImageInfo.Width, ImageInfo.Format, D3DPOOL_DEFAULT, &m_Cursor, NULL );
D3DXLoadSurfaceFromFile( m_Cursor, NULL, NULL, "textures/cursor.png", NULL, D3DX_FILTER_NONE, D3DCOLOR_XRGB( 255, 255, 255 ), &ImageInfo );
Device->SetCursorProperties( 0, 0, m_Cursor );
Device->SetCursorPosition( 0, 0, D3DCURSOR_IMMEDIATE_UPDATE );
return TRUE;
}
void Graphics::Shutdown( )
{
/*if( Device )
{
Device->Release( );
Device = 0;
}
if( tex )
delete tex;
if( sprt )
delete sprt;
if( m_MainMenu )
delete m_MainMenu;
if( m_GameWorld )
delete m_GameWorld;*/
D3D::Release(Device);
/*D3D::Delete(tex);
D3D::Delete(sprt);
D3D::Delete(m_MainMenu);
D3D::Delete(m_Options);
D3D::Delete(m_Credits);
D3D::Delete(m_Story);
D3D::Delete(m_PreviousState);
D3D::Delete(m_CurrentState);
D3D::Delete(m_GameWorld);*/
}
//this is basically the update function for the graphics class, it passes on message data to the State Machine Update functions
void Graphics::RecvMessages( UINT msg, WPARAM wParam, LPARAM lParam, void * Data )
{
m_CurrentState->ProcessMessages( msg, wParam, lParam, Data );
}
//changes the h/w of the screen when the screen is maximized or returned to windows mode
void Graphics::SetScreenRect( )
{
m_ScreneRect.top = 0;
m_ScreneRect.left = 0;
m_ScreneRect.bottom = d3dpp.BackBufferHeight;
m_ScreneRect.right = d3dpp.BackBufferWidth;
}