Directx problem init device???

Started by
3 comments, last by GotenRulezU 17 years, 8 months ago
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
-Goten
Advertisement
I havn't had a close look at your code, since I am at work. I suggest you turn on the debug runtime, that will tell you why it fails.

Go into the directx properties window from control panel. Choose "Use Debug Version of Direct3d" and move the Debug output level all the way to more. then run your program, DirectX will spit a bunch of messages into the output window and will often tell you what the problem is.

That is, of course assuming that you have a new(ish) version of the DirectX Sdk installed and are using Visual Studio.

HTH.
i did as you said(thx for the input) and it spit this out:
Direct3D9: (ERROR) :The specified mode is unsupported. CreateDevice/Reset FailsDirect3D9: (ERROR) :Unable to set the new mode. CreateDevice/Reset FailsDirect3D9: (ERROR) :Failed to initialize primary swapchainDirect3D9: (ERROR) :Failed to initialize Framework Device. CreateDevice Failed.

any ideas what that means?
thx,
-Cory
-Goten
I could be wrong here, but. My first guess would be that the color format is wrong. When you are running in windowed mode the color format must match your desktop.


d3dpp.BackBufferFormat = D3DFMT_R5G6B5;


You are using the R5G6B5 format, which I have never used before. I suggest trying one of the more common ones like X8R8G8B8. Check if that makes any difference.

Other than that, nothing really jumps out at me.

HTH.

Ever done something REALLY STUPID? like in your main say that you have a fullscreen app when you have a windowed one? lmao
i did program.InitD3D(false,hwnd); telling my class that its fullscreen when the window is really windowed lol. how stupid can i be? changed to InitD3D(true,hwnd) and it works lol i'm so retarded.
Thnx for all your guys help,
-Cory
-Goten

This topic is closed to new replies.

Advertisement