#include "Direct3D.h"
Direct3D::Direct3D()
{
m_d3dObject = NULL;
m_d3dDevice = NULL;
}
Direct3D::~Direct3D()
{
Release();
}
bool Direct3D::Initialise(HWND hwnd, bool fullscreen)
{
//This method really just takes the code that we've been using
//all along and places it in an object.
D3DDISPLAYMODE DisplayMode;
// This will allow us to set the parameters of the screen.
D3DPRESENT_PARAMETERS Present_Parameters;
// This is used to get the capabilities of the hardware.
D3DCAPS9 D3DCaps;
// It is always a good idea to clear out memory in object although not necessary.
ZeroMemory(&Present_Parameters, sizeof(Present_Parameters));
// Create the Direct3D object to get everything started.
m_d3dObject = Direct3DCreate9(D3D_SDK_VERSION);
// Error checking. Make sure that it was successful.
if(m_d3dObject == NULL)
{
MessageBox(NULL, "Error, couldn't initialize DirectX!?!",
"Error!", MB_OK);
return false;
}
// This function will get the display mode of the device and place it in DisplayMode.
if(FAILED(m_d3dObject->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode)))
{
MessageBox(NULL, "Error setting the display mode.", "Error!", MB_OK);
return false;
}
// Get the capabilities of the hardware.
if(FAILED(m_d3dObject->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3DCaps)))
return false;
// Test which is supported, hardware or software vertex processing.
DWORD VertexProcessing = 0;
if(D3DCaps.VertexProcessingCaps != 0)
VertexProcessing |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
VertexProcessing |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
// Here we are setting the applications parameters...
if(fullscreen)
{
Present_Parameters.Windowed = FALSE; // Window mode (fullscreen).
Present_Parameters.BackBufferWidth = DisplayMode.Width;
Present_Parameters.BackBufferHeight = DisplayMode.Height;
}
else
{
Present_Parameters.Windowed = TRUE; // Window mode (not fullscreen).
}
Present_Parameters.SwapEffect = D3DSWAPEFFECT_DISCARD; // Dealing with animation (see doc).
Present_Parameters.BackBufferFormat = DisplayMode.Format;// Render to the area of the screen.
Present_Parameters.BackBufferCount = 1; // Number of back buffers.
Present_Parameters.EnableAutoDepthStencil = TRUE; // Check documentation.
Present_Parameters.AutoDepthStencilFormat = D3DFMT_D16; // Check documentation.
// Now we must create the rendering device.
if(FAILED(m_d3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&Present_Parameters, &m_d3dDevice)))
{
MessageBox(NULL, "CreateDevice() failed! Make sure you have DirectX 9.",
"Error!", MB_OK);
return false;
}
// One last check to be sure.
if(m_d3dDevice == NULL)
{
MessageBox(NULL, "m_d3dDevice is equal to NULL!?!", "Error!", MB_OK);
return false;
}
m_d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
m_d3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);// Culling off.
m_d3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE); // Enable depth testing.
D3DXMATRIX View;
D3DXVECTOR3 Eye(0.0f, 10.0f, -10.0f); // Position of view.
D3DXVECTOR3 LookAt(0.0f, 0.0f, 0.0f); // Point view is looking at.
D3DXVECTOR3 Up(0.0f, 1.0f, 0.0f); // Which way is up.
// Set all the values this matrix will need to know (position, look at point, etc).
D3DXMatrixLookAtLH(&View, &Eye, &LookAt, &Up);
// Move the "view" to the position and values we set above.
m_d3dDevice->SetTransform(D3DTS_VIEW, &View);
D3DXMATRIX Projection;
// Set the perspective information.
D3DXMatrixPerspectiveFovLH(&Projection, 45.0f, 640/480, 0.1f, 500.0f);
// Set the projection.
m_d3dDevice->SetTransform(D3DTS_PROJECTION, &Projection);
return true;
}
void Direct3D::Draw( ThirdPersonCamera* cam, std::vector<GameEntity*> entities)
{
//Here we go through our list of entities and call draw.
m_d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
m_d3dDevice->BeginScene();
for(unsigned int i = 0; i < entities.size(); i++)
{
entities[i]->Draw(m_d3dDevice);
}
m_d3dDevice->EndScene();
m_d3dDevice->Present(NULL, NULL, NULL, NULL);
}
void Direct3D::Release()
{
//This method cleans up the Direct3D objects that have been created.
if(m_d3dDevice)
{
m_d3dDevice->Release();
m_d3dDevice = NULL;
}
if(m_d3dObject)
{
m_d3dObject->Release();
m_d3dObject = NULL;
}
} |