Hey,
I've been working on a project for a while, and had some difficulties at the beginning with the d3dx9.h and lib files, then after some threading here I got the help I needed, or so I thought. It worked well until I decided to make a new project, using the first project as a template. Now all of the sudden, some of the direct 3d functions starts giving linker errors, and I don't know how to fix them.
Some suggests that it has to do with mixing C and C++ others say it's project included directories. Now I turn to you, once again.
I'll give you the code as well as the errors
Note that this code worked before, but now it doesn't(!)
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#include <cstring>
#include <vector>
#define SCREEN_WIDTH 1366
#define SCREEN_HEIGHT 768
#pragma comment (lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
HINSTANCE g_hInst = NULL;
HWND g_hWnd = NULL;
LPDIRECT3D9 d3d; //pointer to d3d interface
LPDIRECT3DDEVICE9 d3ddev; //pointer to device
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; //vertex buffer pointer
LPDIRECT3DINDEXBUFFER9 i_buffer = NULL;
LPDIRECT3DTEXTURE9 texture = NULL; //first texture used for cube
LPDIRECT3DTEXTURE9 test;
LPD3DXFONT game_whisper;
//sprite interface
LPD3DXSPRITE d3dspt;
D3DLIGHT9 light; // create the light struct
D3DLIGHT9 light_point;
int ZOOM = 45;
float CAMX = 0.0f, CAMY = 25.0f;
bool ON = true;
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
void initd3d(HWND hWnd); //initializes the d3d
void cleand3d(); //release and close d3d
void initgraphics(); //start up the engine
void rendering(); //render frames
void initlight(); //initialize lighting
void LoadTextures(); //Texture initializing
void font_creation(); //create fonts
void LoadTexture(LPDIRECT3DTEXTURE9* texture, LPCTSTR filename);
void DrawTexture(LPDIRECT3DTEXTURE9 texture, RECT* texcoords, int x, int y, int a);
//================
static int i = 0;
int tex = 100;
struct MyVertex{
FLOAT x, y, z;
DWORD color;
};
struct MySecondVertex{
FLOAT x, y, z;
D3DVECTOR NORMAL;
FLOAT u, v;
};
void font_creation(){
D3DXCreateFont(
d3ddev,
20,
8,
100,
1,
0,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH || FF_DONTCARE,
"Arial",
&game_whisper);
}
void LoadTexture(LPDIRECT3DTEXTURE9* texture, LPCTSTR filename){
[B]D3DXCreateTextureFromFileEx(d3ddev, // the device pointer
filename, // the file name
D3DX_DEFAULT, // default width
D3DX_DEFAULT, // default height
D3DX_DEFAULT, // no mip mapping
NULL, // regular usage
D3DFMT_A8R8G8B8, // 32-bit pixels with alpha
D3DPOOL_MANAGED, // typical memory handling
D3DX_DEFAULT, // no filtering
D3DX_DEFAULT, // no mip filtering
D3DCOLOR_XRGB(255, 0, 255), // the hot-pink color key
NULL, // no image info struct
NULL, // not using 256 colors
texture); // load to sprite[/B]
return;
}
void LoadTextures(){
}
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE) //not using XYZRFV
#define SECONDCUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1) //using normals and 1 texture
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
return 0;
int i = 0;
initd3d(g_hWnd);
MSG msg = {0};
while( WM_QUIT != msg.message )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
rendering();
}else{
rendering();
}
}
cleand3d();
return ( int )msg.wParam;
}
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = ( HBRUSH ) 0;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "rpgwnd";
wcex.hIconSm = LoadIcon( wcex.hInstance, IDI_APPLICATION );
if( !RegisterClassEx( &wcex ) )
return E_FAIL;
//create window
g_hInst = hInstance;
RECT rc = { 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) };
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
g_hWnd = CreateWindow( "rpgwnd", "Elunar",
WS_POPUP | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
NULL );
if( !g_hWnd )
return E_FAIL;
ShowWindow( g_hWnd, nCmdShow );
return S_OK;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;
switch( message )
{
case WM_PAINT:
{
hdc = BeginPaint( hWnd, &ps );
EndPaint( hWnd, &ps );
}
break;
case WM_MOUSEMOVE:
{
}
break;
case WM_MOUSEWHEEL:
{
switch(GET_WHEEL_DELTA_WPARAM(wParam)){
case -WHEEL_DELTA: //Scroll Up
{if(ZOOM < 90)ZOOM += 5;
tex++;
}
break;
case WHEEL_DELTA: //Scroll Down
{
if(ZOOM > 0)ZOOM -= 5;
tex--;
}
break;
}
}
break;
case WM_LBUTTONDOWN:
{
if( ON ){
ON = false;
}else{
ON = true;
}
d3ddev->SetRenderState(D3DRS_LIGHTING, ON);
}
break;
case WM_RBUTTONDOWN:
break;
case WM_KEYDOWN:
{
int virtual_key = (int)wParam;
int key_bits = (int)lParam;
switch(virtual_key){
case VK_ESCAPE:
PostQuitMessage( 0 );
break;
case VK_LEFT:
CAMX -= 5.0f;
break;
case VK_RIGHT:
CAMX += 5.0f;
break;
case VK_HOME:
break;
case VK_DELETE:
break;
case VK_DOWN:
CAMY += 5.0f;
break;
case VK_UP:
CAMY -= 5.0f;
break;
case VK_NUMPAD7:
break;
case VK_NUMPAD9:
break;
case VK_NUMPAD6:
break;
case VK_NUMPAD4:
break;
case VK_NUMPAD8:
break;
case VK_NUMPAD5:
break;
case VK_RETURN:
{
}
break;
case VK_SPACE:
i = 1;
break;
case 0x57: //W
break;
case 0x41: //A
break;
case 0x53: //S
break;
case 0x44: //D
break;
case 0x51: //Q
break;
case 0x45: //E
break;
default:
break;
}
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
}
return 0;
}
void initd3d(HWND hWnd){
d3d = Direct3DCreate9(D3D_SDK_VERSION); //interface
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp)); //clean up the video memory
d3dpp.Windowed = FALSE; //full screen mode (X)
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //No swapping
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; //format is 32 bit (4*8)
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.EnableAutoDepthStencil = TRUE; //turn on model depth, where the closest pixel to the camera is displayed (stored in a z-buffer)
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; //using 16-bit
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
initgraphics();
initlight();
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); //turn on the lighting
d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //show both sides of the vertex
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // z-buffer
d3ddev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(100,100,100));
d3ddev->SetSamplerState( 0, D3DSAMP_MAXANISOTROPY, 8 );
d3ddev->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC );
d3ddev->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
d3ddev->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
D3DXCreateSprite(d3ddev, &d3dspt); //create direct3d sprite
LoadTextures();
font_creation();
}
void initlight(){
D3DMATERIAL9 material; // create the material struct
ZeroMemory(&light, sizeof(light)); // clear out the light struct for use
ZeroMemory(&light_point, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL; // make the light type 'directional light'
light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set the light's color
light.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
light_point.Type = D3DLIGHT_POINT;
light_point.Diffuse = D3DXCOLOR( 0.5f, 0.5f, 0.5f, 1.0f );
light_point.Position = D3DXVECTOR3( 10.0f, 0.0f, 0.0f );
light_point.Range = 250.0f;
light_point.Attenuation0 = 0.0f;
light_point.Attenuation1 = 0.125f;
light_point.Attenuation2 = 0.0f;
d3ddev->SetLight(0, &light); // send the light struct properties to device light 0
d3ddev->LightEnable(0, FALSE); // turn on light 0
d3ddev->SetLight(1, &light_point);
d3ddev->LightEnable(1, TRUE);
ZeroMemory(&material, sizeof(D3DMATERIAL9)); // clear out the struct for use
material.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); // set diffuse color to white
material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set ambient color to white
d3ddev->SetMaterial(&material); // set the globally-used material to &material
}
void initgraphics(){
D3DXCreateTextureFromFile(d3ddev, "Tiger_and_Dragon,_Yin_and_Yang.jpg", &texture);
//D3DXCreateTextureFromFile(d3ddev, "signature.png", &texture);
MySecondVertex vertices[] = {
//body
{ -3.0f, -3.0f, 3.0f, 0.0f, 0.0f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.0f, },
{ -3.0f, -3.0f, -3.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, }, // Left
{ -3.0f, -3.0f, 3.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, },
{ -3.0f, 3.0f, -3.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, },
{ -3.0f, 3.0f, 3.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, },
};
d3ddev->CreateVertexBuffer(24*sizeof(MySecondVertex),
0,
SECONDCUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL); //create a buffer to hold the vertices with opt info
VOID* pVoid;
v_buffer->Lock(0,0, (void**)&pVoid,0); //grab control over windows to avoid writing over other memory bits
memcpy(pVoid, vertices, sizeof(vertices)); //copy correct vertices to locked memory in video RAM
v_buffer->Unlock(); //give back control over mem block
int indices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 5, 6, // side 2
6, 5, 7,
8, 9, 10, // side 3
10, 9, 11,
12, 13, 14, // side 4
14, 13, 15,
16, 17, 18, // side 5
18, 17, 19,
20, 21, 22, // side 6
22, 21, 23,
};
d3ddev->CreateIndexBuffer(36*sizeof(int),
0,
D3DFMT_INDEX32,
D3DPOOL_MANAGED,
&i_buffer,
NULL); //create device index buffer and give misc info to opt mem
i_buffer->Lock(0,0,(void**)&pVoid,0); //grab control
memcpy(pVoid, indices, sizeof(indices)); //copy to video memory
i_buffer->Unlock(); //give back control
}
void rendering(){
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene(); //begin drawing
d3ddev->SetFVF(SECONDCUSTOMFVF);
D3DXMATRIX matView;
POINT mouse;
GetCursorPos(&mouse);
mouse.x -=SCREEN_WIDTH/2;
mouse.y -=SCREEN_HEIGHT/2;
//changing the invertion first created
mouse.x *=-1;
mouse.y *=-1;
//camera
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3(CAMX, 8.0f, CAMY), //eye
//&D3DXVECTOR3((FLOAT)mouse.x, (FLOAT)mouse.y, 0.0f),//lookat
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f) //Ypos
);
d3ddev->SetTransform(D3DTS_VIEW, &matView); //move to the appointed coordinates and use as a view state
//fov
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(ZOOM), //lens
(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, //how big
1.0f,
500.0f); //view dist (drawing dist)
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
d3ddev->SetStreamSource(0,v_buffer, 0, sizeof(MySecondVertex)); //tell device where we draw from
d3ddev->SetIndices(i_buffer); //give information to primitive
d3ddev->SetTexture(0, texture);
D3DXMATRIX matRotateCube1X;
D3DXMATRIX matRotateCube1Y;
D3DXMATRIX matRotateCube1Z;
D3DXMATRIX matRotateCube180;
D3DXMATRIX matScaling;
D3DXMATRIX matTranslation;
static int ind; //rotator
ind += 1; //increments forever
if(ind >= 360)ind = 1;
D3DXMatrixRotationX(&matRotateCube1X, D3DXToRadian(ind) );
D3DXMatrixRotationY(&matRotateCube1Y, D3DXToRadian(ind) );
D3DXMatrixRotationZ(&matRotateCube1Z, D3DXToRadian(ind) );
D3DXMatrixRotationX(&matRotateCube180, D3DXToRadian(180) );
D3DXMatrixTranslation(&matTranslation, 20, 0, 0);
D3DXMatrixScaling(&matScaling, 0.5f, 0.5f, 0.5f);
d3ddev->SetTransform(D3DTS_WORLD, &(matRotateCube1X*matRotateCube180*matRotateCube1Y)); //rotate the cube in the x axle, then the y axle
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12); //draw the primitive (model)
d3ddev->SetTransform(D3DTS_WORLD, &(matRotateCube1Y*matRotateCube180*matTranslation)); //rotate the cube in the x axle, then the y axle
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12); //draw the primitive (model)
d3dspt->Begin(D3DXSPRITE_ALPHABLEND);
d3dspt->End();
d3ddev->EndScene(); //finished drawing
//calculations go here or before drawing
d3ddev->Present(NULL, NULL, NULL, NULL); //put everything on screen
}
void cleand3d(){
v_buffer->Release(); //zero buffer and return control
i_buffer->Release(); // =||=
texture->Release();
d3ddev->Release(); //release device
d3d->Release(); //release d3d
}
And the Error messages:
1>------ Build started: Project: Elunar Campaign, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol _D3DXCreateFontA@48 referenced in function "void __cdecl font_creation(void)" (?font_creation@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXCreateTextureFromFileExA@56 referenced in function "void __cdecl LoadTexture(struct IDirect3DTexture9 * *,char const *)" (?LoadTexture@@YAXPAPAUIDirect3DTexture9@@PBD@Z)
1>main.obj : error LNK2019: unresolved external symbol _D3DXCreateSprite@8 referenced in function "void __cdecl initd3d(struct HWND__ *)" (?initd3d@@YAXPAUHWND__@@@Z)
1>main.obj : error LNK2019: unresolved external symbol _Direct3DCreate9@4 referenced in function "void __cdecl initd3d(struct HWND__ *)" (?initd3d@@YAXPAUHWND__@@@Z)
1>main.obj : error LNK2019: unresolved external symbol _D3DXCreateTextureFromFileA@12 referenced in function "void __cdecl initgraphics(void)" (?initgraphics@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXMatrixScaling@16 referenced in function "void __cdecl rendering(void)" (?rendering@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXMatrixTranslation@16 referenced in function "void __cdecl rendering(void)" (?rendering@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXMatrixRotationZ@8 referenced in function "void __cdecl rendering(void)" (?rendering@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXMatrixRotationY@8 referenced in function "void __cdecl rendering(void)" (?rendering@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXMatrixRotationX@8 referenced in function "void __cdecl rendering(void)" (?rendering@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXMatrixPerspectiveFovLH@20 referenced in function "void __cdecl rendering(void)" (?rendering@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXMatrixLookAtLH@16 referenced in function "void __cdecl rendering(void)" (?rendering@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _D3DXMatrixMultiply@12 referenced in function "public: struct D3DXMATRIX __thiscall D3DXMATRIX::operator*(struct D3DXMATRIX const &)const " (??DD3DXMATRIX@@QBE?AU0@ABU0@@Z)
1>C:\Users\ShadowScripter\Documents\Visual Studio 2008\Projects\Elunar Campaign\Debug\Elunar Campaign.exe : fatal error LNK1120: 13 unresolved externals
1>Build log was saved at "file://c:\Users\ShadowScripter\Documents\Visual Studio 2008\Projects\Elunar Campaign\Elunar Campaign\Debug\BuildLog.htm"
1>Elunar Campaign - 14 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And the included directories in the project settings are:
Tools -> Options -> Projects and Solutions -> VC++ Directories
Include: C:\Program Files (x86)\Microsoft DirectX SDK (March 2009)\Include
Lib: C:\Program Files (x86)\Microsoft DirectX SDK (March 2009)\Lib\x64
C:\Program Files (x86)\Microsoft DirectX SDK (March 2009)\Lib\x86