ExclamationDirectX drawing 2D sprites STUPID EXCEPTION

Posts 110 of 10 · Page 1 of 1
DirectX drawing 2D sprites STUPID EXCEPTION
Ok, so I decided I was gonna practice making a menu with directx9 drawing the 2d parts with directx2dsprites. But everytime I build and debug once it opens I get the exceptions
Code:
First-chance exception at 0x00c82835 in LPAC.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x00c82835 in LPAC.exe: 0xC0000005: Access violation reading location 0x00000000.


So I get those exceptions on this line
Code:
mainSprite->Release();
I have declared my sprite and everything

Code:
	if (FAILED(D3DXCreateSprite(d3dDevice, &mainSprite))) {
		return false;
	}
So I have no idea what is wrong please help
post full code? :3
Quote Originally Posted by Hell_Demon View Post
post full code? :3
main.cpp
Code:
#include "main.h"

//main file

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wnd;

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

	RegisterClassEx(&wnd);
	
	if (!LoadOptions()) {
		messge = "Failed to load options please reinstall.";
		MessageBox(NULL, messge, "Lost, Scared and Confused Error", MB_ICONINFORMATION|MB_SETFOREGROUND);
		return 0;
	}

	hWnd = CreateWindow("wndClass", "Lost, Scared and Confused", WS_SYSMENU,
		CW_USEDEFAULT, CW_USEDEFAULT, fW, fH,
		NULL, NULL, hInstance, NULL);

	
	if (!StartDirect3D(hWnd)) {
		messge = "Failed to start DirectX9, this may be caused by DirectX Runtime not being installed.";
		MessageBox(NULL, messge, "Lost, Scared and Confused Error", MB_ICONINFORMATION|MB_SETFOREGROUND);
		return 0;
	}

	if (!LoadData) {
		messge = "Failed to load models and textures.";
		MessageBox(NULL, messge, "Lost, Scared and Confused Error", MB_ICONINFORMATION|MB_SETFOREGROUND);
		return 0;
	}
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	MSG msg;

	ZeroMemory(&msg, sizeof(msg));

	while (msg.message != WM_QUIT) {
		if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		RenderDirectX();
		Update();
	}
	ReleaseD3D();
	return 0;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message) {
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
main.h
Code:
#include "DataHandler.h"
#include <stdio.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
HWND hWnd;
char *messge;

void Update() {
}

class SpriteHandler {
public:
	void DrawGUI() {
		if (status == 0) {
			mainSprite->SetTransform(&createMatrix(0.0f, 0.0f, fW, fH, 1.0f, 0.8f));
			mainSprite->Draw(main[0], NULL, NULL, NULL, 0xFFFFFFFF);
		}
	}
private:
};
SpriteHandler sh;

void RenderDirectX() {
	d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);
	d3dDevice->BeginScene();
	mainSprite->Begin(D3DXSPRITE_ALPHABLEND);
	sh.DrawGUI();
	mainSprite->End();
	d3dDevice->EndScene();
	d3dDevice->Present(NULL, NULL, NULL, NULL);
}

void ReleaseD3D() {
	d3d->Release();
	d3dDevice->Release();
	mainSprite->Release();
	for (int i = 0; i < 3; i++) {
		main[i]->Release();
	}
}
DataHandler.h
Code:
#include "D3DX.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int status = 0;
LPDIRECT3DTEXTURE9 main[3];
LPD3DXSPRITE mainSprite;

bool LoadTextures() {
	main[0] = NULL;
	main[1] = NULL;
	main[2] = NULL;
	if (FAILED(D3DXCreateTextureFromFile(d3dDevice, "bin/mainer/main800.png", &main[0])))
		return false;

	if (FAILED(D3DXCreateTextureFromFile(d3dDevice, "bin/mainer/main1024.png", &main[1])))
		return false;

	if (FAILED(D3DXCreateTextureFromFile(d3dDevice, "bin/mainer/main1280.png", &main[2])))
		return false;

	return true;
}

bool LoadData() {
	if (FAILED(D3DXCreateSprite(d3dDevice, &mainSprite))) {
		return false;
	}
	if (!LoadTextures()) {
		return false;
	}
	return true;
}

bool LoadOptions() {
	ifstream options ("data/options.txt");
	string bWH;
	if (options.is_open()) {
		getline (options,bWH);
		options.close();
	} else {
		return false;
	}
	baseWH = atoi(bWH.c_str());
	if (baseWH == 0) {
		fW = 800;
		fH = 600;
	} else {
		if (baseWH == 1) {
			fW = 1024;
			fH = 768;
		} else {
			if (baseWH == 2) {
				fW = 1280;
				fH = 1024;
			}
		}
	}
	return true;
}
D3DX.h
Code:
#include <windows.h>
#include <mmsystem.h>
#include <XAnimator_lib.h>

