Skip to main content
gamedev.net
🔒 Locked

Direct3D

Started by Shaw Mishrak 9 replies 500+ views
Original Post
Shaw Mishrak
Shaw Mishrak
Could anyone point me to a good tutorial for fullscreen Direct3D under DirectX7? I can''t seem to figure it out...missing some initialization thing. Oh, and I would like it to be without the Framework of D3DX. Mainly the initialization. I am trying to convert a program from directx5 to directx7. Thanks, Shaw Mishrak
Shaw Mishrak
Shaw Mishrak
Nothing fails when I initialize. The main problem I have is that when I call Clear and set the color to a blue and call Flip, it stays black. To me, it seems like the 3D Device isnt being attached correctly to the surface...sorry for the length, but heres my initializationg funcion..the code at the end is my test code (mainly taken from the Triangle tutorial):

HRESULT hr;
HWND hWnd;
WNDCLASS wc;
DDSURFACEDESC2 ddsd;
DDSCAPS2 ddscaps;
HRESULT hRet;

// Set up and register window class
wc.style = CS_HREDRAW / CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY2));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NAME;
wc.lpszClassName = NAME;
RegisterClass(&wc);

// Create a window
hWnd = CreateWindowEx(WS_EX_TOPMOST,
NAME,
"D3D",
WS_POPUP,
0,
0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
SetFocus(hWnd);

///////////////////////////////////////////////////////////////////////////
// Create the main DirectDraw object
///////////////////////////////////////////////////////////////////////////
hRet = DirectDrawCreateEx(NULL, (VOID**)&g_pDD, IID_IDirectDraw7, NULL);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "DirectDrawCreateEx FAILED");

// Get exclusive mode
hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE / DDSCL_FULLSCREEN);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "SetCooperativeLevel FAILED");

OutputDebugString("Set Coop!\n");

// Set the video mode to 640x480x8
hRet = g_pDD->SetDisplayMode(1024, 768, 16, 0, 0);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "SetDisplayMode FAILED");

OutputDebugString("Set display!\n");
// Create the primary surface with 1 back buffer
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS / DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE / DDSCAPS_FLIP /
DDSCAPS_COMPLEX / DDSCAPS_3DDEVICE;
ddsd.dwBackBufferCount = 1;
hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "CreateSurface FAILED");

OutputDebugString("Create Surf!\n");

// Get a pointer to the back buffer
ZeroMemory(&ddscaps, sizeof(ddscaps));
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
hRet = g_pDDSPrimary->GetAttachedSurface(&ddscaps, &g_pDDSBack);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "GetAttachedSurface FAILED");

OutputDebugString("Getattached!\n");

// Create a timer to flip the pages
/*if (TIMER_ID != SetTimer(hWnd, TIMER_ID, TIMER_RATE, NULL))
return InitFail(hWnd, hRet, "SetTimer FAILED");*/


// Query DirectDraw for access to Direct3D
hr = g_pDD->QueryInterface( IID_IDirect3D7, (VOID**)&g_pD3D );
if( FAILED( hr) )
{
OutputDebugString("D3D Query failed!\n");
return hr;
}
OutputDebugString("Query!\n");

// Before creating the device, check that we are NOT in a palettized
// display. That case will cause CreateDevice() to fail, since this simple
// tutorial does not bother with palettes.
ddsd.dwSize = sizeof(DDSURFACEDESC2);
g_pDD->GetDisplayMode( &ddsd );
if( ddsd.ddpfPixelFormat.dwRGBBitCount <= 8 )
return DDERR_INVALIDMODE;

OutputDebugString("GetDispMode!\n");

// Create the device. The GUID is hardcoded for now, but should come from
// device enumeration, which is the topic of a future tutorial. The device
// is created off of our back buffer, which becomes the render target for
// the newly created device.
hr = g_pD3D->CreateDevice( IID_IDirect3DHALDevice, g_pDDSBack,
&g_pd3dDevice );
if( FAILED( hr ) )
{
// This call could fail for many reasons. The most likely cause is
// that we specifically requested a hardware device, without knowing
// whether there is even a 3D card installed in the system. Another
// possibility is the hardware is incompatible with the current display
// mode (once again, the correct implementation would use enumeration
// to check that). In any case, let''s simply try again with the RGB
// software rasterizer.
hr = g_pD3D->CreateDevice( IID_IDirect3DRGBDevice, g_pDDSBack,
&g_pd3dDevice );
if( FAILED( hr ) )
return hr;
}

OutputDebugString("Create device!\n");

//-------------------------------------------------------------------------
// Step 4: Create the viewport
//-------------------------------------------------------------------------

// Create the viewport
DWORD dwRenderWidth = 1024;
DWORD dwRenderHeight = 768;
D3DVIEWPORT7 vp = { 0, 0, dwRenderWidth, dwRenderHeight, 0.0f, 1.0f };
hr = g_pd3dDevice->SetViewport( &vp );
if( FAILED( hr ) )
return hr;

OutputDebugString("View!\n");




