//---------------------------------------------------------------------------------------------
// Initialize Direct3D
//---------------------------------------------------------------------------------------------
int CScreen::InitD3D ( int resWidth, // screen width
int resHeight, // screen height
D3DFORMAT resFormat, // resolution format of d3d
HWND hWnd, // HWND pointer
BOOL bWindowedMode, // windowed mode:false=fullscreen true=windowed
BOOL vsync) // vsync enabled?
{
// tell engine which wrapper we are using
DirectX = true;
OpenGL = false;
// set variables
ScreenWidth = resWidth;
ScreenHeight = resHeight;
// results
HRESULT hr;
//Make Direct3D object
d3d = Direct3DCreate9(D3D_SDK_VERSION);
//Make sure NULL pointer was not returned
if (!d3d)
return FALSE;
//Get device capabilities
ZeroMemory (&d3dCaps, sizeof(d3dCaps));
if (FAILED(d3d->GetDeviceCaps (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps)))
return FALSE;
//Setup present parameters
ZeroMemory(&d3dPresent,sizeof(d3dPresent));
d3dPresent.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dPresent.hDeviceWindow = hWnd;
d3dPresent.BackBufferCount = 1;
//Check if windowed
if (bWindowedMode)
{
fullscreen = false;
D3DDISPLAYMODE d3ddm;
RECT rWindow;
//Get display mode
d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
//Get window bounds
GetClientRect (hWnd, &rWindow);
//Setup screen dimensions
resWidth = rWindow.right - rWindow.left;
resHeight = rWindow.bottom - rWindow.top;
//Setup backbuffer
d3dPresent.Windowed = true;
d3dPresent.BackBufferFormat = d3ddm.Format;
d3dPresent.BackBufferWidth = rWindow.right - rWindow.left;
d3dPresent.BackBufferHeight = rWindow.bottom - rWindow.top;
}
else
{
fullscreen = true;
d3dPresent.Windowed = false;
d3dPresent.BackBufferFormat = resFormat;
d3dPresent.BackBufferWidth = resWidth;
d3dPresent.BackBufferHeight = resHeight;
}
// check for vsync
if(vsync)
{
d3dPresent.PresentationInterval= D3DPRESENT_INTERVAL_ONE;
}
else
{
d3dPresent.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
}
//Check if hardware vertex processing is available
if (d3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
//Create device with hardware vertex processing
hr = d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dPresent,
&d3dDevice);
}
else
{
//Create device with software vertex processing
hr = d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dPresent,
&d3dDevice);
}
//Make sure device was created
if (FAILED(hr))
return FALSE;
//Set vertex shader
d3dDevice->SetVertexShader(NULL);
d3dDevice->SetFVF(D3DFVF_TLVERTEX);
//Setup orthographic projection matrix
D3DXMatrixOrthoLH (&matOrtho, (float)resWidth, (float)resHeight, 1.0f, 10.0f);
D3DXMatrixIdentity (&matIdentity);
d3dDevice->SetTransform (D3DTS_PROJECTION, &matOrtho);
d3dDevice->SetTransform (D3DTS_WORLD, &matIdentity);
d3dDevice->SetTransform (D3DTS_VIEW, &matIdentity);
//Setup rendering states
d3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3dDevice->SetRenderState(D3DRS_DITHERENABLE, FALSE);
d3dDevice->SetRenderState(D3DRS_SPECULARENABLE, FALSE);
//Set texture stages
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
//to enable texcoord transforms on stage 0
d3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
// setup the viewport
// default viewport is the same size as the screen
D3DVIEWPORT9 ViewPort = { 0, 0, resWidth, resHeight, 0.0f, 1.0f };
d3dDevice->SetViewport(&ViewPort);
ViewPortX = 0;
ViewPortY = 0;
ViewPortWidth = resWidth;
ViewPortHeight = resHeight;
//turn off cursor
ShowCursor(FALSE);
//set variables
scr_width = resWidth;
scr_height = resHeight;
//ADAPTER PROPERTIES
//clear the adapter identifier
memset( &ident, 0, sizeof(ident) );
//set the indentifier
d3d->GetAdapterIdentifier( 0, 0, &ident ); //only supports default video card!
AdapterDesc(); //loads the description string
//Successfully initalized Direct3D
return TRUE;
}
Stupid VSYNC!!
Ok, for some STUPID reason, VSYNC decided to stop working on my office computer. On my home computer, it works using the exact same EXE, source, etc.
Here is my init:
So, on my office PC, the program runs like there is no VSYNC enabled. It USED to work and I can't find ANYTHING that has changed. I take this code home to my PC and it works!
I checked the PresentationInterval CAPS on the work PC and it supports both D3DPRESENT_INTERVAL_ONE and D3DPRESENT_INTERVAL_IMMEDIATE.
WTF??
Office computer:
Dell
XP Pro
1 Gig Ram
Radeon 7000
Home computer:
XP Home
768 MB Ram
Radeon 7500
Thanks
-cbmeeks
Check your driver settings. It's possible to force VSYNC on/off no matter what the application does. It depends on the video card, but I know my ATI does that.
Dur...that did the trick.
I was playing with it earlier and changed "balanced" performance to "optimal" performance. That disabled VSYNC. lol
Thanks
cb
I was playing with it earlier and changed "balanced" performance to "optimal" performance. That disabled VSYNC. lol
Thanks
cb
It should be mandatory to list what a driver affects when you move a slider. For example a list box containing:
"Chooses smaller mip levels sooner"
"Uses lower quality multisampling than the app requests"
"Uses lower precision color blending"
"Texture filtering is approximated"
"Disables VSync"
Unfortunately, you have to guess. It changes every driver version, and is not consistant across IHVs.
"Chooses smaller mip levels sooner"
"Uses lower quality multisampling than the app requests"
"Uses lower precision color blending"
"Texture filtering is approximated"
"Disables VSync"
Unfortunately, you have to guess. It changes every driver version, and is not consistant across IHVs.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement