Sign in to follow this  
Enerjak

Some problem with DirectX code.

Recommended Posts

Ok, for some strange reason, Direct3D can't be created. I made a lib with a class inside it. everything compiled fine. i made a window and put the renderer in it. but for some reason, i get the message box that i programmed into it to tell me about Direct3D not being created.

#include "Cor_Direct3D9.h"


int Cor_Direct3DRenderer::Cor_InitializeDirect3D9(HWND hwnd, int width, int height, bool fullscreen)
{
	//check and see if Direct3D9 is ok.
	Cor_Direct3D9 = Direct3DCreate9(D3D_SDK_VERSION);

	//If it couldn't work, display MessageBox
	if(Cor_Direct3D9 != D3D_OK)
	{
		MessageBoxA(NULL,"Direct3D Could not be created","Error",MB_OK);
		return 0;
	}
	HRESULT result = Cor_Direct3D9->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X8R8G8B8,D3DFMT_X8R8G8B8,true);
	if(FAILED(result))
	{
		MessageBoxA(NULL,"The device type could not be created","Error",MB_OK);
		return 0;
	}

	ZeroMemory(&Cor_PresentParams,sizeof(Cor_PresentParams));

	Cor_PresentParams.Windowed = (!fullscreen);
	Cor_PresentParams.BackBufferCount	= 3;
	Cor_PresentParams.SwapEffect = D3DSWAPEFFECT_COPY;
	Cor_PresentParams.BackBufferWidth = width;
	Cor_PresentParams.BackBufferHeight = height;
	Cor_PresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
	Cor_PresentParams.hDeviceWindow		= hwnd;

	 result = Cor_Direct3D9->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hwnd,
		D3DCREATE_HARDWARE_VERTEXPROCESSING,
		&Cor_PresentParams,
		&Cor_Direct3DDevice9);

	if(FAILED(result))
	{
		MessageBoxA(NULL,"Could not create the device","Error",MB_OK);
		return 0;
	}

	Cor_Direct3DDevice9->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);

	Cor_Direct3DDevice9->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&Cor_BackBuffer);

	return 1;
}

int Cor_Direct3DRenderer::Cor_Sync()
{
	if(Cor_Direct3DDevice9->BeginScene())
	{
		Cor_Direct3DDevice9->EndScene();
	}

	Cor_Direct3DDevice9->Present(NULL,NULL,NULL,NULL);

	return 1;
}


Do you see ANYTHING wrong with this code? what do you think it could be that stops Direct3D9 from being created?

Share this post


Link to post
Share on other sites
Direct3DCreate9() doesn't return a HRESULT, it returns an IDirect3D9*.

D3D_OK is 0. NULL is 0.
Quote:
If successful, this function returns a pointer to an IDirect3D9 interface; otherwise, a NULL pointer is returned.


Share this post


Link to post
Share on other sites
change the if statement to

if(!Cor_Direct3D9)
{
MessageBoxA(NULL,"Direct3D Could not be created","Error",MB_OK);
return 0;
}

Direct3DCreate9
Return Values
If successful, this function returns a pointer to an IDirect3D9 interface; otherwise, a NULL pointer is returned

Cor_Direct3D9 will either be a valid pointer or null

D3D_OK = S_OK
S_OK = 0x00000000L or NULL

Share this post


Link to post
Share on other sites
thanks, i've decided to re-code the lib. I have done it over again and the device and Direct3D creates good but when i run the window with the lib in it also, i get like weird debug errorsa. like it compiles with out an error, but of course i get access vilolations and such. it points to this:



void Cor_CDirect3D9Renderer::Cor_Sync()
{
Cor_Direct3DDevice9->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,0,0),1.0f,1);

if(Cor_Direct3DDevice9->BeginScene())
{
Cor_Direct3DDevice9->EndScene();
}

Cor_Direct3DDevice9->Present(NULL,NULL,NULL,NULL);
}




Do you guys see anything wrong with this code? cause i don't it should run and show a ren screen. But it doesn't show it. when i remove the function from the main loop and works fine. So what do I do?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this