add direct3D

This commit is contained in:
2025-09-10 15:24:32 +02:00
parent ed7b1816e9
commit 3bda921efb
3 changed files with 58 additions and 2 deletions
+51
View File
@@ -1,28 +1,79 @@
#include "Direct3D.h"
#include "Utilities.h"
#include <random>
INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
{
// 1. create connection to Direct3D API
IDirect3D9* pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D == nullptr) return 200;
// 2. check compatibility
UINT adapter = D3DADAPTER_DEFAULT; // graphic card index
D3DDEVTYPE devType = D3DDEVTYPE_HAL; // hardware type, hardware or software renderer
D3DCAPS9 caps = {};
HRESULT hr = pD3D->GetDeviceCaps(adapter, devType, &caps);
CheckFailed(hr, 210);
// support for fixed functiuon pipeline
DWORD vertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING; // fallback
if (caps.VertexProcessingCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
// hardware support for fixed function pipeline
vertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
// 3. define presentation parameters
D3DPRESENT_PARAMETERS d3dpp = {};
d3dpp.hDeviceWindow = hWnd; // target window to draw into
d3dpp.Windowed = !isFullscreen; // fullscreen window?
d3dpp.BackBufferCount = 1; // at least 1
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; // back buffer pixel format
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // what happen to front buffer after swap
// 4. create Direct3D device
hr = pD3D->CreateDevice(
adapter, // graphic adapter index
devType, // hardware device type
hWnd, // target window handle
vertexProcessing, // optional flags, here important for fixed function pipeline support
&d3dpp, &_pD3DDevice
);
CheckFailed(hr, 220);
// 5. tidy up
safeRelease(pD3D);
return 0;
}
void Direct3D::beginScene()
{
_pD3DDevice->BeginScene(); // draw calls only after this call
// clear back buffer
static std::default_random_engine e;
static std::uniform_int_distribution<UINT> d(0, 255);
_pD3DDevice->Clear(
0, nullptr, // clear specific areas only
D3DCLEAR_TARGET, // what to clear
D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0xFFFFFFFF // how to clear
);
}
void Direct3D::endScene()
{
_pD3DDevice->EndScene(); // draw calls only before this call
// swap front with back buffer
_pD3DDevice->Present(nullptr, nullptr, nullptr, nullptr);
}
void Direct3D::deInit()
{
safeRelease(_pD3DDevice);
}
+2 -1
View File
@@ -14,6 +14,7 @@ public:
IDirect3DDevice9* getDevice() const { return _pD3DDevice; }
private:
IDirect3DDevice9* _pD3DDevice = nullptr; // our interface for api communication
// COM - Component Object Model
IDirect3DDevice9* _pD3DDevice = nullptr; // our interface for graphic card communication
};
+5 -1
View File
@@ -12,17 +12,21 @@ INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
wc.hbrBackground = CreateSolidBrush(RGB(0, 255, 0));
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // technical behaviour
// 2. register window class
if (!RegisterClass(&wc)) return 100;
// 3. adjust window size (optional)
DWORD style = {};
style = WS_OVERLAPPEDWINDOW; // window with border & title
//style = WS_POPUP; // window without any border or title
// 4. create window instance
_hWnd = CreateWindow(
wc.lpszClassName, // class name of window class to be instantiated
wc.lpszClassName, // window title
WS_OVERLAPPEDWINDOW, // visual style
style, // visual style
100, 100, // window position (left-top corner)
width, height, // window size
nullptr, // handle to parent window