Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Help with book code


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 MisteRnoOrOnha   Members   -  Reputation: 241

Like
0Likes
Like

Posted 12 January 2013 - 01:06 PM

Hi, i'm studying directx with the book "Introduction To 3D Game Programming With DirectX 9" but the sample of the first chapter isn't compiling in Visual Studio C++ Express Edition, the compiler output show this error 

 

1>d3dUtility.obj : error LNK2019: unresolved external symbol "long __stdcall d3d::WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@d3d@@YGJPAUHWND__@@IIJ@Z) referenced in function "bool __cdecl d3d::InitD3D(struct HINSTANCE__ *,int,int,bool,enum _D3DDEVTYPE,struct IDirect3DDevice9 * *)" (?InitD3D@d3d@@YA_NPAUHINSTANCE__@@HH_NW4_D3DDEVTYPE@@PAPAUIDirect3DDevice9@@@Z)

I don't know how to fix this and i think the book code is correct. Can anyone help me?

 



Sponsor:

#2 MisteRnoOrOnha   Members   -  Reputation: 241

Like
0Likes
Like

Posted 12 January 2013 - 03:40 PM

UP!



#3 Indifferent   Members   -  Reputation: 576

Like
0Likes
Like

Posted 12 January 2013 - 03:49 PM

Make sure you've set your project up correctly. Double check the settings you've used, specifically for library linking, against the ones given to you by the book.

You're getting this error because the linker (which is invoked after the compiler) is unable to locate the library containing the Direct3D functions you're attempting to call.

Edited by Indifferent, 12 January 2013 - 03:51 PM.


#4 MisteRnoOrOnha   Members   -  Reputation: 241

Like
0Likes
Like

Posted 12 January 2013 - 03:57 PM

I'm thinking i'm doing this correct, i linked to the correct libraries, but this error don't disappear



#5 Washu   Senior Moderators   -  Reputation: 5369

Like
0Likes
Like

Posted 12 January 2013 - 03:59 PM

That's because you didn't define that function. You've declared it somewhere, but not defined it. As such the linker cannot find the definition for the function and is wondering what you're talking about.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#6 MisteRnoOrOnha   Members   -  Reputation: 241

Like
0Likes
Like

Posted 12 January 2013 - 05:28 PM

Are you talking about the function prototype?



#7 Steve_Segreto   Crossbones+   -  Reputation: 1542

Like
0Likes
Like

Posted 12 January 2013 - 10:08 PM

No the function prototype is the declaration, the definition is the actual body of the function.

 

http://www.cprogramming.com/declare_vs_define.html



#8 MisteRnoOrOnha   Members   -  Reputation: 241

Like
0Likes
Like

Posted 13 January 2013 - 06:19 PM

But this is one possibilitie or can have another errors?



#9 Steve_Segreto   Crossbones+   -  Reputation: 1542

Like
0Likes
Like

Posted 13 January 2013 - 06:41 PM

I don't have the book you mentioned - can you post the code for the definition of d3d::WndProc()?



#10 Jason Z   Crossbones+   -  Reputation: 5164

Like
0Likes
Like

Posted 13 January 2013 - 08:41 PM

Washu and Steve are right - when your d3dUtility object file is being linked to all the other functions that it references, there is a missing implementation to that d3d::WndProc function.  The error you showed is very clear - but the cause of the error could be a number of different things.  Did you get any compilation errors at all?  Are there other libraries that you need to compile first?  Please reference the instructions from the book very clearly and make sure you have followed them to the letter!



#11 MisteRnoOrOnha   Members   -  Reputation: 241

Like
0Likes
Like

Posted 14 January 2013 - 05:43 AM

main.cpp

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    
    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

     //Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

 

 

d3dUtility.cpp

//////////////////////////////////////////////////////////////////////////////////////////////////
// 
// File: d3dUtility.cpp
// 
// Author: Frank Luna © All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 
//
// Desc: Provides utility functions for simplifying common tasks.
//          
//////////////////////////////////////////////////////////////////////////////////////////////////
#include <Windows.h>
#include "d3dUtility.h"

