Code:
#include "stdafx.h"
using namespace std;
struct D3DVertex
{
float x, y, z;
DWORD dwColor;
} *LPD3DVertex;
IDirect3D9 *g_pD3D = NULL;
IDirect3DDevice9 *g_pD3DDevice = NULL;
IDirect3DVertexBuffer9 *pVertexBuffer = NULL;
HRESULT Initialise(int nWidth, int nHeight);
void Cleanup();
void RenderFrame();
int main(int argc, char *argv[])
{
SDL_Event sdleEvent;
if (FAILED(Initialise(640, 480)))
{
cout << "Error initialising" << endl;
Cleanup();
return 1;
}
// Main loop
while (1)
{
RenderFrame();
while (SDL_PollEvent(&sdleEvent))
{
switch (sdleEvent.type)
{
case SDL_KEYUP:
if (sdleEvent.key.keysym.sym == SDLK_ESCAPE)
{
cout << "DEBUG: GOT ESCAPE MESSAGE\n";
break;
}
case SDL_QUIT:
cout << "DEBUG: GOT QUIT MESSAGE\n";
break;
}
}
}
Cleanup();
return 0;
}
// Initialise
//
// Returns: S_OK if initialisation ok, E_FAIL otherwise
//
HRESULT Initialise(int nWidth, int nHeight)
{
D3DPRESENT_PARAMETERS d3dpp;
D3DXMATRIX d3dxm;
VOID *pData = NULL;
D3DVertex vTriangle[] = {
{-2.0f, 1.0f, 10.0f, 0xffff0000},
{-3.0f, -1.0f, 10.0f, 0xff00ff00},
{-1.0f, -1.0f, 10.0f, 0xff0000ff}
};
// SDL Init
if (SDL_Init(SDL_INIT_VIDEO) < 0 || !SDL_GetVideoInfo())
return E_FAIL;
SDL_SetVideoMode(nWidth, nHeight, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_RESIZABLE);
// Direct3D Init
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (g_pD3D == NULL) return E_FAIL;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.hDeviceWindow = GetActiveWindow();
d3dpp.BackBufferWidth = nWidth;
d3dpp.BackBufferHeight = nHeight;
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetActiveWindow(),
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
{
return E_FAIL;
}
D3DXMatrixPerspectiveFovLH(&d3dxm, D3DX_PI / 4.0f, (float) nWidth / nHeight, 1, 1000);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &d3dxm);
g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, RGB(255, 255, 255));
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, false);
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, true);
g_pD3DDevice->SetFVF((D3DFVF_XYZ | D3DFVF_DIFFUSE));
// Create vertex buffer
g_pD3DDevice->CreateVertexBuffer(sizeof(vTriangle), D3DUSAGE_WRITEONLY,
(D3DFVF_XYZ | D3DFVF_DIFFUSE),
D3DPOOL_MANAGED, &pVertexBuffer, NULL);
// Set pData to point to the vertex buffer and use it to copy
// the vertex data into it
pVertexBuffer->Lock(0, sizeof(pData), (void **) &pData, 0);
memcpy(pData, vTriangle, sizeof(vTriangle));
pVertexBuffer->Unlock();
return S_OK;
}
// Cleanup
//
void Cleanup()
{
if (g_pD3DDevice)
{
g_pD3DDevice->Release();
g_pD3DDevice = NULL;
}
if (g_pD3D)
{
g_pD3D->Release();
g_pD3D = NULL;
}
}
// RenderFrame
//
void RenderFrame()
{
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
g_pD3DDevice->BeginScene();
g_pD3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(D3DVertex));
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_pD3DDevice->EndScene();
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
Once I get it accepting SDL input I'll be building in DirectInput.