Skip to main content
gamedev.net
🔒 Locked

Problems creating the direct3d object!

Started by SSJCORY 5 replies 500+ views
Original Post
SSJCORY
SSJCORY
I compiled and ran the code from nexe well i wrote it but i wrote it with the assistance of it. It kept exiting instead of leaving the dx window up. So i ran the debugger to cursor and found out that it quits after gpD3D=Direct3DCreate8(D3D_SDK_VERSION); I have no idea why it's not working. It just fails when i check it. Here's my source:

#include<d3dx8.h>
#include<d3d8.h>
HINSTANCE gInstance;
HWND gHwnd;
bool gFullscreen;
IDirect3D8 *gpD3D;
IDirect3DDevice8 *gpD3DD;
HRESULT gResult;
bool D3DInit(){
	gpD3D=Direct3DCreate8(D3D_SDK_VERSION);
	if(gpD3D == NULL)
		return false;
	D3DDISPLAYMODE displaymode;
	if(gFullscreen == false){
		gResult = gpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displaymode);
		if(FAILED(gResult))
			return false;
	}
	else{
		displaymode.Width = 640;
		displaymode.Height = 480;
		displaymode.RefreshRate = 0;
		displaymode.Format = D3DFMT_R5G6B5;
	}
	D3DPRESENT_PARAMETERS D3DPP;
	if(gFullscreen == false){
		D3DPP.Windowed = TRUE;
	}
	else{
		D3DPP.Windowed = FALSE;
	}
	D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
	D3DPP.BackBufferFormat = displaymode.Format;
	D3DPP.BackBufferWidth = displaymode.Width;
	D3DPP.BackBufferHeight = displaymode.Height;
	gResult = gpD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,gHwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&D3DPP,&gpD3DD);
	if(FAILED(gResult))
		return false;
	return true;
}
void DrawScene(){
	gpD3DD->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
	gpD3DD->BeginScene();
	gpD3DD->EndScene();
	gpD3DD->Present(NULL,NULL,NULL,NULL);
}
void D3DRelease(){
	if(gpD3DD)
		gpD3DD->Release();
	gpD3DD = NULL;
	if(gpD3D)
		gpD3D->Release();
	gpD3D = NULL;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam){
	switch(iMsg){
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdLine,int icmd){
	WNDCLASSEX wc;
	MSG msg;
	gInstance = hInstance;
	wc.cbSize         = sizeof(WNDCLASSEX); 
	wc.style			= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc	= WndProc; 
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0; 
	wc.hInstance		= gInstance; 
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION); 
	wc.hCursor		= LoadCursor(NULL, IDC_ARROW); 
	wc.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH); 
	wc.lpszMenuName	= NULL; 
	wc.lpszClassName	= "MyWindow"; 
	wc.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
	if(!RegisterClassEx(&wc))
		return 1;
	if(MessageBox(NULL,"Fullscreen?","FULLSCREEN?",MB_YESNO) == IDYES)
		gFullscreen = true;
	else
		gFullscreen = false;
	if (gFullscreen==false)
	{
		// Create a normal window with a border, a caption, and an X button
		gHwnd = CreateWindowEx(WS_EX_CLIENTEDGE,  
							 "MyWindow",     
							 "MyWindow", 
							 WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE, 
					 		 0, 0 ,
							 640, 480,
							 NULL,
							 NULL,
							 gInstance,  
							 NULL);	
	}
	else
	{
		// Create a fullscreen window, one that doesn't have anything in it
		gHwnd = CreateWindowEx(NULL,  
							 "MyWindow",     
							 "MyWindow", 
							 WS_POPUP | WS_VISIBLE, 
					 		 0, 0,
							 GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
							 NULL,
							 NULL,
							 gInstance,  
							 NULL);		
		

	}
	if (!gHwnd) 
		return 1; 
	if (D3DInit()==false)
	{
		D3DRelease();
		return 2;
	}
	while(1){
		while(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
			if(msg.message == WM_QUIT)
				break;
		}
		if(msg.message == WM_QUIT)
			break;
		if(GetAsyncKeyState(VK_ESCAPE))
			PostQuitMessage(0);
		DrawScene();
	}
	D3DRelease();
	return 0;
}
I've been struggling with directx for a while and i'm just now givin it a try again. I dont know whats wrong with it. Please help. -COry
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
tomek_zielinski
tomek_zielinski
if " gpD3D=Direct3DCreate8(D3D_SDK_VERSION);" is culprit then
1. you don't have DX8 installed
2. reset your computer adn try again - it often helps in sunch situations
www.tmreality.com
SSJCORY
SSJCORY
I have the latest version of directx installed i'm sure...
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
rohitgonsalves
rohitgonsalves
Ok Fine I have made the following changes to your program.

