Texture won't load and I get an unhandled exception

Started by
12 comments, last by sarim 11 years, 2 months ago

I'm trying to get a sprite to appear on my screen in the pong game I'm making but there seems to be some sort of bug that occurs. Right after the line that loads the texture executes, I get an unhandled exception. Here's the code:

Object.h


#ifndef OBJECT_H
#define OBJECT_H

#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#include "Graphics.h"

class Object
{

public:

	Object();
	~Object();

	void setTexture( LPDIRECT3DTEXTURE9 t );

	void setPosition( float x, float y );

	LPDIRECT3DTEXTURE9 getTexture()  { return this -> texture; }
	LPD3DXVECTOR3      getPosition() { return this -> position; }

private:

	LPDIRECT3DTEXTURE9 texture;
	LPD3DXVECTOR3      position;
};

#endif  OBJECT_H

Object.cpp


#include "Object.h"
#include "Globals.h"

Object::Object()
{
	texture  = NULL;
	position = NULL;
}

Object::~Object()
{
	RELEASE( texture );
	
	delete position;
	position = NULL;
}

void Object::setTexture( LPDIRECT3DTEXTURE9 t )
{
	texture = t;
}

void Object::setPosition( float x, float y )
{
	position -> x = x;
	position -> y = y;
}

Graphics.h



#ifndef GRAPHICS_H
#define GRAPHICS_H

#include "DxManager.h"
#include <string>

class Graphics
{

public:

	bool InitGraphics( HWND &window );

	void StartScene();
	void Render();
	void EndScene();

	void Update();

	LPDIRECT3DTEXTURE9 LoadTexture( std::string file );

	DxManager getD3DManager() { return this -> D3DManager; }

private:

	DxManager D3DManager;
};

#endif  GRAPHICS_H

Graphics.cpp



#include "Graphics.h"
#include "Globals.h"

bool Graphics::InitGraphics( HWND &window )
{
	if( !D3DManager.InitD3D( window ) )
	{
		GameOver = true;
		MessageBox( window, "Error initializing DirectX9", "Error", MB_OK );
		return false;
	}

	return true;
}

void Graphics::StartScene()
{
	D3DManager.getDirect3DDevice() -> Clear( 0, NULL, D3DCLEAR_TARGET, Black, 0.0f, 0 );

	D3DManager.getDirect3DDevice() -> BeginScene();
	D3DManager.getSpriteObject() -> Begin( D3DXSPRITE_ALPHABLEND );
}

void Graphics::Render()
{

}

void Graphics::EndScene()
{
	D3DManager.getSpriteObject() -> End();
	D3DManager.getDirect3DDevice() -> EndScene();

	D3DManager.getDirect3DDevice() -> Present( NULL, NULL, NULL, NULL );
}

void Graphics::Update()
{
	StartScene();
	Render();
	EndScene();
}

LPDIRECT3DTEXTURE9 Graphics::LoadTexture( std::string file )
{
	LPDIRECT3DTEXTURE9 texture;

	D3DXIMAGE_INFO info;

	D3DXGetImageInfoFromFile( file.c_str(), &info );

	D3DXCreateTextureFromFileEx( 
		D3DManager.getDirect3DDevice(),
		file.c_str(),
		info.Width, info.Height,
		1,
		D3DPOOL_DEFAULT,
		D3DFMT_X8R8G8B8,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		Transparent,
		&info,
		NULL,
		&texture );

	return texture;
}

WinMain.cpp


Graphics g;

	g.InitGraphics( window );

	Object Ball;
	Ball.setTexture( g.LoadTexture( "Resources/Ball.png" ) );
	Ball.setPosition( 500, 100 );

	while ( GameOver != true )
	{
		if ( PeekMessage( &message, NULL, 0, 0, PM_REMOVE ) )
		{
			TranslateMessage( &message );
			DispatchMessage( &message );
		}

		//g.Update();

		g.StartScene();

		g.getD3DManager().getSpriteObject() -> Draw( Ball.getTexture(), NULL, NULL, Ball.getPosition(), White );

		g.EndScene();
	}

Is there anything that I'm doing wrong? Thanks for the help in advance.

Advertisement
You (incorrectly) pass a member of the D3DPOOL enumeration as the Usage argument to D3DXCreateTextureFromFileEx.
This function has a return value that could tell you a lot about what you could be doing wrong.

What should I change it to then? I'm looking at my book that I'm using as a sort of reference and in it they use D3DPOOL_DEFAULT for it. Plus I've set up that LoadTexture() function many time the same exact way and it never gave me an error until now, so I'm wondering what could be causing the problem.

For default usage, use '0' (which also happens to be the value of D3DPOOL_DEFAULT, but it's an unrelated constant).

What is the HRESULT of D3DXCreateTextureFromFileEx?

I just tested it and the HRESULT from D3DXCreateTextureFromFileEx() is S_OK

I had another look at your code, and there's a bug in the Object::setPosition() method. You are dereferencing a null-pointer, 'Object::position'.

You never create the actual vector (in Object's constructor): 'position = new D3DXVECTOR3'.

b.t.w. there's no reason why position should be placed on the heap: LPD3DXVECTOR3 > D3DXVECTOR3;

Ok so I got rid of the LPD3DXVECTOR3 and replaced it with a D3DXVECTOR3 that I create in the constructor. There's no error now after I load the texture however I discovered that the problem now occurs when I try to draw the sprite to the screen. In WinMain the Draw() call causes the unhandled exception, and when I comment that line out, the error stops so im pretty sure there is something wrong with that.

Here's the entire project in case you want to look through the entire code.

Also I discovered that if I don't put anything between g.StartScene() and g.EndScene(), the program runs fine. However as soon as something is put in between, there is an unhandled exception. I think there is something wrong with the D3DXSPRITE object that is in my D3DManager but I can't seem to figure out what.

There's a bug in the Graphics class.

DxManager getD3DManager() { return this -> D3DManager; }

This method returns a copy of the D3DManager object. Because it's a (temporary) copy, its destructor is called causing the D3D device to be released.

You should return a pointer or a reference to the manager:

DxManager &getD3DManager() { return this -> D3DManager; }


(+1 for posting clean/readable code)

Thanks a lot man, that solved my problem :) and you helped me learn something new

This topic is closed to new replies.

Advertisement