// Data for the geometry of the triangle. Note that this tutorial only
// uses ambient lighting, so the vertices'' normals are not actually used.
D3DVECTOR p1( 0.0f, 3.0f, 0.0f );
D3DVECTOR p2( 3.0f,-3.0f, 0.0f );
D3DVECTOR p3(-3.0f,-3.0f, 0.0f );
D3DVECTOR vNormal( 0.0f, 0.0f, 1.0f );

// Initialize the 3 vertices for the front of the triangle
g_pvTriangleVertices[0] = D3DVERTEX( p1, vNormal, 0, 0 );
g_pvTriangleVertices[1] = D3DVERTEX( p2, vNormal, 0, 0 );
g_pvTriangleVertices[2] = D3DVERTEX( p3, vNormal, 0, 0 );

// Initialize the 3 vertices for the back of the triangle
g_pvTriangleVertices[3] = D3DVERTEX( p1, -vNormal, 0, 0 );
g_pvTriangleVertices[4] = D3DVERTEX( p3, -vNormal, 0, 0 );
g_pvTriangleVertices[5] = D3DVERTEX( p2, -vNormal, 0, 0 );

// Set the material as yellow. We''re setting the ambient color here
// since this tutorial only uses ambient lighting. For apps that use real
// lights, the diffuse and specular values should be set. (In addition, the
// polygons'' vertices need normals for true lighting.)
D3DMATERIAL7 mtrl;
ZeroMemory( &mtrl, sizeof(mtrl) );
mtrl.ambient.r = 1.0f;
mtrl.ambient.g = 1.0f;
mtrl.ambient.b = 0.0f;
g_pd3dDevice->SetMaterial( &mtrl );

// The ambient lighting value is another state to set. Here, we are turning
// ambient lighting on to full white.
g_pd3dDevice->SetRenderState( D3DRENDERSTATE_AMBIENT, 0xffffffff );

// Set the transform matrices. Direct3D uses three independant matrices:
// the world matrix, the view matrix, and the projection matrix. For
// convienence, we are first setting up an identity matrix.
D3DMATRIX mat;
mat._11 = mat._22 = mat._33 = mat._44 = 1.0f;
mat._12 = mat._13 = mat._14 = mat._41 = 0.0f;
mat._21 = mat._23 = mat._24 = mat._42 = 0.0f;
mat._31 = mat._32 = mat._34 = mat._43 = 0.0f;

// The world matrix controls the position and orientation of the polygons
// in world space. We''ll use it later to spin the triangle.
D3DMATRIX matWorld = mat;
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATE_WORLD, &matWorld );

// The view matrix defines the position and orientation of the camera.
// Here, we are just moving it back along the z-axis by 10 units.
D3DMATRIX matView = mat;
matView._43 = 10.0f;
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATE_VIEW, &matView );

// The projection matrix defines how the 3D scene is "projected" onto the
// 2D render target (the backbuffer surface). Refer to the docs for more
// info about projection matrices.
D3DMATRIX matProj = mat;
matProj._11 = 2.0f;
matProj._22 = 2.0f;
matProj._34 = 1.0f;
matProj._43 = -1.0f;
matProj._44 = 0.0f;
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATE_PROJECTION, &matProj );





g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, 0x000000ff, 0L, 0L );



// Begin the scene
if( FAILED( g_pd3dDevice->BeginScene() ) )
return E_FAIL;

// Draw the triangle using a DrawPrimitive() call. Subsequent
// tutorials will go into more detail on the various calls for
// drawing polygons.
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, D3DFVF_VERTEX,
g_pvTriangleVertices, 6, NULL );

// End the scene.
g_pd3dDevice->EndScene();

g_pDDSBack->Flip(NULL, DDFLIP_WAIT);




return DD_OK;

Formating is a little off, but its all there. Its pretty much 90% of the program. Alot of this code was from the Triangle tutorial, except the DirectDraw part, which was from the DirectDraw examples (DDEX1).
Shaw Mishrak
Shaw Mishrak
Dont mind the odd messages in the OutputDebugString commands...there just my weird way of keeping track of errors =)

I dont know if this means anything to you guys, but at the end of the debugging session, one thread returns 0 and one returns -1, the main program returns 0:

The thread 0xFFF8C961 has exited with code -1 (0xFFFFFFFF).
The thread 0xFFE70F85 has exited with code 0 (0x0).
The program 'd:\D3DX\2\Debug\2.exe' has exited with code 0 (0x0).



Edited by - Shaw Mishrak on July 5, 2000 10:23:49 PM
Shaw Mishrak
Shaw Mishrak
Thanks for your help so far =) With DirectX5, it seemed easier for some reason, maybe ''cause I went through the tutorials and got a working fullscreen app in just a little bit. Ive been working on this for quite for time. I keep searching the net and gamedev, but all the tutorials I find use either the framework, D3DX, or are for dx5. Once I get the initialization part okay, I think I should be okay.
Shaw Mishrak
Shaw Mishrak
Thank you very much! Sometimes, I hate when the biggest problems are with 3-4 characters in your code. =) Its working now, and thanks again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.