Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with basic DirectX app

Started by Nahrix Apr 23, 2009 at 4:39 PM 5 replies 1.1k views
Original Post
Nahrix
Nahrix
Trying a piece of code from the book, "Ultimate Game Programming With DirectX" Language: C++ Compiler: MinGW IDE: Eclipse Problem: I'm getting the error, "undefined reference to `Direct3DCreate9@4'" on the line that uses Direct3DCreate9(D3D_SDK_VERSION), and I'm getting two warnings, "ignoring #pragma comment", for the two pragma lines up front.

#include <d3d9.h>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME "Blank D3D Window"

// Function prototypes
bool InitializeD3D(HWND hWnd, bool fullscreen);
void RenderScene();
void Shutdown();

// Direct3D object and device
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;

	case WM_KEYUP:
		if (wParam == VK_ESCAPE)
		{
			PostQuitMessage(0);
			break;
		}
	}

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

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int show)
{
	// Register the window class
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0, hInst, NULL, NULL, NULL, NULL, WINDOW_CLASS, NULL };

	RegisterClassEx(&wc);

	// Create the application's window
	HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, GetDesktopWindow(), NULL, wc.hInstance, NULL);

	// Initialize Direct3D
	if (InitializeD3D(hWnd, false))
	{
		// Show the window
		ShowWindow(hWnd, SW_SHOWDEFAULT);
		UpdateWindow(hWnd);

		// Enter the 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
			{
				RenderScene();
			}
		}
	}

	// Release any and all resources
	Shutdown();

	// Unregister the class
	UnregisterClass(WINDOW_CLASS, wc.hInstance);
	return 0;
}

bool InitializeD3D(HWND hWnd, bool fullscreen)
{
	D3DDISPLAYMODE displayMode;

	// Create the D3D object
	g_D3D = Direct3DCreate9(D3D_SDK_VERSION);

	if (g_D3D == NULL)
	{
		return false;
	}

	// Get the desktop display mode
	if (FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
	{
		return false;
	}

	// Set up the structure used to create the D3DDevice
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));

	if (fullscreen)
	{
		d3dpp.Windowed = FALSE;
		d3dpp.BackBufferWidth = 640;
		d3dpp.BackBufferHeight = 480;
	}
	else
	{
		d3dpp.Windowed = TRUE;
	}

	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = displayMode.Format;

	// Create the D3DDevice
	if (FAILED(	g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
				&d3dpp, &g_D3DDevice)))
	{
		return false;
	}

	return true;
}

void RenderScene()
{
	// Clear the backbuffer
	g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	// Begin the scene.  Start rendering
	g_D3DDevice->BeginScene();

	// End the scene.  Stop rendering
	g_D3DDevice->EndScene();

	// Display the scene.
	g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}

void Shutdown()
{
	if (g_D3DDevice != NULL)
	{
		g_D3DDevice->Release();
	}

	if (g_D3D != NULL)
	{
		g_D3D->Release();
	}

	g_D3DDevice = NULL;
	g_D3D = NULL;
}


[Edited by - Nahrix on April 23, 2009 5:50:59 PM]
Laccolith
Laccolith
I think the second library should be d3dx9, not d3d9x:

#pragma comment(lib, "d3dx9.lib")
Nahrix
Nahrix
Damn; I fixed that, but the problem still exists (same error / two warnings)
Alatar
Alatar
I haven't used that compiler/IDE, but do you need to include windows.h?
"All you have to decide is what to do with the time that is given to you." - Gandalf
Nahrix
Nahrix
No
Nahrix
Nahrix
I have resolved the issue by using Visual Studio instead of Eclipse.
SiCrane
SiCrane
#pragmas are compiler specific. Visual C++ can use them to specify libraries to link against. However, MinGW doesn't like MSVC's linking pragmas.

Topic Locked

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

Sign in to reply to this topic.