8 Replies - 3305 Views - Last Post: 20 March 2010 - 08:02 PM Rate Topic: -----

#1 KelliB   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-April 08

DirectX problem, Unable to init Direct3D

Posted 17 March 2010 - 12:33 PM

I'm working with Visual Studio 2008 and the code below is the starting code given to me for this assignment. When I compile it it seems to try to compile and then the window pops up and I get an Error: Unable to init Direct3D. I'm pretty sure I set up the environment right, can anyone tell me how I can fix this error?


/****************************************************************
* example2
* shows the user how to setup a windowed directx application
* which clears the window to a blue color
****************************************************************/
#include <windows.h>

// include directx9
#include <d3d9.h>
#include <d3dx9tex.h>

// global variables
HINSTANCE hInst;							// application instance
HWND wndHandle;								// application window handle

LPDIRECT3D9             pD3D;				// the Direct3D Object
LPDIRECT3DDEVICE9       pd3dDevice;			// the Direct3D Device

////////////////////////////////////////////// forward declarations
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

// DirectX functions
bool initDirect3D();	
void render(void);

/*********************************************************************
* WinMain
*********************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPTSTR lpCmdLine, int nCmdShow)
{
	if (!initWindow(hInstance))
	{
		MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
		return false;
	}

	if (!initDirect3D())
	{
		MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
		return false;
	}

	// Main message loop:
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }		
		else
		{
			render();
		} 
    }

	// release the device and the direct3D object
	if( pd3dDevice != NULL) 
        pd3dDevice->Release();

    if( pD3D != NULL)
        pD3D->Release();
		
	return (int) msg.wParam;
}

/*********************************************************************
* initWindow
*********************************************************************/
bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "DirectXExample";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	// create the window
	wndHandle = CreateWindow("DirectXExample", 
							 "DirectXExample", 
							 WS_OVERLAPPEDWINDOW,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}

/*********************************************************************
* WndProc
*********************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

/*********************************************************************
* initDirect3D
* initializes direct3D
*********************************************************************/
bool initDirect3D()
{
	pD3D = NULL;
	pd3dDevice = NULL;

	// create the directX object
	if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
	{
		return false;
	}

	// fill the presentation parameters structure
	D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount  = 1;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferWidth  = 640;
	d3dpp.hDeviceWindow    = wndHandle;

	// create a default directx device
    if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &pd3dDevice ) ) )
    {
        return false;
    }
	return true;
}

/*********************************************************************
* render
*********************************************************************/
void render(void)
{
	// check to make sure we have a valid Direct3D Device
	if( NULL == pd3dDevice )
        return;

    // Clear the backbuffer to a blue color
    pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

	// Present the backbuffer contents to the display
    pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


Is This A Good Question/Topic? 0
  • +

Replies To: DirectX problem, Unable to init Direct3D

#2 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: DirectX problem, Unable to init Direct3D

Posted 17 March 2010 - 10:25 PM

        if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        {
                return false;
        }

Smells fishy to me. Try:
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(pD3D == 0) {
    return false;
}

Was This Post Helpful? 0
  • +
  • -

#3 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 547
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: DirectX problem, Unable to init Direct3D

Posted 17 March 2010 - 10:31 PM

Hi KelliB, the only thing I can think of is that your DirectX setup is not quite correct. I would try installing it and try again.

View PostPlasticineGuy, on 18 March 2010 - 04:25 AM, said:

        if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        {
                return false;
        }

Smells fishy to me. Try:
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(pD3D == 0) {
    return false;
}


@PlasticineGuy, the code is from a Microsoft example, and is fine.
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: DirectX problem, Unable to init Direct3D

Posted 18 March 2010 - 02:18 AM

Ah, okay. I generally prefer not to rely on things such as return values of operators unless it's not avoidable. I find it makes code harder to read and more error-prone.

This post has been edited by PlasticineGuy: 18 March 2010 - 02:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 KelliB   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-April 08

Re: DirectX problem, Unable to init Direct3D

Posted 18 March 2010 - 12:55 PM

I'll install the DirectX things again.. But I tried to run code that I got to work before and it worked fine.. I'm not sure what is wrong with this.
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: DirectX problem, Unable to init Direct3D

Posted 18 March 2010 - 12:56 PM

Your code works just fine for me. You probably have some problems with your video drivers ...

This post has been edited by sarmanu: 18 March 2010 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 KelliB   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-April 08

Re: DirectX problem, Unable to init Direct3D

Posted 20 March 2010 - 02:14 PM

I also tried to update my video drivers to see if that was the problem, Nothing seems to have worked..
Was This Post Helpful? 0
  • +
  • -

#8 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 547
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: DirectX problem, Unable to init Direct3D

Posted 20 March 2010 - 02:29 PM

Hi KelliB, what was the version of the DirectX SDK you downloaded? The latest version can be found here. Also, are you running an Intel processor, or an AMD?

You need a CoInitializeEx(NULL, COINIT_MULTITHREADED); call at the start of the WinMain, as DirectX uses COM. Also CoUninitialize(); at the end of your WinMain.

I am still investigating this issue.

This post has been edited by Martyn.Rae: 20 March 2010 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 KelliB   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-April 08

Re: DirectX problem, Unable to init Direct3D

Posted 20 March 2010 - 08:02 PM

I did install the newest DirectX SDK, and Intel. Thank you for helping me. What is COM? Sorry I'm pretty new.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1