Jump to content
  • Advertisement
Sign in to follow this  
aleisterbukowski

Direct X Templates ?

This topic is 3706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I'm starting DirectX 9, and I bought "Introductioin to 3D Game Programming with DrectX 9.0" by Frank Luna Good book. Chapter 1 being what I would consider the most tedious, rigorous task in history initializing DirectX he sorta gives you a template to go by using D3DUtility and D3dUtility.h as files for your main project. Um... I was wondering what you would call this. Like a small class to Initialize DirectX? A template eveN/ My friend from Fullsail called it a template, and I was wondering if this is so, does anyone have any links to other DirectX 9.0 templates, or other ways of initializing DirectX 9.0 either with ease, or not. I'm just trying to learn what I can, so in the future I could possibly make my own template, but with the shear fact I will actually understand it and won't just copy N' paste it to my project. Thanks! ;)

Share this post


Link to post
Share on other sites
Advertisement
Here's one. It was an experiment to see how small of a program to render a single colored triangle I could write without doing anything unreasonable.

#include <windows.h>
#include <d3d9.h>

HWND hWindow;
IDirect3D9* D3D;
IDirect3DDevice9* Device;

struct Vertex
{
float x, y, z, rhw;
D3DCOLOR Color;
};
#define FVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

LRESULT WINAPI WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
ValidateRect( hWnd, NULL );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}

int main()
{
HINSTANCE hInstance = GetModuleHandle( NULL );

//create a window
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0, 0, hInstance, NULL, NULL, NULL, NULL, "MiniD3D", NULL };
RegisterClassEx( &wc );
hWindow = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, "MiniD3D", "MiniD3D", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
300, 300, NULL, NULL, hInstance, NULL );
ShowWindow( hWindow, SW_SHOW );
UpdateWindow( hWindow );

//Set up d3d
D3D = Direct3DCreate9( D3D_SDK_VERSION );
D3DPRESENT_PARAMETERS d3dpp = { 0 };
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
D3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &Device );

//now create our triangle
Vertex Triangle[3] =
{
{ 150.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 255, 0, 0 ) },
{ 250.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 0, 255, 0 ) },
{ 50.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 0, 0, 255 ) }
};

MSG msg;
bool RunApp = true;
while( RunApp )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
RunApp = false;

TranslateMessage( &msg );
DispatchMessage( &msg );
}

//render stuff
Device->Clear( 0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );

Device->BeginScene();
Device->SetFVF( FVF_VERTEX );
Device->DrawPrimitiveUP( D3DPT_TRIANGLELIST, 1, Triangle, sizeof(Vertex) );
Device->EndScene();

Device->Present( 0, 0, 0, 0 );
}

Device->Release();
D3D->Release();

DestroyWindow( hWindow );
return 0;
}

That's 87 lines of code, with fairly generous whitespace and giving all the scoping braces their own lines. I happen to like this piece of code because most people who have written D3D books have managed to screw it up. As a result, everybody comes out convinced that D3D is really hard to get started and takes a lot of code to initialize.

For the record, there's an OpenGL version too. It's longer, mainly thanks to extra code to set up the pixel format. It doesn't really differ otherwise.

Share this post


Link to post
Share on other sites
Thanks!

Um... I'm getting one error was wondering if ya could help real quick?


------ Build started: Project: Dx_09_Programming, Configuration: Debug Win32 ------
LINK : fatal error LNK1104: cannot open file 'd3dx9.lib'



Any ideas?

I thought it was because I didn't have d3dx9.lib linked, or that Visual C++ can't find the libs of Dx, or it simply wasn't there, but that's not the case, for I use the exact project from the books example with working code except I removed the Utility class and Utility header file, and used your code in the main source.

But I got zie error. How Come?

Share this post


Link to post
Share on other sites
Quote:
Original post by aleisterbukowski
I fixed it. Appearantly when I copied and paste code the Dx SDK wasn't linked to the project correctly.

Flippin' weird.

Thanks! ;)
Yeah, the linker settings have nothing to do with source code. You could also add the line #pragma comment(lib, "d3d9.lib") to the source file somewhere (I tend to do it after the includes).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!