DirectX and Dev-C++

Started by
4 comments, last by caldiar 18 years, 7 months ago
ok.... well in my last thread I discovered that my compiler is funky. It thinks it has to do things differently than everybody else so Im getting a linker error from the code Im testing to get a REALLY simple window to open using DirectX 9. here's the code....

#include <windows.h>

// include directx9
#include "dx/d3d9.h"

// global variables
HINSTANCE hInst;							// application instance
HWND wndHandle;								// application window handle

LPDIRECT3D9             pD3D;				// the Direct3D Object
LPDIRECT3DDEVICE9       pd3dDevice;			// the Direct3D Device

////////////////////////////////////////////// forward declarations
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

// DirectX functions
bool initDirect3D();	
void render(void);

/*********************************************************************
* WinMain
*********************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPTSTR lpCmdLine, int nCmdShow)
{
	if (!initWindow(hInstance))
	{
		MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
		return false;
	}

	if (!initDirect3D())
	{
		MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
		return false;
	}

	// Main message loop:
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }		
		else
		{
			render();
		} 
    }

	// release the device and the direct3D object
	if( pd3dDevice != NULL) 
        pd3dDevice->Release();

    if( pD3D != NULL)
        pD3D->Release();
		
	return (int) msg.wParam;
}

/*********************************************************************
* initWindow
*********************************************************************/
bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "DirectXExample";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	// create the window
	wndHandle = CreateWindow("DirectXExample", 
							 "DirectXExample", 
							 WS_OVERLAPPEDWINDOW,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}

/*********************************************************************
* WndProc
*********************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

/*********************************************************************
* initDirect3D
* initializes direct3D
*********************************************************************/
bool initDirect3D()
{
	pD3D = NULL;
	pd3dDevice = NULL;

	// create the directX object
	if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
	{
		return false;
	}

	// fill the presentation parameters structure
	D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount  = 1;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferWidth  = 640;
	d3dpp.hDeviceWindow    = wndHandle;

	// create a default directx device
    if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &pd3dDevice ) ) )
    {
        return false;
    }
	return true;
}

/*********************************************************************
* render
*********************************************************************/
void render(void)
{
	// check to make sure we have a valid Direct3D Device
	if( NULL == pd3dDevice )
        return;

    // Clear the backbuffer to a blue color
    pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

	// Present the backbuffer contents to the display
    pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

and heres the linking error! [linker error] undefined reference to 'Direct3DCreate9@4' hmm... any solutions? I've tried using reimp to change .lib files to the .a files but um... I get this when trying to use some of them g++ exe: cannot specify -o with -c or -S and multiple compilations = help is greatly appreciated =)
Advertisement
Quote:Original post by caldiar
hmm... any solutions? I've tried using reimp to change .lib files to the .a files but um... I get this when trying to use some of them

g++ exe: cannot specify -o with -c or -S and multiple compilations


can you show us your linker command? sounds like you're using some flags out of place.
indeed. I think it was
-lm c:\dev-cpp\lib\libd3setup.a

Did you fix it? If not, here some thing you might do:

Replace #include "dx/d3d9.h" with #include <d3d9.h>

or

link to ld3d9 in the linker options located in your Projects Options.

Hope this helps!
Hope I was helpful. And thank you if you were!
Quote:Original post by caldiar
indeed. I think it was
-lm c:\dev-cpp\lib\libd3setup.a


it should be either:

-lm -Lc:/dev-cpp/lib -ld3setup

or

-lm -ld3setup

if c:/dev-cpp/lib is the standard location for your libraries (probably is), notice the use of / instead of \.

Edit: Oh! and Direct3DCreate9 is in d3d9.lib, so use -ld3d9

[Edited by - Kwizatz on September 9, 2005 10:22:58 AM]
YAY I MADE A WINDOW WITH A BLUE BACKGROUND!!!!
Thank you so much! =) I tried -ld3d9 before but it said it couldn't be found
the issue there was I had it in my include folder... not my lib folder lol.

Thanks again! Hope is not lost. I will continue my quest to learn DirectX and will hold off on switching to OpenGL again.

Again, thanks =)

This topic is closed to new replies.

Advertisement