DX8 crashing with Unhandled exception: access violation.

Started by
-1 comments, last by Lost 18 years, 1 month ago
The code compiles without any problems, but when I run it I get error message. When I run the debug it says "Unhandled exception in Terrain.exe: 0xC0000005: Access Violation". I'm mostly using this to help speed up the typing & only have switched it to a class handling DX. Can someone help, or at least point me in the right direction please.
//main.cpp
#define WIN32_LEAN_AND_MEAN
#include <afxwin.h>
#include <windows.h>
#include "TEclass.h"

// a simple WndProc that validates a window and kills it when the time comes
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch(iMsg)
	{
	case WM_ERASEBKGND:
			return 0;

		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
	}

	return DefWindowProc(hwnd, iMsg, wParam, lParam);
} // WndProc

// the main purpose of this function is to build a window and call DirectX
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int iCmdShow)
{
	const TCHAR szClassName[] = _T("DirectX 8 Tutorial");

    // build us a window
	WNDCLASSEX wndclass =
	{
		sizeof(wndclass),                   // cbSize
		CS_HREDRAW | CS_VREDRAW,            // style
		WndProc,                            // lpfnWndProc
		0,                                  // cbClsExtra
		0,                                  // cbWndExtra
		hInstance,                          // hInstance
		LoadIcon(NULL, IDI_APPLICATION),    // hIcon
		LoadCursor(NULL, IDC_ARROW),        // hCursor
		NULL,                               // hBackground
		NULL,                               // lpszMenuName
		szClassName,                        // lpszClassName
		LoadIcon(NULL, IDI_APPLICATION)     // hIconSm
	};

	RegisterClassEx(&wndclass);

	HWND hwnd	= CreateWindow(szClassName,           // lpClassName
								szClassName,           // lpWindowName
								WS_OVERLAPPEDWINDOW,   // dwStyle
								CW_USEDEFAULT,         // x
								CW_USEDEFAULT,         // y
								CW_USEDEFAULT,         // nWidth
								CW_USEDEFAULT,         // nHeight
								NULL,                  // hwndParent
								NULL,                  // hMenu
								hInstance,             // hInstance
								NULL);                 // lpParam

    // in a real app, you might want to give an error message here
 //	if(FAILED(InitDirect3D(hwnd)))
 //		return 0;
	TEngine graphics(hwnd);
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while(1)
	{
		// clear out all the messages before we draw a new frame
		MSG msg;
		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(WM_QUIT == msg.message)
			{
//				ShutdownDirect3D();
				return msg.wParam;
			}

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

        // draw a frame and end if we error
		if(FAILED(graphics.DrawScene()))
		{
			DestroyWindow(hwnd);
		}
	}
} // WinMain
//TEclass.h
#include <d3dx8.h>

class TEngine{
public:
	TEngine(HWND hwnd);
	~TEngine();
	HRESULT DrawScene();


private:
	IDirect3D8 * m_pID3D;
	IDirect3DDevice8 * m_pID3DDevice;
	IDirect3DVertexBuffer8 * m_pStreamData;
	IDirect3DIndexBuffer8 * m_pIndexBuffer;
	IDirect3DTexture8 * m_pTexture;
	HRESULT m_DXresult;
};

HRESULT TEngine::DrawScene()
{
	HRESULT hr;
	do
	{
        // clear back buffer
		hr = m_pID3DDevice->Clear(	0, 
															NULL,
															D3DCLEAR_TARGET,
															D3DCOLOR_RGBA(0,63,0,0),
															0,
															0);

		if(FAILED(hr))
			break;

        // start drawing
		hr = m_pID3DDevice->BeginScene();
		if(FAILED(hr))
			break;

        // Put all drawing code here

		hr = m_pID3DDevice->EndScene();
		if(FAILED(hr))
			break;

        // flip back buffer to front
		hr = m_pID3DDevice->Present(NULL, NULL, NULL, NULL);
	} while(0);

	return hr;
}

TEngine::TEngine(HWND hwnd){


	IDirect3D8 * m_pID3D									= NULL;
	IDirect3DDevice8 * m_pID3DDevice			= NULL;
	IDirect3DVertexBuffer8 * m_pStreamData	 = NULL;
	IDirect3DIndexBuffer8 * m_pIndexBuffer		= NULL;
	IDirect3DTexture8 * m_pTexture					 = NULL;


	m_pID3D = Direct3DCreate8(D3D_SDK_VERSION);

	do
	{
		// we need the display mode so we can get
		// the properties of our back buffer
		D3DDISPLAYMODE d3ddm;
		m_DXresult = m_pID3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
		if(FAILED(m_DXresult))
			break;

		D3DPRESENT_PARAMETERS present;
		ZeroMemory(&present, sizeof(present));
		present.SwapEffect       = D3DSWAPEFFECT_COPY;
		present.Windowed         = TRUE;
		present.BackBufferFormat = d3ddm.Format;

		m_DXresult = m_pID3D->CreateDevice(D3DADAPTER_DEFAULT,
								D3DDEVTYPE_HAL,
								hwnd,
								D3DCREATE_SOFTWARE_VERTEXPROCESSING,
								&present,
								&m_pID3DDevice);

		if(FAILED(m_DXresult))
			break;

        // we do our own coloring, so disable lighting
		m_DXresult = m_pID3DDevice->SetRenderState(D3DRS_LIGHTING,
                                         FALSE);

	} while(0);

}


TEngine::~TEngine(){
	if (m_pID3D != NULL) { m_pID3D->Release(); m_pID3D = NULL; }
	if (m_pID3DDevice != NULL) { m_pID3DDevice->Release(); m_pID3DDevice = NULL; }
	if (m_pStreamData != NULL) { m_pStreamData->Release(); m_pStreamData = NULL; }
	if (m_pIndexBuffer != NULL) { m_pIndexBuffer->Release(); m_pIndexBuffer = NULL; }
	if (m_pTexture != NULL) { m_pTexture->Release(); m_pTexture = NULL; }
	return;
}

This topic is closed to new replies.

Advertisement