-
Notifications
You must be signed in to change notification settings - Fork 0
/
WWindow.cpp
executable file
·168 lines (140 loc) · 4.32 KB
/
WWindow.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
#include "WWindow.h"
//---------------------------------------------------------------------------
WWindow::WWindow()
{
// If we declare a window class with a default constructor,
// we need to reset the window to a nothing
handle = NULL;
}
//---------------------------------------------------------------------------
HWND WWindow::Create(HINSTANCE hinst,
LPCTSTR clsname,
LPCTSTR wndname,
HWND parent,
DWORD dStyle,
DWORD dXStyle,
int x,
int y,
int width,
int height)
{
// When call the Create() method, we can use it to create a new window
handle = CreateWindowEx(dXStyle, clsname, wndname, dStyle, x, y, width,
height, parent, NULL, hinst, NULL);
// We hope everything went alright and the window was created
if( handle != NULL )
{
setHandle(handle);
return handle;
}
// If something went wrong, for example if the window could not
// be created, return a "nothing" window
return NULL;
}
//---------------------------------------------------------------------------
BOOL WWindow::Show(int dCmdShow)
{
// We will display the main window as a regular object and update it
if( ShowWindow(handle, dCmdShow) && UpdateWindow(handle) )
return TRUE;
return FALSE;
}
//---------------------------------------------------------------------------
/********************************************************************************
* Initialise Direct3D
********************************************************************************/
HRESULT WWindow::InitialiseD3D(HWND hWnd)
{
/* Create main Direct3D object */
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_pD3D == NULL)
{
return E_FAIL;
}
/*** Set Window handle ***/
handle = hWnd;
/* Get the current display mode */
D3DDISPLAYMODE d3ddm;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
//Create a structure to hold the settings for our device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
/* Fill the structure.
* We want our program to be windowed, and set the back buffer to a format
* that matches our current display mode
*/
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY; //_VSYNC;
d3dpp.BackBufferFormat = d3ddm.Format;
//set global variables about size of the window
// Create a Direct3D device.
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, handle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
{
return E_FAIL;
}
screenWidth = d3dpp.BackBufferWidth;
screenHeight = d3dpp.BackBufferHeight;
//Enable alpha blending so we can use transparent textures
if(FAILED(g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)))
{
return E_FAIL;
}
else
{
//Set how the texture should be blended (use alpha)
g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
}
// Turn on back face culling. This is becuase we want to hide the back of our polygons
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); //D3DCULL_NONE
// Turn off lighting becuase we are specifying that our vertices have colour
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
return S_OK;
}
/*******************************************************
* Clean up after exit
*******************************************************/
void WWindow::CleanUp()
{
//Clean up D3D objects
if(g_pD3DDevice != NULL)
{
g_pD3DDevice->Release();
g_pD3DDevice = NULL;
}
if(g_pD3D != NULL)
{
g_pD3D->Release();
g_pD3D = NULL;
}
SafeRelease(g_pD3DDevice);
SafeRelease(g_pD3D);
}
HWND WWindow::getHandle()
{
return handle;
}
void WWindow::setHandle(HWND newHandle)
{
handle = newHandle;
}
int WWindow::getScreenWidth()
{
return screenWidth;
}
int WWindow::getScreenHeight()
{
return screenHeight;
}
void WWindow::setScreenWidth(int newWidth)
{
screenWidth = newWidth;
}
void WWindow::setScreenHeight(int newHeight)
{
screenHeight = newHeight;
}