Sign in to follow this  
White Scorpion

DirectX crashes my program :(

Recommended Posts

HI everyone, I'm trying to get into 3D programming ( once and for all ) but I've stumbled on a problem, the program crashes when I add dx_Scene() in the sys_MessageLoop(), does someone find something buggy in my code?
#ifndef _DX_MAIN_H_
#define _DX_MAIN_H_

#pragma comment(lib, "D3d8.lib") // Direct 3D 8

#include <d3d8.h>
#include <windows.h>

namespace {
	IDirect3D8* g_pD3D;
	D3DDISPLAYMODE g_DispMode;
	D3DPRESENT_PARAMETERS g_Params;
	IDirect3DDevice8* g_Device;
};

/*********************************************************
**				DirectX-specific functions
*********************************************************/
BOOL dx_InitD3D();
BOOL dx_GetDisplayMode();
BOOL dx_CreateMainDevice();
void dx_Cleanup();
void dx_Scene();

#endif

#include "dx_main.h"
#include "sys_main.h"

BOOL dx_InitD3D()
{
	CoInitialize(NULL); // Initialize COM
	g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
	if(g_pD3D == NULL)
		return FALSE;
	dx_GetDisplayMode();
	dx_CreateMainDevice();
	return TRUE;
}

BOOL dx_GetDisplayMode()
{
	if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &g_DispMode)))
		return FALSE;
	return TRUE;
}

BOOL dx_CreateMainDevice()
{
	ZeroMemory(&g_Params, sizeof(g_Params));
	g_Params.Windowed = TRUE;
	g_Params.SwapEffect = D3DSWAPEFFECT_DISCARD;
	g_Params.BackBufferFormat = g_DispMode.Format;
	
	if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_Wnd,
									D3DCREATE_SOFTWARE_VERTEXPROCESSING,
									&g_Params, &g_Device)))
		return FALSE;
	return TRUE;
}

void dx_Cleanup()
{
	if(g_Device != NULL)
		g_Device->Release();
	if(g_pD3D != NULL)
		g_pD3D->Release();
}

void dx_Scene()
{
	g_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);

	//g_Device->BeginScene();
	//g_Device->EndScene();

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

#include "sys_main.h"
#include "dx_main.h"

int __stdcall WinMain(HINSTANCE hPrevInstance,
					  HINSTANCE hInstance,
					  LPSTR lpszCmdLine,
					  int nCmdShow)
{
	if(!sys_RegisterWindowClass() || !sys_CreateWindow()) {
		MessageBox(NULL, "Program failed on startup", g_Title, MB_ICONEXCLAMATION);
		return 1;
	}
	dx_InitD3D();
	sys_MessageLoop();
	dx_Cleanup();
	sys_Cleanup();
	return 0 ;
}

Besides, does any of you guys know how to do so that IntelliSense finds the function declaration and do its job (finds the parameters of the functions your are currently typing or find the members of a struct/class/namespace.... ) Thank you all PS: Sorry, I know that's a bit too much code to post on a forum but I hope I'm not annoying anybody.

Share this post


Link to post
Share on other sites
You don't check that dx_CreateMainDevice returns true [or much of anything else for that matter] before calling dx_Scene. Likely it doesn't and thus g_Device is null, causing g_Device->Clear to blow up.

Share this post


Link to post
Share on other sites
Perhaps that bit of code may help you ?
#ifndef _DX_MAIN_H_
#define _DX_MAIN_H_

#pragma comment(lib, "D3d8.lib") // Direct 3D 8

#include <d3d8.h>
#include <windows.h>

namespace {
IDirect3D8* g_pD3D;
D3DDISPLAYMODE g_DispMode;
D3DPRESENT_PARAMETERS g_Params;
IDirect3DDevice8* g_Device;
};

/*********************************************************
** DirectX-specific functions
*********************************************************/

BOOL dx_InitD3D();
BOOL dx_GetDisplayMode();
BOOL dx_CreateMainDevice();
void dx_Cleanup();
void dx_Scene();
#endif

Share this post


Link to post
Share on other sites
Heh, I'm not going to go through and debug the code. One of the parameters being passed to CreateDevice is wrong or broken or invalid. Probably your best bet is to toy around with things until it works, or find someone else here who's far more experienced than I.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Install the DX Debug runtimes and put the debug level up to full. Then you'll get told whats invalid in the debug window.
There's more info on that in the Forum FAQ, btw.

You've not set the hDeviceWindow member of the D3DPRESENT_PARAMETERS structure, which may be why it's failing. The debugger output window will tell you though.

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