DirectX crashes my program :(

Started by
10 comments, last by White Scorpion 19 years, 7 months ago
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.
Advertisement
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.
How would I make it so that g_Device isn't NULL ?
I don't know.

What error does CreateDevice return?
D3DERR_INVALIDCALL
Well, there you go.

Just a quick glance at your code... g_Wnd looks suspicious.
Nope, I'm positive about that, g_Wnd is really correct.
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
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.
Install the DX Debug runtimes and put the debug level up to full. Then you'll get told whats invalid in the debug window.

This topic is closed to new replies.

Advertisement