Sign in to follow this  
wfrye2005

Learning DirectX, can't compile example code

Recommended Posts

wfrye2005    452
I'm working through Beginning Game Programming (J. Harbour, 3rd Ed) and I've hit a roadblock. The code in the book won't compile, and after reading similar topics here (about the same sample program in the same book) none of the solutions that apparently worked for them are working for me. I tried Harbour's website, but it won't let me view any of the book's help boards without making an account and having it manually approved by an administrator (and three days later, no word from them).

Here's the full code, exactly as it appears in the book (and on the accompanying disc):

[code]/*
Beginning Game Programming, 3rd Ed.
Chapter 3
Direct3D_Windowed program
*/

#include <windows.h>
#include <d3d9.h>
#include <time.h>
#include <iostream>
using namespace std;

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

//program settings
const string APPTITLE = "Direct3D_Windowed";
const int SCREENW = 1024;
const int SCREENH = 768;

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

bool gameover = false;

//macro to detect key presses
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)


/**
** Game initialization function
**/
bool Game_Init(HWND window)
{
MessageBox(window, "Game_Init", "BREAKPOINT", 0);

//initialize Direct3D
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == NULL)
{
MessageBox(window, "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_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = SCREENW;
d3dpp.BackBufferHeight = SCREENH;
d3dpp.hDeviceWindow = window;

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

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

return true;
}

/**
** Game update function
**/
void Game_Run(HWND hwnd)
{
//make sure the Direct3D device is valid
if (!d3ddev) return;

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

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

//stop rendering
d3ddev->EndScene();

//copy back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);
}

//check for escape key (to exit program)
if (KEY_DOWN(VK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);
}

/**
** Game shutdown function
**/
void Game_End(HWND hwnd)
{
//display close message
MessageBox(hwnd, "Program is about to end", "Game_End", MB_OK);

//free memory
if (d3ddev) d3ddev->Release();
if (d3d) d3d->Release();
}


/**
** Windows event handling function
**/
LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
gameover = true;
PostQuitMessage(0);
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}

/**
** Main Windows entry function
**/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//set the new window's properties
//previously found in the MyRegisterClass function
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
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.c_str();
wc.hIconSm = NULL;
RegisterClassEx(&wc);

//create a new window
//previously found in the InitInstance function
HWND window = CreateWindow( APPTITLE.c_str(), APPTITLE.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
SCREENW, SCREENH,
NULL, NULL, hInstance, NULL);

//was there an error creating the window?
if (window == 0) return 0;

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

//initialize the game
if (!Game_Init(window)) return 0;


// main message loop
MSG message;
while (!gameover)
{
if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}

Game_Run(window);
}

Game_End(window);

return message.wParam;[/code]

Other topics were resolved by linking the appropriate DirectX SDK libsand setting the Additional Dependencies, but doing so simply gives me a different (and longer) set of compiler errors, as follows:

[code]1>------ Rebuild All started: Project: Direct3D_Windowed, Configuration: Debug Win32 ------
1> winmain.cpp
1>winmain.obj : error LNK2019: unresolved external symbol __imp__MessageBoxA@16 referenced in function "bool __cdecl Game_Init(struct HWND__ *)" (?Game_Init@@YA_NPAUHWND__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__PostMessageA@16 referenced in function "void __cdecl Game_Run(struct HWND__ *)" (?Game_Run@@YAXPAUHWND__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__GetAsyncKeyState@4 referenced in function "void __cdecl Game_Run(struct HWND__ *)" (?Game_Run@@YAXPAUHWND__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__DefWindowProcA@16 referenced in function "long __stdcall WinProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WinProc@@YGJPAUHWND__@@IIJ@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WinProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WinProc@@YGJPAUHWND__@@IIJ@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__DispatchMessageA@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__PeekMessageA@20 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__UpdateWindow@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__ShowWindow@8 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExA@48 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__RegisterClassExA@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__GetStockObject@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__LoadCursorA@8 referenced in function _WinMain@16
1>C:\Users\....\Beginning Game Programming\Direct3D_Windowed\Debug\Direct3D_Windowed.exe : fatal error LNK1120: 14 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========[/code]

The only thing I can think of would be that the software I'm using (VC++2010 Express, June 2010 DirectX SDK) and what the author was using (VC++2008, March 2009 DirectX SDK) are just incompatible. I can't really continue with the book without getting this to work. Any ideas?

Share this post


Link to post
Share on other sites
yewbie    677
try adding:

[code]#pragma comment(lib,"user32.lib");[/code]

edit: explaination:
Your windows.h include was referencing functions that are part of the user32 library, under your linker settings you are probably not currently linking to that.

Or since your using VS2010

under your project settings do the following:
Linker->System->SubSystem :
Change to Windows (/SUBSYSTEM:WINDOWS)

Share this post


Link to post
Share on other sites
wfrye2005    452
[quote name='yewbie' timestamp='1313701823' post='4850936']
try adding:

[code]#pragma comment(lib,"user32.lib");[/code]

edit: explaination:
Your windows.h include was referencing functions that are part of the user32 library, under your linker settings you are probably not currently linking to that.

Or since your using VS2010

under your project settings do the following:
Linker->System->SubSystem :
Change to Windows (/SUBSYSTEM:WINDOWS)
[/quote]

Thanks, that lib took care of all but one unresolved external symbol error (the /SUBSYSTEM:WINDOWS was already set). Here's the remaining error:

[code]1>------ Rebuild All started: Project: Direct3D_Windowed, Configuration: Debug Win32 ------
1> winmain.cpp
1>winmain.obj : error LNK2019: unresolved external symbol __imp__GetStockObject@4 referenced in function _WinMain@16
1>C:\Users\...\Beginning Game Programming\Direct3D_Windowed\Debug\Direct3D_Windowed.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========[/code]

Any idea what that one needs? My C++ primer didn't cover anything like this (Herbert Schildt's C++: A Beginner's Guide). Probably should have gone through a more advanced C++ book before jumping into game programming. >___>

Share this post


Link to post
Share on other sites
yewbie    677
[quote name='TheSasquatch' timestamp='1313702881' post='4850947']
Probably should have gone through a more advanced C++ book before jumping into game programming. >___>
[/quote]

Im sure the author assumed the library would be linked as part of the project setttings.. but I bet upgrading the solution to vs2010 removed it for no good reason!
I struggled with some of the easier concepts like linking libraries / compiler settings etc early in my game development... I wish more books would cover things like that.

Share this post


Link to post
Share on other sites
wfrye2005    452
[quote name='ArthY303' timestamp='1313703293' post='4850950']
Hi! Look at bottom of [url="http://msdn.microsoft.com/en-us/library/dd144925%28v=vs.85%29.aspx"]this[/url] page on msdn. It tells you that GetStockObject is in Gdi32.lib. So you'll have to link that in your project just like you did before.
[/quote]

Yes! That worked. I hadn't realized msdn had that kind of information... or even thought to look for it there. That should help if any similar issues crop up in the remaining chapters (and I get the feeling they will). Thanks!

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