-
Locating DirectX 9
I'm prepared to be told this is bleedingly obvious, but I'm just testing the waters of DirectX. Code so far:
App.h
Code:
#ifndef APP_H
#define APP_H
#include <Windows.h>
#include <d3d9.h>
//global finctions
//handle any messages from windows
LRESULT CALLBACK wndProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam);
//main entry point
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd);
//global variables
IDirect3D9 *g_pD3D;
IDirect3DDevice9 *g_pD3DDevice;
#define WINDOW_WIDTH 300
#define WINDOW_HEIGHT 200
//error macro debugging
#define ERROR(msg) {MessageBox(NULL,msg,L"Error",MB_OK|MB_ICONEXCLAMATION);}
#endif
WinMain.cpp
Code:
#include "App.h"
#include <Windows.h>
#include <d3d9.h>
LRESULT CALLBACK wndProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//main method
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) wndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = L"DirectXWindow";
wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
RECT rect;
rect.top = (long)0;
rect.left = (long)0;
rect.right = (long)WINDOW_WIDTH;
rect.bottom = (long)WINDOW_HEIGHT;
//REGISTER
if(!RegisterClassEx(&wc))
{
ERROR(L"Error couold not RegisaterClass");
return 0;
}
//calculates the required size of the window
AdjustWindowRectEx(&rect,
WS_OVERLAPPEDWINDOW,
FALSE,
WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
HWND hWindow = CreateWindowEx(NULL,
L"DirectXWidnow",
L"DirectX Window!",
WS_CLIPSIBLINGS | WS_CLIPSIBLINGS,
0,0,
rect.right-rect.left,
rect.bottom-rect.top,
NULL, NULL,
hInstance,
NULL);
if (!hWindow)
{
DestroyWindow(hWindow);
UnregisterClass(L"DirectXWindow", hInstance);
ERROR(L"Window Creation Error.");
return 0;
}
ShowWindow(hWindow,SW_SHOW);
UpdateWindow(hWindow);
SetForegroundWindow(hWindow);
SetFocus(hWindow);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.hDeviceWindow = hWindow;
d3dpp.BackBufferWidth = WINDOW_WIDTH;
d3dpp.BackBufferHeight = WINDOW_HEIGHT;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
//GET A HOLD ON VERSION
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
//CREATE DEVICE
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&g_pD3DDevice)))
{
ERROR(L"Failed to create device");
}
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
g_pD3DDevice->Clear(0,0,
D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,0),
1.0f,0.0f);
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
TranslateMessage (&msg);
DispatchMessage (&msg);
}
g_pD3D->Release();
g_pD3DDevice->Release();
return 0;
}
Gives the message LNK1104 cannot open file d3dx9.lib d3d9.lib winmain.lib
If I use d3dx9.h for the include lines, it is not recognised even though that header file appears to be in the relevant folder.
I am using VC++ 2010 and running DirectX 9 (graphics card won't support HiDef or DirectX 10/11 so not an option yet) I have made a separate program just for windows.h and that works fine, just an empty window.
-
You must link with d3dx9.lib and d3d9.lib. Your include paths should be set to where your DirectX SDK installation is. Likewise your linker paths should be set to the lib/x86 or lib/x64 folder of your DirectX SDK installation. If you want to get more debug information be sure to define D3D_DEBUG_INFO, set your DirectX control panel error level to the 3rd or 4th notch from the left, and link with d3dx9d.lib.
As a side note be sure you install the DirectX redists that came with your SDK to ensure the correct d3dx9_xx.dll is installed in your System32 folder. Don't worry if you have a newer version of DirectX 9, 10, or 11 on your system. DirectX will not install over newer versions of itself but it will install files you might be missing from previous versions that are no longer in newer versions. The d3dx9_xx.dll has had many many revisions over the years and thus there will be a lot of these in your System32 folder.
Also do not use GetMessage(). Use PeekMessage() instead. I also recommend you set every member of the D3DPRESENT_PARAMETERS structure to ensure correct setup. Many people zero it out, set a few params, miss a couple key ones, and then wonder what the problem is. Always set every member of this structure to minimize device setup errors.