Sign in to follow this  
sarim

Texture won't load and I get an unhandled exception

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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. Edited by eppo

Share this post


Link to post
Share on other sites

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.

Edited by Cryptiik

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by Cryptiik

Share this post


Link to post
Share on other sites
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) Edited by eppo

Share this post


Link to post
Share on other sites

Ok so new problem now dry.png The program runs fine and the objects render. However when I close the program, there is an exception in the destructor of the Object class. I think it's because it's trying to delete something that was already deleted? Or is it something else? I think I need a way to sequentially delete everything in the game ( i.e Objects, then ObjectManager, then D3DManager ) but I'm not sure how I would do that?

Share this post


Link to post
Share on other sites

Stack objects are destroyed in the reverse order they are created. Objects on the heap can be deleted in any order (although deleting those in reverse-order as well is generally good practice).

 

The D3D9 debug runtime (enabled in dxcpl.exe) can give you insight into unreleased resources.

Share this post


Link to post
Share on other sites

So would dynamically creating Objects, ObjectManager, and D3DManager as pointers be a solution so that I can delete them in the right order manually when the program ends?

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