Files
hwr2023_b/GDP/D3D9/Direct3D.cpp
T
2025-09-10 10:05:09 +02:00

82 lines
2.1 KiB
C++

#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;
D3DDEVTYPE devType = D3DDEVTYPE_HAL;
D3DCAPS9 caps = {};
HRESULT hr = pD3D->GetDeviceCaps(adapter, devType, &caps);
CheckFailed(hr, 210);
DWORD vertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING; // fallback software fixed function pipeline
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 render window
d3dpp.Windowed = !isFullscreen; // window or fullscreen mode
d3dpp.BackBufferCount = 1; // at least 1
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
// 4. create Direct3D device
hr = pD3D->CreateDevice(
adapter,
devType,
hWnd,
vertexProcessing, // optional parameters
&d3dpp,
&_pD3DDevice
);
CheckFailed(hr, 220);
// 5. tidy up
safeRelease(pD3D);
return 0;
}
void Direct3D::beginScene()
{
_pD3DDevice->BeginScene(); // init a frame, after this call we are allowed to do draw calls
// change background color
static std::default_random_engine e;
static std::uniform_int_distribution<int> d(0, 255);
_pD3DDevice->Clear(
0, nullptr, // areas to clear, null -> everything
D3DCLEAR_TARGET, // what to clear, bitmask
// how to clean
//D3DCOLOR_XRGB(d(e), d(e), d(e)), //random color
//D3DCOLOR_XRGB(255, 0, 0), // red color
D3DCOLOR_XRGB(0, 0, 0), // black color
1.0f, 0xFFFFFFFF
);
}
void Direct3D::endScene()
{
_pD3DDevice->EndScene(); // close a frame, after this call we are not allowed to do draw calls
_pD3DDevice->Present(nullptr, nullptr, nullptr, nullptr); // swap back- with frontbuffer
}
void Direct3D::deInit()
{
safeRelease(_pD3DDevice);
}