Jump to content
  • Advertisement
Sign in to follow this  
Antonym

Error handling textures

This topic is 3646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okey so I made some code to store and retrieve my loaded textures when needed however it seems to be causing some strange problem.. If anyone can help me figure out what's going on I'd be deeply grateful. No errors show up on the build log but an error window pops up and tells me there has been an unhandled exception on win32.. The sprite class that holds the texture
class Sprite
{
private:
	LPDIRECT3DTEXTURE9 texture;

public:

	Sprite();
	LPDIRECT3DTEXTURE9 get_texture()const;
};

Sprite::Sprite()
{
	texture = NULL;
}

LPDIRECT3DTEXTURE9 Sprite::get_texture() const
{
	return texture;
}






The resource manager class that stores pointers to sprite class instances(the loaded sprites)
class ResourceManager
{
private:
	std::map<std::string, Sprite *> sprite_map;

public:
	Sprite* retrieve_sprite(std::string name);
	void store_sprite (Sprite* sprite, std::string name);
};

Sprite* ResourceManager::retrieve_sprite(std::string name)
{
	Sprite *sprite = sprite_map[name];

	if(sprite != 0) 
		return sprite;

	return NULL;
}

void ResourceManager::store_sprite (Sprite* sprite, std::string name)
{
	sprite_map[name] = sprite;
}

the sprite instance class, an instance of this class is assigned to each created object to determine the sprite it uses and that sprite's current properties(frame, time since last frame was displayed etc..)
struct SpriteInstance
{
	Sprite *sprite;
	int row, column;
	DWORD last_frame;

	SpriteInstance();
	SpriteInstance(Sprite*);
};

SpriteInstance::SpriteInstance()
:
	sprite(NULL),
	row(0),
	column(0),
	last_frame(0)
{}

SpriteInstance::SpriteInstance(Sprite *new_sprite)
:
	sprite(new_sprite),
	row(0),
	column(0),
	last_frame(0)
{}






The render function, it takes a vector of the game objects and passes them one by one to the draw function.
void render(Direct3D direct3d,
			std::vector<Object>* game_objects)
{
	direct3d.start_render();

	Object object;
	std::vector<Object>::iterator it = game_objects->begin();

	for( ; it != game_objects->end(); it++)
	{
		object = *it;
		direct3d.draw_sprite(&object);
	}

	direct3d.end_render();
}






the load function, this one calls the load_sprite function on the direct3d object(from the application object) also creates a sprite object and stores a pointer to it in the map of the resource manager.
void Application::load_sprite(Direct3D direct3d, 
							  std::string sprite_name, 
							  LPCSTR file_name)
{
	Sprite sprite;
	
	direct3d.load_sprite(&sprite, file_name);
	resource_manager.store_sprite(&sprite, sprite_name);
}


The direct3d load_sprite function
void Direct3D::load_sprite(Sprite *sprite, 
						   LPCTSTR file_name)
{
	LPDIRECT3DTEXTURE9 texture = sprite->get_texture();

	D3DXCreateTextureFromFileEx(d3d_device, 
								file_name, 
								D3DX_DEFAULT,
								D3DX_DEFAULT,
								D3DX_DEFAULT,
								NULL, 
								D3DFMT_A8R8G8B8, 
								D3DPOOL_MANAGED, 
								D3DX_DEFAULT, 
								D3DX_DEFAULT, 
								D3DCOLOR_XRGB(255, 0, 255),
								NULL, 
								NULL, 
								&texture);
}






Finally the draw_sprite function where I think the problem is. I commented out the irrelevant lines.
void Direct3D::draw_sprite(Object *object)
{
	SpriteInstance sprite_instance = object->get_sprite_instance();
	LPDIRECT3DTEXTURE9 texture = sprite_instance.sprite->get_texture();
	
//	D3DXVECTOR2 scaling = get_scaling();
//	D3DXVECTOR2 center = get_center();
//	float rotation = object->degrees_to_radians(object->get_rotation());
//	D3DXVECTOR2 position = object->get_position();

//	int size = get_size();
//	RECT frame = get_frame(&sprite_instance, size);

	D3DXMATRIX mat;
	D3DXMatrixTransformation2D(&mat,
							   NULL, 
							   NULL,
							   NULL,
							   NULL,
							   NULL, 
							   NULL);
	
	d3d_sprite->SetTransform(&mat);
    d3d_sprite->Draw(texture, NULL, NULL, NULL, 0xFFFFFFFF);
}






[Edited by - Antonym on December 24, 2008 10:28:02 AM]

Share this post