bool d3d::InitD3D(
	HINSTANCE hInstance,
	int width, int height,
	bool windowed,
	D3DDEVTYPE deviceType,
	IDirect3DDevice9** device)
{
	//
	// Create the main application window.
	//

	WNDCLASS wc;

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)d3d::WndProc; 
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
	wc.hCursor       = LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = "Direct3D9App";

	if( !RegisterClass(&wc) ) 
	{
		::MessageBox(0, "RegisterClass() - FAILED", 0, 0);
		return false;
	}
		
	HWND hwnd = 0;
	hwnd = ::CreateWindow("Direct3D9App", "Direct3D9App", 
		WS_EX_TOPMOST,
		0, 0, width, height,
		0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/); 

	if( !hwnd )
	{
		::MessageBox(0, "CreateWindow() - FAILED", 0, 0);
		return false;
	}

	::ShowWindow(hwnd, SW_SHOW);
	::UpdateWindow(hwnd);

	//
	// Init D3D: 
	//

	HRESULT hr = 0;

	// Step 1: Create the IDirect3D9 object.

	IDirect3D9* d3d9 = 0;
    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

    if( !d3d9 )
	{
		::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
		return false;
	}

	// Step 2: Check for hardware vp.

	D3DCAPS9 caps;
	d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);

	int vp = 0;
	if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
		vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	// Step 3: Fill out the D3DPRESENT_PARAMETERS structure.
 
	D3DPRESENT_PARAMETERS d3dpp;
	d3dpp.BackBufferWidth            = width;
	d3dpp.BackBufferHeight           = height;
	d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
	d3dpp.BackBufferCount            = 1;
	d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
	d3dpp.MultiSampleQuality         = 0;
	d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
	d3dpp.hDeviceWindow              = hwnd;
	d3dpp.Windowed                   = windowed;
	d3dpp.EnableAutoDepthStencil     = true; 
	d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
	d3dpp.Flags                      = 0;
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;

	// Step 4: Create the device.

	hr = d3d9->CreateDevice(
		D3DADAPTER_DEFAULT, // primary adapter
		deviceType,         // device type
		hwnd,               // window associated with device
		vp,                 // vertex processing
	    &d3dpp,             // present parameters
	    device);            // return created device

	if( FAILED(hr) )
	{
		// try again using a 16-bit depth buffer
		d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
		
		hr = d3d9->CreateDevice(
			D3DADAPTER_DEFAULT,
			deviceType,
			hwnd,
			vp,
			&d3dpp,
			device);

		if( FAILED(hr) )
		{
			d3d9->Release(); // done with d3d9 object
			::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
			return false;
		}
	}

	d3d9->Release(); // done with d3d9 object
	
	return true;
}

int d3d::EnterMsgLoop( bool (*ptr_display)(float timeDelta) )
{
	MSG msg;
	::ZeroMemory(&msg, sizeof(MSG));

	static float lastTime = (float)timeGetTime(); 

	while(msg.message != WM_QUIT)
	{
		if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}
		else
        {	
			float currTime  = (float)timeGetTime();
			float timeDelta = (currTime - lastTime)*0.001f;

			ptr_display(timeDelta);

			lastTime = currTime;
        }
    }
    return msg.wParam;
}

 

 

d3dUtility.h

 

//////////////////////////////////////////////////////////////////////////////////////////////////
// 
// File: d3dUtility.h
// 
// Author: Frank Luna © All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 
//
// Desc: Provides utility functions for simplifying common tasks.
//          
//////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __d3dUtilityH__
#define __d3dUtilityH__

#include <d3dx9.h>
#include <string>
#include <Windows.h>

namespace d3d
{
	bool InitD3D(
		HINSTANCE hInstance,       // [in] Application instance.
		int width, int height,     // [in] Backbuffer dimensions.
		bool windowed,             // [in] Windowed (true)or full screen (false).
		D3DDEVTYPE deviceType,     // [in] HAL or REF
		IDirect3DDevice9** device);// [out]The created device.

	int EnterMsgLoop( 
		bool (*ptr_display)(float timeDelta));

	LRESULT CALLBACK WndProc(
		HWND hwnd,
		UINT msg, 
		WPARAM wParam,
		LPARAM lParam);

	template<class T> void Release(T t)
	{
		if( t )
		{
			t->Release();
			t = 0;
		}
	}
		
	template<class T> void Delete(T t)
	{
		if( t )
		{
			delete t;
			t = 0;
		}
	}
}

#endif // __d3dUtilityH__


#12 Jason Z   Crossbones+   -  Reputation: 5164

Like
0Likes
Like

Posted 14 January 2013 - 06:02 AM

You still haven't shown the body of the d3d::MsgProc function - you have shown the declaration, but there is no implementation yet.  This is probably the issue you are facing!



#13 MisteRnoOrOnha   Members   -  Reputation: 241

Like
0Likes
Like

Posted 14 January 2013 - 06:45 AM

Where is d3d::MsgProc? The error isn't d3d::WindProc?



#14 TiagoCosta   Crossbones+   -  Reputation: 2347

Like
0Likes
Like

Posted 14 January 2013 - 10:38 AM

You're trying to create 2 windows:
The first one using the function WindowProc which is both declared and defined.
The second one using the function WndProc which is only declared.

Edited by TiagoCosta, 14 January 2013 - 10:44 AM.


#15 MisteRnoOrOnha   Members   -  Reputation: 241

Like
0Likes
Like

Posted 14 January 2013 - 12:04 PM

I removed the d3d::WndProc and compiled without errors. Thanks to all that helped me and sorry for my worry english. biggrin.png



#16 Steve_Segreto   Crossbones+   -  Reputation: 1542

Like
0Likes
Like

Posted 14 January 2013 - 12:41 PM

Glad you got it working, but you're going to have trouble in the future if you don't understand that writing this line in your CPP file:


wc.lpfnWndProc = (WNDPROC)d3d::WndProc;



and this line in your header file:

<ul class="bbcol decimal">LRESULT CALLBACK WndProc(

HWND hwnd,

UINT msg,

WPARAM wParam,

LPARAM lParam);

Is never going to work unless you also add these lines in your CPP file:

<ul class="bbcol decimal">LRESULT CALLBACK WndProc(

HWND hwnd,

UINT msg,

WPARAM wParam,

LPARAM lParam)

{

return 0;

}

Please see this website for more information about what a WndProc is and why you need it for a Windows game:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633573(v=vs.85).aspx

Edited by Steve_Segreto, 14 January 2013 - 12:42 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS