#include "DirX9Cls.h" #define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) DirX9Cls::DirX9Cls() { } DirX9Cls::~DirX9Cls() { cleanD3D(); } void DirX9Cls::initD3D(HWND hWnd, bool fullScreen, UINT screenWidth, UINT screenHeight) { d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information ::ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use d3dpp.Windowed = fullScreen; // program windowed, not fullscreen d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit d3dpp.BackBufferWidth = screenWidth; // set the width of the buffer d3dpp.BackBufferHeight = screenHeight; // set the height of the buffer // create a device class using this information and information from the d3dpp struct d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); initGraphics(); // call the function to initialize the triangle } // this is the function used to render a single frame void DirX9Cls::renderFrame() { // clear the window to a deep blue d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,40,100), 1.0f, 0); d3ddev->BeginScene(); // begins the 3d scene d3ddev->SetFVF(CUSTOMFVF); // select which vertex format we are using d3ddev->SetStreamSource(0, buffer, 0, sizeof(CUSTOMVERTEX)); // select the vertex buffer to display d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); // copy the vertex buffer to the back buffer d3ddev->EndScene(); // ends the 3d scene d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame } // this is the function that cleans up Direct3D and COM void DirX9Cls::cleanD3D() { buffer->Release(); // close and release the vertex buffer d3ddev->Release(); // close and release the 3D device d3d->Release(); // close and release Direct3D } struct DirX9Cls::CUSTOMVERTEX { float x, y, z, rhw; // from the D3DFVF_XYZRHW flag DWORD color; // from the D3DFVF_DIFFUSE flag }; // this is the function that puts the 3D models into the video ram void DirX9Cls::initGraphics() { // create the vertices using the CUSTOMVERTEX struct CUSTOMVERTEX vert[] = { {320.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), }, {520.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), }, {120.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), }, }; // create a vertex buffer interface called buffer d3ddev->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &buffer, NULL); void* pVoid; // a void pointer // lock buffer and load the vertices into it buffer->Lock(0, 0, (void**)&pVoid, 0); memcpy(pVoid, vert, sizeof(vert)); buffer->Unlock(); }