Okay, so I have been programming in C++ and OPENGL for about 10 years but I am just learning Direct3d so I need some help. My program is compiling and linking fine but I am getting a runtime error. Here is the source code and the Problem description follows:
#include <afxwin.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
IDirect3D9 * pD3D;
IDirect3DDevice9 * pD3DDevice;
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
D3DPRESENT_PARAMETERS pDeviceParams;
ZeroMemory(&pDeviceParams, sizeof(D3DPRESENT_PARAMETERS));
if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
{
MessageBox(NULL, "IDirect3D9 object creation failed", "Error", MB_ICONEXCLAMATION | MB_OK);
return E_FAIL;
}
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
pDeviceParams.Windowed = TRUE;
pDeviceParams.hDeviceWindow = hwnd;
pDeviceParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
pDeviceParams.BackBufferFormat = D3DFMT_UNKNOWN;
pDeviceParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
pDeviceParams.EnableAutoDepthStencil = TRUE;
pDeviceParams.AutoDepthStencilFormat = D3DFMT_D16;
if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pDeviceParams, &pD3DDevice)));
{
MessageBox(NULL, "Device Creation Failed\n", "Error", MB_ICONEXCLAMATION | MB_OK);
return E_FAIL;
}
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
In the above source code you should see an if statement that is larger than the rest, that is where the error is occuring. For some reason it always fails when I try to create the IDirect3DDevice9 object. I have tried switching from software, hardware, and mixed vertex processing. None of that works, and every tidbit of code for this that I could find on the internet seems to do it the same way. Can someone please tell me how to fix this, as I said I am new to Direct3d. Thanks You.