• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sarim

Texture won't load and I get an unhandled exception

13 posts in this topic

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.

0

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
1

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
0

Share this post


Link to post
Share on other sites

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?

1

Share this post


Link to post
Share on other sites

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

Edited by Cryptiik
0

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;

1

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.

0

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
1

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
1

Share this post


Link to post
Share on other sites

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

0

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?

0

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.

0

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?

0

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  
Followers 0