Original Post
I''m hoping someone here can tell me why my init function is failing at the device creation?
It compiles fine, but m_pDevice is unable to be created and initilization returns 2 (I put in the ints so I knew where it was closing).
int CGraphics::Initilize(int iWidth, int iHeight, D3DFORMAT Format, HWND hWnd)
{
HRESULT hr;
m_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
if(!m_pDirect3D)
return 1;
ZeroMemory(&m_D3DCaps, sizeof(m_D3DCaps));
if(FAILED(m_pDirect3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_D3DCaps)))
return false;
D3DDISPLAYMODE displayMode; // Holds Monitor Display Mode
hr = m_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
if(FAILED(hr))
return 4;
// and here are the D3D present parameters
//ZeroMemory(&m_D3DPresent, sizeof(m_D3DPresent));
//m_D3DPresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
//m_D3DPresent.SwapEffect = D3DSWAPEFFECT_FLIP;
m_D3DPresent.hDeviceWindow = hWnd;
m_D3DPresent.Windowed = TRUE;
m_D3DPresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_D3DPresent.BackBufferCount = 1;
m_D3DPresent.BackBufferFormat = displayMode.Format;
m_D3DPresent.BackBufferWidth = displayMode.Width;
m_D3DPresent.BackBufferHeight = displayMode.Height;
//m_D3DPresent.BackBufferFormat = Format;
// lets see if vertex processing is available...
if(m_D3DCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
hr = m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_D3DPresent, &m_pDevice);
else
hr = m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_D3DPresent, &m_pDevice);
if(FAILED(hr))
return 2;
m_pDevice->SetVertexShader(NULL);
m_pDevice->SetFVF(D3DFVF_VERTEX);
m_pDevice->CreateVertexBuffer(sizeof(VERTEX) * 4, NULL, D3DFVF_VERTEX, D3DPOOL_MANAGED, &m_pVertexBuffer, NULL);
m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(VERTEX));
// last but not least... rendering states...
m_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
return 3;
}
What is really weird is the following code worked fine in another program I did, and they are a different setup, but I don''t see why it would fail above...
int Direct3DInit()
{
g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION); // Create Direct 3D
if (g_pDirect3D == NULL) // Note: Add d3d.lib!
return false;
D3DDISPLAYMODE displayMode; // Holds Monitor Display Mode
// If not full screen then fill displayMode with current settings.
if(g_bFullScreen == false)
{
g_hr = g_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
if(FAILED(g_hr))
return false;
}
else
{
displayMode.Width = WND_WIDTH;
displayMode.Height = WND_HEIGHT;
displayMode.RefreshRate = 0; // default
// Set the color format to 565 16 bit
displayMode.Format = D3DFMT_R5G6B5;
}
// Tells Direct3D how it''s supposed to show shit, then nulls it.
D3DPRESENT_PARAMETERS presentParameters;
memset(&presentParameters, 0, sizeof(D3DPRESENT_PARAMETERS));
// Set Window Mode & Other Present Paramaters for D3D
if(g_bFullScreen == false)
presentParameters.Windowed = TRUE;
else
presentParameters.Windowed = FALSE;
presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentParameters.BackBufferFormat = displayMode.Format;
presentParameters.BackBufferWidth = displayMode.Width;
presentParameters.BackBufferHeight = displayMode.Height;
// Create our D3D device
g_hr = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &g_pDevice);
if(FAILED(g_hr))
return false;
// Matrix code
D3DXMATRIX mat;
D3DXMatrixPerspectiveFovLH(&mat, D3DX_PI/6, WND_WIDTH/WND_HEIGHT, 1.0, 100.0 );
g_pDevice->SetTransform(D3DTS_PROJECTION, &mat );
D3DXMatrixIdentity(&mat);
g_pDevice->SetTransform(D3DTS_WORLD, &mat);
D3DXMatrixTranslation(&mat, 0, 0, 14.0);
g_pDevice->SetTransform(D3DTS_VIEW, &mat);
// Setup Vertex Buffers
g_pDevice->CreateVertexBuffer(sizeof(CVertex)*4, NULL, D3DFVF_CVertex, D3DPOOL_MANAGED, &g_pVertexBuffer, NULL);
CVertex *vtxQuad;
g_pVertexBuffer->Lock(0, 0, (void**)&vtxQuad, D3DLOCK_DISCARD);
// Create Object Vertices
vtxQuad[0].Create(0, 0, 0, 1, 1, 1, 0, 1);
vtxQuad[1].Create(0, 2, 0, 1, 1, 1, 0, 0);
vtxQuad[2].Create(2, 0, 0, 1, 1, 1, 1, 1); // Front Face
vtxQuad[3].Create(2, 2, 0, 1, 1, 1, 1, 0);
vtxQuad[4].Create(0, 0, 2, 1, 1, 1, 0, 1);
vtxQuad[5].Create(0, 2, 2, 1, 1, 1, 0, 0);
vtxQuad[6].Create(2, 0, 2, 1, 1, 1, 1, 1); // Back Face
vtxQuad[7].Create(2, 2, 2, 1, 1, 1, 1, 0);
//vtxQuad[8].Create(2, 0, 0, 0, 1, 1, 0, 1);
//vtxQuad[9].Create(2, 2, 0, 0, 1, 0, 0, 0);
//vtxQuad[10].Create(2, 0, 2, 0, 0, 1, 1, 1); // Right Side
//vtxQuad[11].Create(2, 2, 2, 0, 0, 0, 1, 0);
//vtxQuad[12].Create(0, 0, 0, 0, 1, 1, 0, 1);
//vtxQuad[13].Create(0, 2, 0, 0, 1, 0, 0, 0);
//vtxQuad[14].Create(0, 0, 2, 0, 0, 1, 1, 1); // Left Side
//vtxQuad[15].Create(0, 2, 2, 0, 0, 0, 1, 0);
//vtxQuad[12].Create(0, 0, 2, 0, 0, 1, 0, 1);
//vtxQuad[13].Create(0, 1, 2, 0, 0, 0, 0, 0);
//vtxQuad[14].Create(0, 1, 2, 0, 0, 0, 0, 0);
//vtxQuad[14].Create(0, 1, 2, 0, 0, 0, 0, 0);
//vtxQuad[12].Create(0, 0, 1, 0, 0, 1, 0, 1);
//vtxQuad[13].Create(0, 1, 1, 0, 0, 0, 0, 0);
//vtxQuad[14].Create(0, 1, 1, 0, 0, 0, 0, 0);
//vtxQuad[14].Create(0, 1, 1, 0, 0, 0, 0, 0);
g_pVertexBuffer->Unlock();
g_pDevice->CreateIndexBuffer(36*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIndexBuffer, NULL);
WORD cube_indices[18] = {0,1,2,1,2,3,
4,5,6,5,6,7/*,
8,9,10,9,10,11/*,
12,13,14,13,14,15*/};
VOID* pIndices;
g_pIndexBuffer->Lock(0, sizeof(cube_indices), &pIndices, 0);
// copy over our index information into our index buffer
memcpy(pIndices, cube_indices, sizeof(cube_indices));
// now unlock the index buffer so that we can use it
g_pIndexBuffer->Unlock();
// Set it up to blend between the vertex color and the texture color
g_pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
// Turn off the alpha channel since the texture doesn''t have one
g_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
// Load our Texture
D3DXCreateTextureFromFile(g_pDevice, "blstar.jpg", &g_pTexture);
g_pDevice->SetTexture(0, g_pTexture);
g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
return true;
}
- Newb Programmer: Geek++