bool CGame::InitialiseDirect3D(HWND hWnd, UINT nWidth, UINT nHeight)
{
LogInfo("<br>Initialise Direct3D:");
//First of all, create the main D3D object. If it is created successfully we
//should get a pointer to an IDirect3D8 interface.
m_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
if(m_pD3D == NULL)
{
LogError("<li>Unable to create DirectX8 interface.");
return false;
}
//Get the current display mode
D3DDISPLAYMODE d3ddm;
if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return false;
}
d3ddm.Format = CheckDisplayMode(nWidth, nHeight, 32);
if(d3ddm.Format != D3DFMT_UNKNOWN)
{
//Width x Height x 32bit has been selected
d3ddm.Width = nWidth;
d3ddm.Height = nHeight;
LogInfo("<li>%d x %d x 32bit back buffer format selected. Format = %d.", nWidth, nHeight, d3ddm.Format);
}
else
{
d3ddm.Format = CheckDisplayMode(nWidth, nHeight, 16);
if(d3ddm.Format != D3DFMT_UNKNOWN)
{
//Width x Height x 16bit has been selected
d3ddm.Width = nWidth;
d3ddm.Height = nHeight;
LogInfo("<li>%d x %d x 16bit back buffer format selected. Format = %d.", nWidth, nHeight, d3ddm.Format);
}
else
{
LogError("<li>Unable to select back buffer format for %d x %d.", nWidth, nHeight);
return false;
}
}
//Create a structure to hold the settings for our device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.BackBufferWidth = d3ddm.Width;
d3dpp.BackBufferHeight = d3ddm.Height;
d3dpp.hDeviceWindow = hWnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
// d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
//// d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE;
m_nScreenWidth = d3ddm.Width;
m_nScreenHeight = d3ddm.Height;
//
// g_ptCenter.x=nWidth/2;
// g_ptCenter.y=nHeight/2;
/* //Select the best depth buffer, select 32, 24 or 16 bit
if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D32) == D3D_OK)
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D32;
d3dpp.EnableAutoDepthStencil = TRUE;
LogInfo("<li>32bit depth buffer selected");
}
else if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24X8) == D3D_OK)
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;
d3dpp.EnableAutoDepthStencil = TRUE;
LogInfo("<li>24bit depth buffer selected");
}
else if(m_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16) == D3D_OK)
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.EnableAutoDepthStencil = TRUE;
LogInfo("<li>16bit depth buffer selected");
}
else
{
d3dpp.EnableAutoDepthStencil = FALSE;
LogError("<li>Unable to select depth buffer.");
}
*/
//Create a Direct3D device.
if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice)))
{
LogError("<li>Unable to create device.");
return false;
}
//Turn on back face culling. This is becuase we want to hide the back of our polygons
if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW)))
{
LogError("<li>SetRenderState: D3DRS_CULLMODE Failed");
return false;
}
else
{
LogInfo("<li>SetRenderState: D3DRS_CULLMODE OK");
}
//Turn on Depth Buffering
if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE)))
{
LogError("<li>SetRenderState: D3DRS_ZENABLE Failed");
return false;
}
else
{
LogInfo("<li>SetRenderState: D3DRS_ZENABLE OK");
}
//Set fill state. Possible values: D3DFILL_POINT, D3DFILL_WIREFRAME, D3DFILL_SOLID
if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID)))
{
LogError("<li>SetRenderState: D3DRS_FILLMODE Failed");
return false;
}
else
{
LogInfo("<li>SetRenderState: D3DRS_FILLMODE OK");
}
//Set the D3DRS_NORMALIZENORMALS render state to fix the problem when scaling the objects get darker
if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE)))
{
LogError("<li>SetRenderState: D3DRS_NORMALIZENORMALS Failed");
return false;
}
else
{
LogInfo("<li>SetRenderState: D3DRS_NORMALIZENORMALS OK");
}
//Enable alpha blending so we can use transparent textures
if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)))
{
LogError("<li>SetRenderState: D3DRS_ALPHABLENDENABLE Failed");
return false;
}
else
{
//Set how the texture should be blended (use alpha)
m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
LogInfo("<li>SetRenderState: D3DRS_ALPHABLENDENABLE OK");
}
return true;
}
windowed fps weird looking!
hey guys i have this question why is that when i use windowed mode for my fps the objects that i look at seem to be behind one another.........meaning....... its like when i look into a wall..........i see the objects behind the wall when im not supposed to and the objects sometimes seem to be infront of the wall when its actually isnt............ but when i use full screen mode.......everything seems fine........
here's the part for the display........maybe u guys can help out here?? thanks
You've commented out the code that actually sets the up the ZBuffer:
Which would cause the effects you've described.
d3dpp.AutoDepthStencilFormat = D3DFMT_D32; d3dpp.EnableAutoDepthStencil = TRUE;
Which would cause the effects you've described.
The reason it probably works in fullscreen is that since you don't set whether or not to use the zbuffer, it uses the adapter defaults. Which varies, but for you apparently it uses a zbuffer by default.
When in windowed mode it doesn't do this check, and you're left with zbuffer = false, unless you change it.
When in windowed mode it doesn't do this check, and you're left with zbuffer = false, unless you change it.
Yeah........... i had to comment that out......otherwise i cant see a thing..........everything is black...........
if i uncomment that part........it causes the problem that im getting........
any ideas?
if i uncomment that part........it causes the problem that im getting........
any ideas?
Are you actually clearing the z-buffer? Make sure you put that in when you clear the target before rendering... Are you sure your card supports 32 bit z-buffers? I think my card only supports up to 24 bit...
You can mess with it with the D3D caps. There is a function that will tell you if the hardware will support the z-buffer.
What video card do you have?
Edit: In managed DX, the function is in the manager class (Don't know if there is an equivalent in unmanaged, however). Here is the syntax:
where win is a PresentParameters structure.
Hope that helps.
What video card do you have?
Edit: In managed DX, the function is in the manager class (Don't know if there is an equivalent in unmanaged, however). Here is the syntax:
D3DRoot.CheckDepthStencilMatch(adapter, device type, adapter format, render target format, depth stencil format)example:D3DRoot.CheckDepthStencilMatch(0, DeviceType.Hardware, win.BackBufferFormat, win.BackBufferFormat, DepthFormat.D16)
where win is a PresentParameters structure.
Hope that helps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement