121 lines
3.2 KiB
C++
121 lines
3.2 KiB
C++
|
#include "stdafx.h"
|
||
|
|
||
|
// D3D object
|
||
|
LPDIRECT3D8 g_pD3D = NULL;
|
||
|
// D3D device
|
||
|
LPDIRECT3DDEVICE8 g_pd3dDevice = NULL;
|
||
|
// D3D Vertex Buffer
|
||
|
LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL;
|
||
|
|
||
|
struct Vertex
|
||
|
{
|
||
|
FLOAT x, y, z;
|
||
|
D3DCOLOR color;
|
||
|
};
|
||
|
|
||
|
#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
|
||
|
|
||
|
HRESULT InitVertexBuffer()
|
||
|
{
|
||
|
Vertex g_Vertices[] =
|
||
|
{
|
||
|
{-1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0)},
|
||
|
{0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0)},
|
||
|
{1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255)}
|
||
|
};
|
||
|
|
||
|
// Create a Vertex Buffer
|
||
|
HRESULT vertexBufferCreation = g_pd3dDevice->CreateVertexBuffer(3 * sizeof(Vertex),
|
||
|
D3DUSAGE_WRITEONLY, D3DFVF_VERTEX, D3DPOOL_MANAGED, &g_pVB);
|
||
|
if (FAILED(vertexBufferCreation))
|
||
|
return E_FAIL;
|
||
|
|
||
|
|
||
|
Vertex* pVertices;
|
||
|
// Lock the Vertex Buffer and retreive a pointer to it
|
||
|
if (FAILED(g_pVB->Lock(0, 0, (BYTE**)&pVertices, 0)))
|
||
|
return E_FAIL;
|
||
|
// Copy vertices to the GPU
|
||
|
memcpy(pVertices, g_Vertices, 3 * sizeof(Vertex));
|
||
|
// Unlock the Vertex Buffer
|
||
|
g_pVB->Unlock();
|
||
|
|
||
|
return S_OK;
|
||
|
}
|
||
|
|
||
|
HRESULT InitD3D8()
|
||
|
{
|
||
|
// Create D3D8 object
|
||
|
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
|
||
|
if (g_pD3D == (NULL))
|
||
|
return E_FAIL;
|
||
|
|
||
|
// Create D3D device settings
|
||
|
D3DPRESENT_PARAMETERS d3dpp;
|
||
|
ZeroMemory(&d3dpp, sizeof(d3dpp));
|
||
|
// Set the resolution to 480p (640x480).
|
||
|
d3dpp.BackBufferWidth = 640;
|
||
|
d3dpp.BackBufferHeight = 480;
|
||
|
d3dpp.BackBufferFormat = D3DFMT_LIN_X8R8G8B8;
|
||
|
d3dpp.BackBufferCount = 1;
|
||
|
d3dpp.EnableAutoDepthStencil = TRUE;
|
||
|
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
|
||
|
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
||
|
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE_OR_IMMEDIATE;
|
||
|
|
||
|
// Create the D3D8 device with the device settings (present parameters).
|
||
|
HRESULT deviceCreation = g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL,
|
||
|
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice);
|
||
|
if (FAILED(deviceCreation))
|
||
|
return E_FAIL;
|
||
|
|
||
|
// Set the projection matrix
|
||
|
D3DXMATRIX matProj;
|
||
|
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 4.0f/3.0f, 1.0f, 200.0f);
|
||
|
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
|
||
|
|
||
|
// Set the view matrix
|
||
|
D3DXVECTOR3 vEyePt = D3DXVECTOR3(0.0f, 0.0f, -7.0f);
|
||
|
D3DXVECTOR3 vLookatPt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
|
||
|
D3DXVECTOR3 vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
|
||
|
D3DXMATRIX matView;
|
||
|
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUp);
|
||
|
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
|
||
|
|
||
|
return S_OK;
|
||
|
}
|
||
|
|
||
|
VOID Render()
|
||
|
{
|
||
|
DWORD clearFlags = D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL;
|
||
|
D3DCOLOR color = D3DCOLOR_XRGB(0, 0, 0);
|
||
|
g_pd3dDevice->Clear(0L, NULL, clearFlags, color, 1.0f, 0L);
|
||
|
|
||
|
g_pd3dDevice->BeginScene();
|
||
|
|
||
|
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
|
||
|
g_pd3dDevice->SetStreamSource(0, g_pVB, sizeof(Vertex));
|
||
|
g_pd3dDevice->SetVertexShader(D3DFVF_VERTEX);
|
||
|
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
|
||
|
|
||
|
g_pd3dDevice->EndScene();
|
||
|
}
|
||
|
|
||
|
void __cdecl main()
|
||
|
{
|
||
|
// Initialize Direct3D 8.1
|
||
|
if (FAILED(InitD3D8()))
|
||
|
return;
|
||
|
|
||
|
// Initialize the Vertex Buffer
|
||
|
InitVertexBuffer();
|
||
|
|
||
|
while (TRUE)
|
||
|
{
|
||
|
// Render the current scene
|
||
|
Render();
|
||
|
|
||
|
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
|
||
|
}
|
||
|
}
|