Jump to content

  • Log In with Google      Sign In   
  • Create Account


Texture won't load and I get an unhandled exception


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Cryptiik   Members   -  Reputation: 416

Like
0Likes
Like

Posted 28 January 2013 - 05:03 PM

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.



Sponsor:

#2 eppo   Crossbones+   -  Reputation: 2311

Like
1Likes
Like

Posted 29 January 2013 - 08:59 AM

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, 29 January 2013 - 09:03 AM.


#3 Cryptiik   Members   -  Reputation: 416

Like
0Likes
Like

Posted 29 January 2013 - 03:55 PM

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, 29 January 2013 - 03:57 PM.


#4 eppo   Crossbones+   -  Reputation: 2311

Like
1Likes
Like

Posted 30 January 2013 - 08:05 AM

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?



#5 Cryptiik   Members   -  Reputation: 416

Like
0Likes
Like

Posted 30 January 2013 - 01:47 PM

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


Edited by Cryptiik, 30 January 2013 - 01:47 PM.


#6 eppo   Crossbones+   -  Reputation: 2311

Like
1Likes
Like

Posted 31 January 2013 - 04:53 AM

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;



#7 Cryptiik   Members   -  Reputation: 416

Like
0Likes
Like

Posted 31 January 2013 - 04:15 PM

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.



#8 Cryptiik   Members   -  Reputation: 416

Like
1Likes
Like

Posted 31 January 2013 - 04:44 PM

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.

Attached Files

  • Attached File  Pong.zip   11.25MB   23 downloads

Edited by Cryptiik, 31 January 2013 - 05:20 PM.


#9 eppo   Crossbones+   -  Reputation: 2311

Like
1Likes
Like

Posted 01 February 2013 - 09:01 AM

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, 01 February 2013 - 09:08 AM.


#10 Cryptiik   Members   -  Reputation: 416

Like
0Likes
Like

Posted 01 February 2013 - 01:12 PM

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



#11 Cryptiik   Members   -  Reputation: 416

Like
0Likes
Like

Posted 01 February 2013 - 08:58 PM

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?



#12 eppo   Crossbones+   -  Reputation: 2311

Like
0Likes
Like

Posted 02 February 2013 - 03:48 AM

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.



#13 Cryptiik   Members   -  Reputation: 416

Like
0Likes
Like

Posted 02 February 2013 - 01:24 PM

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?



#14 Cryptiik   Members   -  Reputation: 416

Like
0Likes
Like

Posted 02 February 2013 - 07:24 PM

Nevermind, I found a solution to it I think






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS