📄 graphics.cpp
字号:
return TRUE;
}
//+-----------------------------------------------------------------------------
//| Actions performed when the device is lost
//+-----------------------------------------------------------------------------
BOOL GRAPHICS::OnLostDevice()
{
REFERENCE<GRAPHICS_WINDOW*, GRAPHICS*>* CurrentReference;
REFERENCE<GRAPHICS_WINDOW*, GRAPHICS*>* NextReference;
Font.OnLostDevice();
if(Sprite) Sprite->OnLostDevice();
CurrentReference = GraphicsWindowReferenceObject.GetFirstReference();
while(CurrentReference != NULL)
{
NextReference = GraphicsWindowReferenceObject.GetNextReference(CurrentReference);
CurrentReference->GetData()->Release();
CurrentReference = NextReference;
}
return TRUE;
}
//+-----------------------------------------------------------------------------
//| Actions performed when the device is reset
//+-----------------------------------------------------------------------------
BOOL GRAPHICS::OnResetDevice()
{
REFERENCE<GRAPHICS_WINDOW*, GRAPHICS*>* CurrentReference;
REFERENCE<GRAPHICS_WINDOW*, GRAPHICS*>* NextReference;
Font.OnResetDevice();
if(Sprite) Sprite->OnResetDevice();
CurrentReference = GraphicsWindowReferenceObject.GetFirstReference();
while(CurrentReference != NULL)
{
NextReference = GraphicsWindowReferenceObject.GetNextReference(CurrentReference);
CurrentReference->GetData()->Acquire();
CurrentReference = NextReference;
}
return TRUE;
}
//+-----------------------------------------------------------------------------
//| Creates a dummy window
//+-----------------------------------------------------------------------------
BOOL GRAPHICS::CreateDummyWindow()
{
Window = CreateWindowEx(0, "BUTTON", "", WS_POPUP,
0, 0, 1, 1,
NULL, NULL, GetModuleHandle(NULL), NULL);
if(Window == NULL)
{
Error.SetMessage("Unable to create a dummy window!");
return FALSE;
}
return TRUE;
}
//+-----------------------------------------------------------------------------
//| Creates a Direct3D object
//+-----------------------------------------------------------------------------
BOOL GRAPHICS::CreateDirect3D()
{
Direct3D = Direct3DCreate9(D3D_SDK_VERSION);
if(Direct3D == NULL)
{
Error.SetMessage("Unable to create a Direct3D object!");
return FALSE;
}
return TRUE;
}
//+-----------------------------------------------------------------------------
//| Checks the device capabilities
//+-----------------------------------------------------------------------------
BOOL GRAPHICS::CheckCapabilities()
{
D3DCAPS9 Capabilities;
D3DDISPLAYMODE DisplayMode;
std::stringstream Stream;
if(!D3DXCheckVersion(D3D_SDK_VERSION, D3DX_SDK_VERSION))
{
Error.SetMessage("Your system does not have the latest DirectX version (DX9 required)!");
return FALSE;
}
if(FAILED(Direct3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &Capabilities)))
{
Error.SetMessage("Unable to retrieve the Direct3D capabilities!");
return FALSE;
}
if(FAILED(Direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode)))
{
Error.SetMessage("Unable to retrieve the Direct3D display mode!");
return FALSE;
}
if(!(Capabilities.DeviceType & D3DDEVTYPE_HAL))
{
Error.SetMessage("Your system does not support HAL (Hardware Abstraction Layer)!");
return FALSE;
}
Properties.MakeShadersAvailable(TRUE);
if(Capabilities.VertexShaderVersion < D3DVS_VERSION(VERTEX_SHADER_MAJOR_VERSION, VERTEX_SHADER_MINOR_VERSION))
{
Properties.MakeShadersAvailable(FALSE);
}
if(Capabilities.PixelShaderVersion < D3DPS_VERSION(PIXEL_SHADER_MAJOR_VERSION, PIXEL_SHADER_MINOR_VERSION))
{
Properties.MakeShadersAvailable(FALSE);
}
if(!(Capabilities.TextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR))
{
Error.SetMessage("Your system does not support linear magnifying filters!");
return FALSE;
}
if(!(Capabilities.TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR))
{
Error.SetMessage("Your system does not support linear minifying filters!");
return FALSE;
}
if(!(Capabilities.TextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR))
{
Error.SetMessage("Your system does not support linear mip-map filters!");
return FALSE;
}
if(!(Capabilities.PrimitiveMiscCaps & D3DPMISCCAPS_CULLNONE))
{
Error.SetMessage("Your system does not support no culling!");
return FALSE;
}
if(!(Capabilities.PrimitiveMiscCaps & D3DPMISCCAPS_CULLCW))
{
Error.SetMessage("Your system does not support clockwise culling!");
return FALSE;
}
if(!(Capabilities.PrimitiveMiscCaps & D3DPMISCCAPS_CULLCCW))
{
Error.SetMessage("Your system does not support counter clockwise culling!");
return FALSE;
}
GraphicsInfo.DeviceInfo.BackBufferFormat = D3DFMT_X8R8G8B8;
if(FAILED(Direct3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, GraphicsInfo.DeviceInfo.BackBufferFormat, TRUE)))
{
GraphicsInfo.DeviceInfo.BackBufferFormat = D3DFMT_X1R5G5B5;
if(FAILED(Direct3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, GraphicsInfo.DeviceInfo.BackBufferFormat, TRUE)))
{
GraphicsInfo.DeviceInfo.BackBufferFormat = D3DFMT_R5G6B5;
if(FAILED(Direct3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, GraphicsInfo.DeviceInfo.BackBufferFormat, TRUE)))
{
Error.SetMessage("Your system does not support back buffers in the X8R8G8B8 format!");
return FALSE;
}
}
}
GraphicsInfo.TextureFormat = D3DFMT_A8R8G8B8;
if(FAILED(Direct3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, 0, D3DRTYPE_TEXTURE, GraphicsInfo.TextureFormat)))
{
GraphicsInfo.TextureFormat = D3DFMT_A4R4G4B4;
if(FAILED(Direct3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, 0, D3DRTYPE_TEXTURE, GraphicsInfo.TextureFormat)))
{
Error.SetMessage("Your system does not support textures in the A8R8G8B8 format!");
return FALSE;
}
}
GraphicsInfo.DeviceInfo.AutoDepthStencilFormat = D3DFMT_D32;
if(FAILED(Direct3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, GraphicsInfo.DeviceInfo.AutoDepthStencilFormat)))
{
GraphicsInfo.DeviceInfo.AutoDepthStencilFormat = D3DFMT_D24X8;
if(FAILED(Direct3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, GraphicsInfo.DeviceInfo.AutoDepthStencilFormat)))
{
GraphicsInfo.DeviceInfo.AutoDepthStencilFormat = D3DFMT_D16;
if(FAILED(Direct3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, GraphicsInfo.DeviceInfo.AutoDepthStencilFormat)))
{
Error.SetMessage("Your system does not support 16-bit depth buffers!");
return FALSE;
}
}
}
return TRUE;
}
//+-----------------------------------------------------------------------------
//| Creates a Direct3D device object
//+-----------------------------------------------------------------------------
BOOL GRAPHICS::CreateDirect3DDevice()
{
GraphicsInfo.DeviceInfo.BackBufferWidth = 0;
GraphicsInfo.DeviceInfo.BackBufferHeight = 0;
GraphicsInfo.DeviceInfo.hDeviceWindow = NULL;
GraphicsInfo.DeviceInfo.Windowed = TRUE;
if(FAILED(Direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&GraphicsInfo.DeviceInfo, &Direct3DDevice)))
{
Error.SetMessage("Unable to create a Direct3D device!");
return FALSE;
}
return TRUE;
}
//+-----------------------------------------------------------------------------
//| Creates all objects
//+-----------------------------------------------------------------------------
BOOL GRAPHICS::CreateObjects()
{
PARTICLE_VERTEX* ParticleVertexPointer;
GROUND_VERTEX* GroundVertexPointer;
if(FAILED(D3DXCreateSprite(Direct3DDevice, &Sprite)))
{
Error.SetMessage("Unable to create a Direct3D sprite object!");
return FALSE;
}
if(FAILED(Direct3DDevice->CreateVertexBuffer(sizeof(LINE_VERTEX) * 2, 0, LINE_VERTEX::FORMAT, D3DPOOL_MANAGED, &LineVertexBuffer, NULL)))
{
Error.SetMessage("Unable to create a line vertex buffer!");
return FALSE;
}
if(FAILED(Direct3DDevice->CreateVertexBuffer(sizeof(PARTICLE_VERTEX) * 4, 0, PARTICLE_VERTEX::FORMAT, D3DPOOL_MANAGED, &ParticleVertexBuffer, NULL)))
{
Error.SetMessage("Unable to create a particle vertex buffer!");
return FALSE;
}
if(FAILED(ParticleVertexBuffer->Lock(0, 0, reinterpret_cast<VOID**>(&ParticleVertexPointer), 0))) return FALSE;
ParticleVertexPointer[0].Position = D3DXVECTOR3(0.0f, -1.0f, 1.0f);
ParticleVertexPointer[0].Normal = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
ParticleVertexPointer[0].Color = 0xFFFFFFFF;
ParticleVertexPointer[0].TexturePosition = D3DXVECTOR2(0.0f, 0.0f);
ParticleVertexPointer[1].Position = D3DXVECTOR3(0.0f, -1.0f, -1.0f);
ParticleVertexPointer[1].Normal = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
ParticleVertexPointer[1].Color = 0xFFFFFFFF;
ParticleVertexPointer[1].TexturePosition = D3DXVECTOR2(0.0f, 1.0f);
ParticleVertexPointer[2].Position = D3DXVECTOR3(0.0f, 1.0f, 1.0f);
ParticleVertexPointer[2].Normal = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
ParticleVertexPointer[2].Color = 0xFFFFFFFF;
ParticleVertexPointer[2].TexturePosition = D3DXVECTOR2(1.0f, 0.0f);
ParticleVertexPointer[3].Position = D3DXVECTOR3(0.0f, 1.0f, -1.0f);
ParticleVertexPointer[3].Normal = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
ParticleVertexPointer[3].Color = 0xFFFFFFFF;
ParticleVertexPointer[3].TexturePosition = D3DXVECTOR2(1.0f, 1.0f);
ParticleVertexBuffer->Unlock();
if(FAILED(Direct3DDevice->CreateVertexBuffer(sizeof(GROUND_VERTEX) * 4, 0, GROUND_VERTEX::FORMAT, D3DPOOL_MANAGED, &GroundVertexBuffer, NULL)))
{
Error.SetMessage("Unable to create a ground vertex buffer!");
return FALSE;
}
if(FAILED(GroundVertexBuffer->Lock(0, 0, reinterpret_cast<VOID**>(&GroundVertexPointer), 0))) return FALSE;
GroundVertexPointer[0].Position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
GroundVertexPointer[0].Normal = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
GroundVertexPointer[0].Color = 0xFFFFFFFF;
GroundVertexPointer[0].TexturePosition = D3DXVECTOR2(0.0f, 0.0f);
GroundVertexPointer[1].Position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
GroundVertexPointer[1].Normal = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
GroundVertexPointer[1].Color = 0xFFFFFFFF;
GroundVertexPointer[1].TexturePosition = D3DXVECTOR2(0.0f, 1.0f);
GroundVertexPointer[2].Position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
GroundVertexPointer[2].Normal = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
GroundVertexPointer[2].Color = 0xFFFFFFFF;
GroundVertexPointer[2].TexturePosition = D3DXVECTOR2(1.0f, 0.0f);
GroundVertexPointer[3].Position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);
GroundVertexPointer[3].Normal = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
GroundVertexPointer[3].Color = 0xFFFFFFFF;
GroundVertexPointer[3].TexturePosition = D3DXVECTOR2(1.0f, 1.0f);
GroundVertexBuffer->Unlock();
return TRUE;
}
//+-----------------------------------------------------------------------------
//| Sets the render states
//+-----------------------------------------------------------------------------
VOID GRAPHICS::SetRenderStates()
{
Direct3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
Direct3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
Direct3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
Direct3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
Direct3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
Direct3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Direct3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
Direct3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
Direct3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Direct3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Direct3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
}
//+-----------------------------------------------------------------------------
//| Sets the projection matrix
//+-----------------------------------------------------------------------------
VOID GRAPHICS::SetProjection()
{
D3DXMatrixPerspectiveFovRH(&ProjectionMatrix, FieldOfView, ScreenAspect, NearDistance, FarDistance);
Direct3DDevice->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -