Sign in to follow this  
bpops

help linking direct 3d library

Recommended Posts

I know some C, C++, Java, and IDL, but I am completely new to Direct3D and graphics in general. I picked up this book, Beginning Game Programming by Jonathan Harbour, which teaches game programming via C++ and Direct3D. I'm on the 5th Chapter, which gets you to use Direct3D for the first time. I am getting a fatal error when I compile.
Quote:
Compiling... winmain.cpp c:\program files\microsoft visual studio\myprojects\d3d_windowed\winmain.cpp(6) : fatal error C1083: Cannot open include file: 'd3d9.h': No such file or directory Error executing cl.exe.
I have at the top of my program,
Quote:
//header files to include #include <d3d9.h> #include <time.h>
And I have added d3d9.lib in the Project Settings (I'm using Microsoft Visual C++). I have the Direct3D SDK installed as well, so I'm unsur as to why this isn't working. Could it be problems with me using a different version of Direct3D? Or maybe I haven't installed it correctly? I'm just a newb, and any help is greatly appreciated. Thanks in advance!

Share this post


Link to post
Share on other sites
i don't know what it is like with VC++, but there should be a thing somewhere that allows you to enter a directory where the compiler will search for your headers. add the folder that d3d9.h is in. this might not be THE problem but it might be A problem.

hope it helps

Share this post


Link to post
Share on other sites
ah, ok, so the Microsoft DirectX SDK has a folder called "Include." I copied all the files from there to the "Include" folder in the MS Visual C++ directory, and all worked (except the compile errors, but that's a different story!)

So thanks for the help!

Share this post


Link to post
Share on other sites
:( I hate to be a pest, but after all the errors were cleaned up it compiled just fine. When I ran it, however, there were linking problems:

Quote:
--------------------Configuration: winmain - Win32 Debug--------------------
Linking...
winmain.obj : error LNK2001: unresolved external symbol _Direct3DCreate9@4
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/winmain.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

winmain.exe - 3 error(s), 0 warning(s)


any ideas?

Share this post


Link to post
Share on other sites
You might need to have:

#include "d3dx9.h"

And you might need to link "d3dx9.lib" in Project Setting as well.

I hope that helps.

P.S You're not being a pest, because you're giving me something to do well my lunch cooks ;)

Share this post


Link to post
Share on other sites
hmm.. well I added d3dx9.lib to the project settings, and moved EVERY .lib file from the Microsoft DirectX library folder to the Visual C++ folder, and now I'm getting a slightly different error:

Quote:
--------------------Configuration: winmain - Win32 Debug--------------------
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/winmain.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

winmain.exe - 2 error(s), 0 warning(s)


thanks again, Harryu

Share this post


Link to post
Share on other sites
Well, at least now you only have 1 error...

Have you included windows.h:

#include <windows.h>

Try that, and if it doesn't work then I will look for something else that might be the problem.

Share this post


Link to post
Share on other sites
i still get the problem. If it helps, here's my entire code:

Quote:

//Beginning Game Programming
//Chapter 5
//d3d_windowed program

//header files to include
#include <windows.h>
#include <d3d9.h>
#include <time.h>

//application title
#define APPTITLE "Direct3D_Windowed"

//forward declarations
LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
ATOM MyRegisterClass(HINSTANCE);
int Game_Init(HWND);
void Game_Run(HWND);
void Game_End(HWND);

//Direct3D objects
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;

//window event callback function
LRESULT WINAPI WinProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
Game_End(hWnd);
PostQuitMessage(0);
return 0;
}

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


//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);

//fill the struct with info
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;

//set up the window with the class info
return RegisterClassEx(&wc);
}

//entry point for a windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//declare variables
MSG msg;

//register the class
MyRegisterClass(hInstance);

//init the app
//note--got rid of initinstance
HWND hWnd;

//create a new window
hWnd = CreateWindow(
APPTITLE,
APPTITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
400,
NULL,
NULL,
hInstance,
NULL);

//was there an error creating the window?
if (!hWnd)
return FALSE;

//display the window
ShowWindow (hWnd, nCmdShow);
UpdateWindow(hWnd);

//init the game
if (!Game_Init(hWnd))
return 0;

//main message loop
int done = 0;
while (!done)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//look for quit message
if(msg.message == WM_QUIT)
{
MessageBox(hWnd, "Received WM_QUIT message","WinMain", MB_OK);
done = 1;
}

//decode and pass messages on to WndProc
TranslateMessage(&msg);
DispatchMessage(&msg);
}

else
//process game loop (else prevents running after window is closed)
Game_Run(hWnd);
}

return msg.wParam;
}


int Game_Init(HWND hwnd)
{
//display init message
MessageBox(hwnd, "Program is about to start","Game_Init",MB_OK);

//init Direct3D
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d == NULL)
{
MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK);
return 0;
}

//set Direct3D presentation parameters
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

//create Direct3D device
d3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

if (d3ddev == NULL)
{
MessageBox(hwnd, "Error creating Direct3D device","Error",MB_OK);
return 0;
}

//set random number seed
srand(time(NULL));

//return okay
return 1;
}


void Game_Run(HWND hwnd)
{
//make sure the Direct3D device is valid
if(d3ddev == NULL)
return;

//clear the backbuffer with a green color
d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,255,255),1.0f,0);

//start rendering
if(d3ddev->BeginScene())
{
//do something here!

//stop rendering
d3ddev->EndScene();
}

//display the backbuffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);

}


void Game_End(HWND hwnd)
{

//display close message
MessageBox(hwnd, "Program is about to end","Game_End",MB_OK);

//release the Direct3D device
if(d3ddev != NULL)
d3ddev->Release();

//release the Direct3D object
if(d3d != NULL)
d3d->Release();
}

Share this post


Link to post
Share on other sites
Once again, seeing as I have not used VC++, I don't really now how it does this stuff, but when you created the project, were there options to create a Console program, Win32 API program, etc? I just know that main() is the main message loop in C++ console applications, so perhaps the linker thinks that you are trying to create a console, but it can't find main() anywhere. Check in Project Settings and see if it says anything about the type of project it is. If it does turn out to be a console, try creating a new project (don't need to redo any code, just reuse the file with the new project) and make it a Win32 project, or Windows Application, depending on what the options are.

Hope this helps

Share this post


Link to post
Share on other sites
thanks again, Harryu.

It's getting late here, so I need to head off to bed, but I'll give this a go in the morning and see if I can get it working.

It's really great that there's people like you out there willing to help out newbie programmers :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this