//basics
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3dDevice;
int fW, fH, baseWH;


bool StartDirect3D(HWND hWnd) {
	d3d = Direct3DCreate9(D3D_SDK_VERSION);

	if (!d3d)
		return false;

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	
	
	HRESULT hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
		D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3dDevice);
	if (FAILED(hr)) {
		HRESULT hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3dDevice);
		if (FAILED(hr)) {
			return false;
		}
	}

	d3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

	d3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(0, 0, 0));
	d3dDevice->LightEnable(0, TRUE);
	d3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);

	D3DXMATRIX matProj;
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 800.0f/600.0f, 1.0f, 200.0f );
	d3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

	D3DXMATRIX matView, rotY;
	D3DXMatrixIdentity(&matView);
	D3DXMatrixRotationY(&rotY, 1.0);
	matView = rotY;
	d3dDevice->SetTransform(D3DTS_VIEW, &matView);

	return true;
}

D3DXMATRIX createMatrix(float x, float y, float w, float h, float sA, float sB) {
	D3DXVECTOR2 centre = D3DXVECTOR2(w/2, h/2);
	D3DXVECTOR2 trans = D3DXVECTOR2(x, y);
	float rotation = 0.0f;
	D3DXVECTOR2 scaling(sA, sB);
	D3DXMATRIX matrixR;
	D3DXMatrixTransformation2D(&matrixR, NULL, 0.0, &scaling, &centre, rotation, &trans);
	return matrixR;
}
ps im using C++ visual studio 10
You're reading from a non-existent memory location - 0

Access violation reading location 0x00000000.
Quote Originally Posted by freedompeace View Post
You're reading from a non-existent memory location - 0

Do you honestly believe you are helping the thread starter by reiterating what the exception states? Furthermore, the exception occurs within the Release function, something that the thread starter has no control over.

@PsychicSounds Release resources according to the principle of FILO/LIFO (First in, Last out or Last in, First out).
Quote Originally Posted by Fovea View Post
Do you honestly believe you are helping the thread starter by reiterating what the exception states? Furthermore, the exception occurs within the Release function, something that the thread starter has no control over.

@PsychicSounds Release resources according to the principle of FILO/LIFO (First in, Last out or Last in, First out).
So I call stuff like
Code:
d3dDevice->Release()
and stuff like that in the order that I initialized them?
Quote Originally Posted by Fovea View Post
Do you honestly believe you are helping the thread starter by reiterating what the exception states? Furthermore, the exception occurs within the Release function, something that the thread starter has no control over.

@PsychicSounds Release resources according to the principle of FILO/LIFO (First in, Last out or Last in, First out).
Help or not, if you've seen the sheer amounts of people who post for help here, you'll come to realise that many don't even bother reading what the errors are, they copy paste from the build window, paste it here, and expect us to magically solve their problems.
Quote Originally Posted by freedompeace View Post


Help or not, if you've seen the sheer amounts of people who post for help here, you'll come to realise that many don't even bother reading what the errors are, they copy paste from the build window, paste it here, and expect us to magically solve their problems.
I read the errors just I have no idea what they mean, also I learnt c++ and directx in a month or 2 so I still dont know how certain things work.
Quote Originally Posted by PsychicSounds View Post
So I call stuff like
Code:
d3dDevice->Release()
and stuff like that in the order that I initialized them?
Basically, the DirectX object needs to be alive at least as long as the device, and the device as long as any device objects.

So you release things in the order
Code:
pDeviceObject1->Release()
pDeviceObject2->Release()
pDevice->Release()
pDirectX->Release()
Quote Originally Posted by mmbob View Post


Basically, the DirectX object needs to be alive at least as long as the device, and the device as long as any device objects.

So you release things in the order
Code:
pDeviceObject1->Release()
pDeviceObject2->Release()
pDevice->Release()
pDirectX->Release()
Ok thanks I will try it

Quote Originally Posted by mmbob View Post


Basically, the DirectX object needs to be alive at least as long as the device, and the device as long as any device objects.

So you release things in the order
Code:
pDeviceObject1->Release()
pDeviceObject2->Release()
pDevice->Release()
pDirectX->Release()
Ok so I did it and now my release function at the end of the program looks like this
Code:
void ReleaseD3D() {
	mainSprite->Release();
	for (int i = 0; i < 3; i++) {
		main[i]->Release();
	}
	d3dDevice->Release();
	d3d->Release();
}
but I still get the exceptions?
Posts 110 of 10 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?