Compare commits

..
3 Commits
Author SHA1 Message Date
david 17b18859b3 Add vertex
Add triangle & quad examples
2026-11-09 12:16:43 +01:00
david 0d55e6be6d Fix ShowMessage 2026-11-09 12:16:26 +01:00
david 24dbe2dd58 Add D3D connection 2026-11-09 12:16:14 +01:00
7 changed files with 140 additions and 4 deletions
+1
View File
@@ -194,6 +194,7 @@
<ClInclude Include="Mesh.h" /> <ClInclude Include="Mesh.h" />
<ClInclude Include="Time.h" /> <ClInclude Include="Time.h" />
<ClInclude Include="Utilities.h" /> <ClInclude Include="Utilities.h" />
<ClInclude Include="Vertex.h" />
<ClInclude Include="Window.h" /> <ClInclude Include="Window.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+3
View File
@@ -68,5 +68,8 @@
<ClInclude Include="Window.h"> <ClInclude Include="Window.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Vertex.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
+37
View File
@@ -1,28 +1,65 @@
#include "Direct3D.h" #include "Direct3D.h"
#include "Utilities.h"
INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen) INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
{ {
HRESULT hr = {};
// 1. create connection to Direct3D API // 1. create connection to Direct3D API
IDirect3D9* pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D == nullptr) return 200;
// 2. check compatibility // 2. check compatibility
D3DCAPS9 caps = {};
UINT adapter = 0; // graphic card index - 0 means primary adapter
D3DDEVTYPE devType = D3DDEVTYPE_HAL; // hardware or software renderer, here hardware
hr = pD3D->GetDeviceCaps(adapter, devType, &caps);
CheckFailed(hr, 210);
DWORD vertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING; // fallback if hardware processing is not available
if (caps.VertexProcessingCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING; // hardware processing is available
// 3. define presentation parameters // 3. define presentation parameters
D3DPRESENT_PARAMETERS pp = {};
pp.hDeviceWindow = hWnd; // target window
pp.Windowed = !isFullscreen; // Window mode yes or no
pp.BackBufferCount = 1;
pp.BackBufferWidth = width;
pp.BackBufferHeight = height;
pp.BackBufferFormat = D3DFMT_A8R8G8B8;
pp.SwapEffect = D3DSWAPEFFECT_DISCARD; // what happens to the front buffer after swap
// 4. create Direct3D device // 4. create Direct3D device
hr = pD3D->CreateDevice(adapter, devType, hWnd, vertexProcessing, &pp, &_pD3DDevice);
CheckFailed(hr, 220);
// 5. tidy up // 5. tidy up
safeRelease(pD3D);
return 0; return 0;
} }
void Direct3D::beginScene() void Direct3D::beginScene()
{ {
_pD3DDevice->BeginScene();
// clear buffers
_pD3DDevice->Clear(
0, nullptr, // only clear specific buffer regions, here clear all
D3DCLEAR_TARGET, // which buffers to clear
D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0xffffffff // clear values
);
} }
void Direct3D::endScene() void Direct3D::endScene()
{ {
_pD3DDevice->EndScene();
_pD3DDevice->Present(nullptr, nullptr, nullptr, nullptr);
} }
void Direct3D::deInit() void Direct3D::deInit()
{ {
safeRelease(_pD3DDevice);
} }
+78
View File
@@ -1,5 +1,6 @@
#include "Mesh.h" #include "Mesh.h"
#include "Utilities.h" #include "Utilities.h"
#include "Vertex.h"
UINT Mesh::init(IDirect3DDevice9* pD3DDevice) UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
{ {
@@ -20,6 +21,19 @@ void Mesh::update(FLOAT dt)
void Mesh::render(IDirect3DDevice9* pD3DDevice) void Mesh::render(IDirect3DDevice9* pD3DDevice)
{ {
// setup pipeline
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
pD3DDevice->SetIndices(_pIndexBuffer);
pD3DDevice->SetFVF(FVF);
// draw call
pD3DDevice->DrawIndexedPrimitive(
D3DPT_TRIANGLELIST, // primitive type
0, 0, // vertex buffer offsets
_vertexCount,
0, // index buffer offset
_indexCount / 3 // primitive count
);
} }
void Mesh::deInit() void Mesh::deInit()
@@ -30,10 +44,74 @@ void Mesh::deInit()
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice) UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
{ {
// specify values
_vertexCount = 4;
_vertexStride = sizeof(Vertex);
// describe & create vertex buffer
HRESULT hr = pD3DDevice->CreateVertexBuffer(
_vertexCount * _vertexStride, // total content size
D3DUSAGE_WRITEONLY, // read and/or write access
FVF, // FVF - Flexible Vertex Format, how the vertex is structured
D3DPOOL_MANAGED, // buffer memory management
&_pVertexBuffer,
nullptr // deprecated, so always nullptr
);
CheckFailed(hr, 300);
// populate vertex buffer
Vertex* pVertices = nullptr;
hr = _pVertexBuffer->Lock(
0, 0, // lock a specific amount of the buffer, here everything
reinterpret_cast<void**>(&pVertices),
0 // optional flags
);
CheckFailed(310, hr);
// triangle
//pVertices[0] = Vertex(0.0f, 1.0f, 0.0f);
//pVertices[1] = Vertex(1.0f, -1.0f, 0.0f);
//pVertices[2] = Vertex(-1.0f, -1.0f, 0.0f);
// quad
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f);
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f);
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f);
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f);
hr = _pVertexBuffer->Unlock();
CheckFailed(hr, 320);
pVertices = nullptr;
return 0; return 0;
} }
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice) UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
{ {
_indexCount = 6;
HRESULT hr = pD3DDevice->CreateIndexBuffer(_indexCount * sizeof(unsigned short),
D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &_pIndexBuffer, 0);
CheckFailed(hr, 330);
unsigned short* pIndices = nullptr;
hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pIndices), 0);
CheckFailed(hr, 340);
// triangle
//pIndices[0] = 0;
//pIndices[1] = 1;
//pIndices[2] = 2;
// quad
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
hr = _pIndexBuffer->Unlock();
CheckFailed(hr, 350);
pIndices = nullptr;
return 0; return 0;
} }
+4 -2
View File
@@ -5,9 +5,11 @@ void ShowError(UINT error)
#if _DEBUG #if _DEBUG
LPCTSTR msg = TEXT("Application quit with an error: "); LPCTSTR msg = TEXT("Application quit with an error: ");
#if UNICODE #if UNICODE
msg = (std::wstring(msg) + std::to_wstring(error)).c_str(); std::wstring str = std::wstring(msg) + std::to_wstring(error);
msg = str.c_str();
#else // UNICODE #else // UNICODE
msg = (std::string(msg) + std::to_string(error)).c_str(); std::string str = std::wstring(msg) + std::to_wstring(error);
msg = str.c_str();
#endif // UNICODE #endif // UNICODE
MessageBox(nullptr, msg, TEXT("Error"), MB_OK); MessageBox(nullptr, msg, TEXT("Error"), MB_OK);
#endif // DEBUG #endif // DEBUG
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <d3d9.h>
#define FVF D3DFVF_XYZ
struct Vertex
{
// position
float x;
float y;
float z;
Vertex() : x(0.0f), y(0.0f), z(0.0f) {}
Vertex(float x, float y, float z) : x(x), y(y), z(z) {}
};
+2 -2
View File
@@ -16,8 +16,8 @@ int WINAPI WinMain(
) )
{ {
// some constant values // some constant values
const UINT width = 640; const UINT width = 1024;
const UINT height = 480; const UINT height = 768;
const BOOL isFullscreen = FALSE; const BOOL isFullscreen = FALSE;
UINT error = 0; // error code for identifying errors UINT error = 0; // error code for identifying errors