Well I have everything so it is easy to debug it gives me a msgbox when something goes wrong. I dont know why it fails to create a device :'(
heres my source.
#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>
#include<string>
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"d3d9.lib")
using namespace std;
bool bAppDone = false;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg, WPARAM wParam, LPARAM lParam);
class Program{
private:
IDirect3D9 *pD3D;
IDirect3DDevice9 *pDevice;
public:
HRESULT InitD3D(bool bWindowed, HWND hwnd){
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(!pD3D){
CatchErrors("Error initializing D3D");
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
if(bWindowed){
RECT rect;
GetWindowRect(hwnd,&rect);
int bbHeight = rect.bottom - rect.top;
int bbWidth = rect.right - rect.left;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
d3dpp.BackBufferHeight = bbHeight;
d3dpp.BackBufferWidth = bbWidth;
d3dpp.hDeviceWindow = hwnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
}
else{
d3dpp.Windowed = FALSE;
}
if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pDevice))){
CatchErrors("Error creating device!");
return E_FAIL;
}
return S_OK;
}
HRESULT ShutdownD3D(){
pD3D->Release();
pD3D = NULL;
if(pD3D != NULL){
CatchErrors("Error shutting down D3D");
return E_FAIL;
}
pDevice->Release();
pDevice = NULL;
if(pDevice != NULL){
CatchErrors("Error shutting down D3D Device");
return E_FAIL;
}
return S_OK;
}
void CatchErrors(string error){
MessageBox(NULL,error.c_str(),"Error",MB_OK);
ShutdownD3D();
}
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, PSTR cmdLine, int iCmd){
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "class";
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW|CS_HREDRAW;
RegisterClass(&wc);
HWND hwnd = CreateWindow("class","window",WS_OVERLAPPEDWINDOW,0,0,500,500,NULL,NULL,hInstance,NULL);
Program program;
program.InitD3D(false,hwnd);
MSG msg;
UpdateWindow(hwnd);
ShowWindow(hwnd, iCmd);
while(!bAppDone){
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
}
}
program.ShutdownD3D();
UnregisterClass("class",hInstance);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg, WPARAM wParam, LPARAM lParam){
switch(iMsg){
case WM_KEYDOWN:
bAppDone = true;
break;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
edit: changed a bit still not working tho:
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
d3dpp.hDeviceWindow = hwnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferCount = 1;
if(bWindowed){
RECT rect;
GetWindowRect(hwnd,&rect);
int bbHeight = rect.bottom - rect.top;
int bbWidth = rect.right - rect.left;
d3dpp.BackBufferHeight = bbHeight;
d3dpp.BackBufferWidth = bbWidth;
d3dpp.Windowed = TRUE;
}
else{
d3dpp.Windowed = FALSE;
}
thnx for any help,
Cory