Link to post
Share on other sites
Advertisement
When you get the exception, what line does it occur on? What's the exact error message? It sounds like you're using a null or invalid pointer.

Also, are you aware you're passing your Direct3D class instance by value to those functions, so it'll get copied and destroyed all over the place (I.e. it must have a correct copy constructor, operator= and destructor)?

Share this post


Link to post
Share on other sites
I am using a debugger although I am not sure how to get info from it.. It sends me to the sprite class and a yellow arrow points at 'return texture'..

Should I pass the application and direct3d objects by address/pointer?

Share this post


Link to post
Share on other sites
Quote:
Original post by Antonym
I am using a debugger although I am not sure how to get info from it.. It sends me to the sprite class and a yellow arrow points at 'return texture'..

Should I pass the application and direct3d objects by address/pointer?
That means the problem is on that line. You can use the watch windows to inspect the values of various pointers and find out if they're valid.

I'd recommend This article by Superpig about using Visual Studio's debugger, the debugger is your most useful tool you can possibly have, and once you learn how to start using it you'll never be able to code without it.

I'd personally pass your D3D object to functions by reference or const reference if I were you; that's the usual way to pass complex types around.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antonym
I read that article but most of the windows and tools I couldnt find..
You can show any hidden windows via the Debug -> Windows menu of Visual Studio while the app is running (Just hit F9 on the first line of WinMain and then hit F5 and the app will run and break at the breakpoint).

Share this post


Link to post
Share on other sites
Make a constant reference to direct3d? You mean something like this? I noticed you don't have to specify in the function prototypes that they have to recieve a constant reference.


#include <windows.h>
#include <windowsx.h>
#include "Application.h"
#include "Direct3D.h"
#include "ResourceManager.h"
#include "Render.h"
#include "Loop.h"

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//Display Window
Application application(640,480);
if(!application.display_window(hInstance, nCmdShow))
return 0;

//Init D3D
Direct3D direct3d;
if(!direct3d.init_d3d(application.get_handle(), application.get_dimensions()))
return 0;

//Reference to direct3D
Direct3D const &direct3d_reference = direct3d;

//Load sprites
load_graphics(application, direct3d_reference);

//Loop
main_loop(application, direct3d_reference);

//Close direct3d
direct3d.close_directx();

return 0;
}

Share this post


Link to post
Share on other sites
Close. The correct syntax is const Direct3D& direct3d_reference = direct3d;

However, the point in references is that you have to change very little to get them working. You should just need to change the function prototypes and it'll "just work". Example:
void render(Direct3D direct3d, std::vector<Object>* game_objects)
should become:
void render(const Direct3D& direct3d, std::vector<Object>* game_objects)

And the rest will just work. If you need the direct3d object to be non-const, then just remove the const.

Share this post


Link to post
Share on other sites
Alright I made the changes however.. I didnt change the prototypes but I compiled and the compiler didnt complain :S, why is this?

Also what do you mean by 'just work' are there other things I should know?

#include <windows.h>
#include <windowsx.h>
#include "Application.h"
#include "Direct3D.h"
#include "ResourceManager.h"
#include "Render.h"
#include "Loop.h"

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//Display Window
Application application(640,480);
if(!application.display_window(hInstance, nCmdShow))
return 0;

//Init D3D
Direct3D direct3d;
if(!direct3d.init_d3d(application.get_handle(), application.get_dimensions()))
return 0;

//Reference to direct3D
const Direct3D &direct3d_reference = direct3d;
const Application &application_reference = application;

//Load sprites
load_graphics(application_reference, direct3d_reference);

//Loop
main_loop(application_reference, direct3d_reference);

//Close direct3d
direct3d.close_directx();

return 0;
}



Edit: when I changed one of the function parameters I did get an error..

1>c:\documents and settings\david\mis documentos\visual studio 2005\projects\dungeon\dungeon\render.cpp(11) : error C2662: 'Application::load_sprite' : cannot convert 'this' pointer from 'const Application' to 'Application &'
1> Conversion loses qualifiers
1>c:\documents and settings\david\mis documentos\visual studio 2005\projects\dungeon\dungeon\render.cpp(12) : error C2662: 'Application::load_sprite' : cannot convert 'this' pointer from 'const Application' to 'Application &'
1> Conversion loses qualifiers
1>WinMain.cpp


void load_graphics(const Application &application, const Direct3D &direct3d)
{
//direct3d object, sprite name(for map), file name
application.load_sprite(direct3d, "ship", "ship.png");
application.load_sprite(direct3d, "missile", "missile.png");
}





Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!