Very Important--> If you are setting only few parameters
of D3DPRESENT_PARAMETERS. then keep habit
of making structure null before you use it.
Otherwise the values not set by you can
kill you.
For making the structure null use
ZeroMemory();

Why not using ShowWindow(hWnd, SW_SHOWDEFAULT); and
UpdateWindow(hWnd);

Your program run on my machine. But I don't know peekMessage
gave me problem so I used GetMessage.

And why to use two while loops If one is enough.

#include<d3dx8.h>#include<d3d8.h>#include <windowsx.h>HINSTANCE gInstance;HWND gHwnd;bool gFullscreen;IDirect3D8 *gpD3D;IDirect3DDevice8 *gpD3DD;HRESULT gResult;bool D3DInit(){	gpD3D = Direct3DCreate8(D3D_SDK_VERSION);		if(gpD3D == NULL)		return false;	D3DDISPLAYMODE displaymode;		if(gFullscreen == false)	{		gResult = gpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displaymode);		if(FAILED(gResult))			return false;	}	else	{		displaymode.Width = 640;		displaymode.Height = 480;		displaymode.RefreshRate = 0;		displaymode.Format = D3DFMT_R5G6B5;	}		D3DPRESENT_PARAMETERS D3DPP;	ZeroMemory( &D3DPP, sizeof(D3DPP) );		if(gFullscreen == false)	{		D3DPP.Windowed = TRUE;	}	else	{		D3DPP.Windowed = FALSE;	}	D3DPP.SwapEffect       = D3DSWAPEFFECT_DISCARD;	D3DPP.BackBufferFormat = displaymode.Format;	D3DPP.BackBufferWidth  = displaymode.Width;	D3DPP.BackBufferHeight = displaymode.Height;	gResult = gpD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,gHwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&D3DPP,&gpD3DD);	if(FAILED(gResult))		return false;	return true;}void DrawScene(){	gpD3DD->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);	if(SUCCEEDED(gpD3DD->BeginScene()))	{		gpD3DD->EndScene();	}	gpD3DD->Present(NULL,NULL,NULL,NULL);}void D3DRelease(){	if(gpD3DD)		gpD3DD->Release();	gpD3DD = NULL;	if(gpD3D)		gpD3D->Release();	gpD3D = NULL;}LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam){	switch(iMsg)	{	/*case WM_PAINT:		DrawScene();		break;*/	case WM_DESTROY:		PostQuitMessage(0);		break;	}	return DefWindowProc(hwnd,iMsg,wParam,lParam);}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdLine,int icmd){	WNDCLASSEX wc;	MSG msg;	gInstance = hInstance;	wc.cbSize         = sizeof(WNDCLASSEX); 	wc.style			= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;	wc.lpfnWndProc	= WndProc; 	wc.cbClsExtra		= 0;	wc.cbWndExtra		= 0; 	wc.hInstance		= gInstance; 	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION); 	wc.hCursor		= LoadCursor(NULL, IDC_ARROW); 	wc.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH); 	wc.lpszMenuName	= NULL; 	wc.lpszClassName	= "MyWindow"; 	wc.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);	if(!RegisterClassEx(&wc))		return 1;	if(MessageBox(NULL,"Fullscreen?","FULLSCREEN?",MB_YESNO) == IDYES)		gFullscreen = true;	else		gFullscreen = false;	if (gFullscreen==false)	{		// Create a normal window with a border, a caption, and an X button		gHwnd = CreateWindowEx(WS_EX_CLIENTEDGE,  							 "MyWindow",     							 "MyWindow", 							 WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE, 					 		 0, 0 ,							 640, 480,							 GetDesktopWindow(),							 NULL,							 gInstance,  							 NULL);		}	else	{		// Create a fullscreen window, one that doesn't have anything in it		gHwnd = CreateWindowEx(NULL,  							 "MyWindow",     							 "MyWindow", 							 WS_POPUP | WS_VISIBLE, 					 		 0, 0,							 GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),							 GetDesktopWindow(),							 NULL,							 gInstance,  							 NULL);					}		if (!gHwnd) 		return 1;		if (D3DInit()!=false)	{		ShowWindow(gHwnd, SW_SHOWDEFAULT);		UpdateWindow(gHwnd);						//while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))		while(GetMessage( &msg, NULL, 0, 0 ))				{			if(msg.message == WM_QUIT)			break;			if(GetAsyncKeyState(VK_ESCAPE))			PostQuitMessage(0);			TranslateMessage(&msg);			DispatchMessage(&msg);			DrawScene();		}	}					D3DRelease();	return 0;} 
MOV AL, 0x00MOV AL, ME
SSJCORY
SSJCORY
I have no idea thats just how they had it on nexe.
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
S1CA
S1CA
There are very few reasons Direct3DCreate8(D3D_SDK_VERSION) would fail.


1a) One reason would be using the header and library files from a newer version of the DirectX SDK without actually having the SDK installed and having an older version of the DirectX runtimes installed. For example if you have the DirectX 8.1 SDK files but only have the DirectX 8.0 runtimes installed.

1b) As an experiment, instead of passing D3D_SDK_VERSION, try passing 200 (which IIRC was the version number for DirectX 8.0)

1c) Did you install the full DirectX SDK using the proper Mictosoft installer?; and was the SDK from a source other than microsoft.com (some people have posted "cut down" versions of the SDK on their websites for smaller download, but have screwed up parts of the installer in the process!).


2) You can check which versions of the DirectX components are actually installed, and whether there are any problems with them by running "DXDIAG" (go to Run... from the Start menu and type in DXDIAG).


3a) To find out WHY DirectX returned NULL from Direct3DCreate8(), switch over to using the DEBUG version of the DirectX SDK and the reason for the error should be sent to your debug output stream:

3b) Go to the Windows Control Panel and open the DirectX applet. If you don't have have a DirectX applet or nothing happens when you double click it, then that's a good sign that something has gone wrong with the installation of the DirectX SDK or something has interfered with it afterwards.

3c) Under the Direct3D tab, select "Use Debug Version of Direct3D", once again, if this is greyed out, then something has gone wrong with the DirectX SDK on your machine.

3d) Drag the "Debug Output Level" slider towards "More".

3e) Next run your program in the debugger (press F5 if you're using MSVC). If you don't have a debugger, you can use DBmon.exe that comes with the DirectX SDK or obtain DebugView from www.sysinternals.com

3f) Once Direct3DCreate8() fails, the REASON WHY THE CALL FAILED will be in the output window of MSVC or in the debug viewer.

3g) IMO using the debug DirectX runtimes to find out why errors were returned is the number 1 thing you should always do if developing with DirectX. Its important enough that the FAQ for this forum includes details, as do popular tutorial sites such as Drunken Hyena's.


4a) Another way you could test whether there is a problem with either the DirectX runtime or the DirectX SDK installation would be to try the sample programs that come with the DirectX SDK:

4b) First try running one of the simpler pre-compiled Direct3D samples (such as the Billboard sample) - if that works, then the DirectX runtime on your machine is working properly.

4c) Next try compiling the same sample yourself from the supplied project files - if that fails, then it indicates the installation of the SDK is at fault. If that works, then there is something stranger going on, and the debug output from your program should provide the clues.
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.