Thread: Need help with getting DirectX 9.0 initilization

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    49

    Need help with getting DirectX 9.0 initilization

    Hi.

    I need help getting the DirectX 9.0 SDK to work with Microsoft visual xpress 2005 C++. I don't know how and the tutorials arent clear to me, from microsoft and their tutorials dont show me what the header looks like nor what libaries to include. and basically all tutorials on the net are for different compilers and include different "stuff". help!

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    • Download and install psdk
    • Download and install dx sdk.
    • Set the include and lib directories order so that the psdk and dx include and lib directories are first
    If you've done that properly, you should be able to build, at the very least, the first example with the dx sdk. If not, you need to provide more information, including any error messages you get and a snippet of the code where the problem lies.

    If your interest lies in game programming then you may find cprogramming's game board of particular interest; be sure to check through their game programming links thread first and any other sticky threads they may have for useful information.
    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    Ok, it almost worked. I dont think it is the libaries problem, I think it might be compiler settings or something. But this code doesnt work says these errors:
    1>Compiling...
    1>dx.cpp
    1>c:\documents and settings\keenan\my documents\visual studio 2005\projects\dx9\dx9\dx.cpp(52) : error C2440: '=' : cannot convert from 'const char [14]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    1>c:\documents and settings\keenan\my documents\visual studio 2005\projects\dx9\dx9\dx.cpp(60) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast




    Code:
    /*	Triangle.cpp
    
    	Demonstration code showing how to draw a simple triangle using Direct3D.
    	See the site for details of each stage.
    
    	Written by Keith Ditchburn 2006
    */
    
    #define VC_EXTRALEAN
    #include <windows.h>
    #include <d3d9.h>		// Main Direct3D header
    #include <d3dx9.h>		// D3DX helper functions
    
    // Forward declarations
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    bool InitialiseDirect3D(HWND hWnd);
    void CloseDownDirect3D();
    void RenderScene();
    bool CreateTriangle();
    void DrawTriangle();
    void SetMatrices();
    
    // Globals
    LPDIRECT3D9 gD3dObject=NULL;
    LPDIRECT3DDEVICE9 gD3dDevice=NULL;
    LPDIRECT3DVERTEXBUFFER9	gVertexBuffer=NULL;
    
    // My very own vertex structure
    struct TMyVertex
    {
    	D3DXVECTOR3 p;	// Vertex position	
    	D3DCOLOR c;		// Vertex colour
    };
    
    // Flag used to describe the above (Flexible Vertex Format) structure to Direct3D
    // so Direct3D knows the type of data sent to it per vertex
    #define MYVERTEX_FVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
    
    // The main entry point of our Windows program
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	// Create and fill out a window class structure
    	WNDCLASS wcex;
    	ZeroMemory(&wcex,sizeof(WNDCLASS));
    	
    	// Provide the address of the function Windows should call with messages	
    	wcex.lpfnWndProc= (WNDPROC)WndProc;
    	wcex.style= CS_HREDRAW | CS_VREDRAW;
    	wcex.hInstance= hInstance;
    	wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
    	wcex.lpszClassName= "MyWindowClass";
    
    	// Register my new window class with the Windows API
    	RegisterClass(&wcex);
    
    	// Create the window
    	HWND hWnd = CreateWindow("MyWindowClass", "D3D Triangle Demo", 
    		WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
    		NULL, NULL, hInstance, NULL);
    	
    	if (hWnd==0)
    		return -1;
    
    	// Set up Direct3D
    	if (!InitialiseDirect3D(hWnd))
    		return -1;
    
    	// Create our triangle
    	if (!CreateTriangle())
    		return -1;
    
    	// Set our matrices
    	SetMatrices();
    
    	// Show the window for the first time
    	ShowWindow(hWnd, nCmdShow);
    	UpdateWindow(hWnd);
    
    	// Windows sends some messages directly to your callback function but others are placed in 
    	// a queue. These tend to be keyboard messages or other input that may need translating. We 
    	// enter a loop that checks for queued messages. If one is found it is translated and dispatched
    	// to our windows procedure. Once the application is closed a WM_QUIT message is received.
    	// When there are no messages to handle we draw our scene. This makes our program 'badly behaved'
    	// as we are hogging system resources so I tend to put in a sleep(0) that lets other threads have 
    	// at least some time.
    	MSG msg;
    	ZeroMemory( &msg, sizeof(msg) );
    	while( msg.message!=WM_QUIT )
    	{
    		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
    		{
    			TranslateMessage( &msg );
    			DispatchMessage( &msg );
    		}
    		else
    		{
    			RenderScene(); 
    			Sleep(0);
    		}
    	}
    
    	// Clean up before exiting
    	CloseDownDirect3D();
    
    	return (int)msg.wParam;
    }
    
    // Callback message handler function for our window
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch(message)
    	{
    		case WM_DESTROY:
                 PostQuitMessage(0);
            break;
    		default:
    			// We do not want to handle this message so pass it back to Windows
    			// to handle it in a default way
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		break;
    	}
    
    	return 0;
    }
    
    // Set up Direct3D
    // Requires creation of the Direct3D object and the Direct3D device
    bool InitialiseDirect3D(HWND hWnd)
    {
    	// Create the main Direct3D object. This object is used to initialise Direct3D
    	// and to provide us with a device interface object
    	gD3dObject=Direct3DCreate9(D3D_SDK_VERSION);
    	if (gD3dObject==0)
    		return false;
    
    	// Fill out the parameters for our required device
    	D3DPRESENT_PARAMETERS presParams;
    	ZeroMemory(&presParams,sizeof(presParams));
    
    	presParams.Windowed=TRUE;
    	presParams.SwapEffect=D3DSWAPEFFECT_DISCARD;
    	presParams.BackBufferFormat=D3DFMT_UNKNOWN;
    	presParams.PresentationInterval=D3DPRESENT_INTERVAL_ONE;
    
    	// Get the Direct3D object to give us a pointer to a device interface object
    	// this device represents the graphics capabilities of the PC	
    	HRESULT hr=gD3dObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
    		D3DCREATE_HARDWARE_VERTEXPROCESSING,&presParams,&gD3dDevice);	
        if (FAILED(hr))
    	{
    		// If we could not get a hardware vertex processing device fallback to a software one
    		hr=gD3dObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
    			D3DCREATE_SOFTWARE_VERTEXPROCESSING,&presParams,&gD3dDevice);
    		if (FAILED(hr))
    			return false;
    	}
    
    	// Since we are providing a colour component per vertex we do not
    	// want Direct3D lighting to be on
    	gD3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
    
    	return true;
    }
    
    // Release all the Direct3D interfaces in reverse order to their creation
    // All Direct3D objects implement a Release interface
    void CloseDownDirect3D()
    {
    	if (gVertexBuffer)
    	{
    		gVertexBuffer->Release();
    		gVertexBuffer=NULL;
    	}
    	if (gD3dDevice)
    	{
    		gD3dDevice->Release();
    		gD3dDevice=NULL;
    	}
    	if (gD3dObject)
    	{
    		gD3dObject->Release();
    		gD3dObject=NULL;
    	}
    }
    
    /* There are 3 matrices using by Direct3D
    World Matrix - Transforms 3D data from Model Space into World Space. 
    	You need to set this before rendering every entity in your world.
    View Matrix - Transforms from World Space into View Space. 
    	You need to set this each time your camera changes position
    Projection Matrix - Transforms from View Space into Screen Space. 
    	You normally set this just once during initialization.
    */
    void SetMatrices()
    {
    	// Projection Matrix
    	D3DXMATRIX matProj;
    	FLOAT fAspect=((FLOAT)800)/600;
    	D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,fAspect,1.0f,100.0f);
    	gD3dDevice->SetTransform(D3DTS_PROJECTION,&matProj);
    
    	// View Matrix
    	// Set the camera back a bit along the Z axis and looking down Z	
    	D3DXMATRIX matView;	
    	D3DXVECTOR3 vEyePt(0.0f,0.5f,-2.0f );
    	D3DXVECTOR3 vLookatPt(0.0f,0.5f,0.0f );
    	D3DXVECTOR3 vUpVec(0.0f,1.0f,0.0f );		
    	D3DXMatrixLookAtLH(&matView,&vEyePt,&vLookatPt,&vUpVec );
    	gD3dDevice->SetTransform(D3DTS_VIEW,&matView ); 
    
    	// World Matrix
    	// Just using identity (so triangle is at origin of world)
    	D3DXMATRIX matWorld;
    	D3DXMatrixIdentity(&matWorld);
    	gD3dDevice->SetTransform(D3DTS_WORLD, &matWorld ); 
    }
    
    //	In this function we create our triangle by creating and filling a vertex buffer
    bool CreateTriangle()
    {
    	HRESULT hr=gD3dDevice->CreateVertexBuffer(3*sizeof(TMyVertex),D3DUSAGE_WRITEONLY, MYVERTEX_FVF,
    			D3DPOOL_MANAGED, &gVertexBuffer, NULL );
    	if (FAILED(hr))
    		return false;
    
    	// Lock and fill
    	TMyVertex* pVertices=NULL;
    	hr=gVertexBuffer->Lock(0,0,(void**)&pVertices,0);
    	if (FAILED(hr))
    		return false;
    
    	// Fill in position and colour values for our 3 vertices
    	// It is essential that the vertices are defined in clockwise order
    	pVertices[0].p=D3DXVECTOR3(-1.0f,0.0f,0.0f);
    	pVertices[0].c=D3DCOLOR_XRGB(255,0,0);			// Red
    
    	pVertices[1].p=D3DXVECTOR3(-1.0f,1.0f,0.0f);
    	pVertices[1].c=D3DCOLOR_XRGB(0,255,0);			// Green
    
    	pVertices[2].p=D3DXVECTOR3(+1.0f,0.0f,0.0f);
    	pVertices[2].c=D3DCOLOR_XRGB(0,0,255);			// Blue
    
    	gVertexBuffer->Unlock();
    
    	return true;
    }
    
    // Draw the scene
    void RenderScene()
    {
    	// Clear the back buffer to black
    	gD3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
    
    	// Note: all drawing must occur between the BeginScene and EndScene calls
    	if (SUCCEEDED(gD3dDevice->BeginScene()))
    	{		
    		// Draw out triangle
    		DrawTriangle();
    
    		// Tell Direct3D that we have finished sending it 3D data
    		gD3dDevice->EndScene();
    	}
    
    	// Indicate that the scene should be shown
    	gD3dDevice->Present(0,0,0,0);
    }
    
    // Draw the triangle
    void DrawTriangle()
    {
    	// To draw we pass the vertices down a stream. We set the source as our vertex buffer	
    	gD3dDevice->SetStreamSource(0,gVertexBuffer,0,sizeof(TMyVertex));
    
    	// We then tell Direct3D the make up of the vertices
    	gD3dDevice->SetFVF(MYVERTEX_FVF);
    
    	// Now draw our one triangle
    	gD3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
    }

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Msvc-express builds with unicode strings by default, so you can't use char arrays. The best option, if you want to code for both unicode and non-unicode is to #include <tchar.h> and use the _T or TEXT macro on all string literals. If you have no such interest then you'll need to prefix your string literals with 'L' to ensure they are of the proper, wide character type required by unicode api functions. Alternatively, but not recommended, is to build without unicode defined, in which case you can go back to using char arrays and unmodified string literals.

    For example, to make the string literal "MyWindowClass" literally a unicode string prefix it with the letter 'L':
    Code:
    L"MyWindowClass"
    Or, arguably better, use the _T or TEXT macros to build for both unicode and non-unicode:
    Code:
    #include <tchar.h>
    
    _T("MyWindowClass")
    The casting error (error C2440) is explicitly described together with its solution; refer to msdn dcumentation for that error code for examples of how to fix it.

    If you still have problems after attempting corrections then please post again.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    I did the second method(it solves 2 errors)
    Code:
    #include <tchar.h>
    _T("MyWindowClass");

    But I dont understand whats wrong, the error points within this:
    Code:
    	HWND hWnd = CreateWindow("MyWindowClass", "D3D Triangle Demo", 
    		WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
    		NULL, NULL, hInstance, NULL);
    And msdn says this: http://msdn2.microsoft.com/en-us/library/s5b150wd.aspx

    about my last error left:
    1>c:\documents and settings\keenan\my documents\visual studio 2005\projects\dx9\dx9\dx.cpp(61) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    Thank you for help so far.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Code:
    HWND hWnd = CreateWindow(_T("MyWindowClass"), _T("D3D Triangle Demo"), 
    		WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
    		NULL, NULL, hInstance, NULL);
    LPCWSTR is a pointer (type) to a unicode string and not a char; using the _T or TEXT macro ensures that it is properly converted if unicode is defined. You just have to apply it to every string literal you use.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    Now I get these errors

    1>dx.obj : error LNK2019: unresolved external symbol _D3DXMatrixLookAtLH@16 referenced in function "void __cdecl SetMatrices(void)" (?SetMatrices@@YAXXZ)
    1>dx.obj : error LNK2019: unresolved external symbol _D3DXMatrixPerspectiveFovLH@20 referenced in function "void __cdecl SetMatrices(void)" (?SetMatrices@@YAXXZ)
    1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
    1>C:\Documents and Settings\Keenan\My Documents\Visual Studio 2005\Projects\dx9\Debug\dx9.exe : fatal error LNK1120: 3 unresolved externals

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Those are linker errors. You need to create a win32 project with msvc-express which requires a little more fiddling about as described here:

    http://msdn.microsoft.com/vstudio/ex...k/default.aspx

    When you select a 'console' project after the aforementioned configuration, you should have some extra 'windows' options - you need to create an empty project, which will take care of the last of the three linker errors you have described.

    The first two relate directly to directx libraries(specifically, d3dx9.lib) which you will need to link as follows: Project menu --> your_project_name Properties. This opens the your_project_name property pages dialog. In the left hand pane, select Linker --> Input and add your library names to the 'additional dependencies' field in the right-hand pane. You'll need to do this for any build configurations you have (usually just 'debug' and 'release').

    For future reference, the library you need to link against for those particular errors is found by looking up the functions in question within the 'directx documentation for c++' which should be installed together with the directx sdk and referring to the 'import library' at the foot of the page; you should also be able to locate this information from msdn online, too. Note that you must remove the preceding underscore of the function name listed in the linker error to locate its name within the help files.

    The process thus described should be generally applicable for locating similar library or header information and making similar configuration adjustments.
    Last edited by Ken Fitlike; 05-09-2006 at 09:00 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Isometric Tile Engine using DirectX
    By Wraithan in forum Game Programming
    Replies: 3
    Last Post: 07-17-2006, 12:16 PM
  2. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 07:16 PM
  3. Problems with DirectX 9.0 SDK includes
    By SkyRaign in forum Game Programming
    Replies: 12
    Last Post: 12-23-2005, 09:45 AM
  4. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  5. How can I draw a simple JPG or BMP in DirectX 9.0 (C++) ??
    By LegendBreath in forum Windows Programming
    Replies: 6
    Last Post: 04-18-2005, 06:18 AM