Sign in to follow this  
Chad Smith

LPD3DXSPRITE

Recommended Posts

I'm back! :). Well, I am now working on a basic sprite class that uses LPD3DXSPRITE. I have it where it can draw the sprite. Yay...me happy right? Wrong! Why? Because when I go to exit there seems to be a problem. I get a run time error from Visual C++ saying that the program has triggered a break point. Then it shows me the disassembly. I have tried debugging over and over again...and I can't seem to figure out why it's happening. I can tell it's happening at the end, as that is when the break point is triggered, when I exit. I have checked my release functions and that stuff, and they all seem to be fine. Maybe y'all can help me out. I will show y'all the wrapper class, and the demo I created for it. EGLSprite.h
/*EGLSprite
*this is the class for the EGL sprite class, so people can use 2D images
*this will lie in the EGLGraphics namespace
*/

//inclusion guards
#ifndef EGLSPRITE_H_
#define EGLSPRITE_H_

//files that we need to include
#include <string>
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>

namespace EGLGraphics
{
	class EGLSprite
	{
		//public
	public:
		//constructor
		EGLSprite();
		//destructor
		~EGLSprite();

		//Load Function, to load from a disk file
		HRESULT LoadEGLSprite(LPDIRECT3DDEVICE9 pDevice, std::string filename);

		//render function
		HRESULT RenderEGLSprite(LPDIRECT3DDEVICE9 pDevice);

		//delete the sprite
		void ReleaseEGLSprite();

		//get the sprite we are using
		LPD3DXSPRITE GetSprite() {return sprite;}

		//private mode:
	private:
		//texture
		LPDIRECT3DTEXTURE9 spriteTexture;
		//sprite
		LPD3DXSPRITE sprite;		
		std::string filename;

		//sprites center
		D3DXVECTOR3 center;
		D3DXVECTOR3 position;
	};
}

#endif


EGLSprite.cpp
/*EGLSprite.cpp
*class definitions for the EGLSprite
*/

//include the EGLSprite.h 
#include "EGLSprite.h"

//constructor
EGLGraphics::EGLSprite::EGLSprite()
{
	sprite=NULL;
	spriteTexture=NULL;
	center=D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	position=D3DXVECTOR3(50.0f, 50.0f, 0.0f);
}

EGLGraphics::EGLSprite::~EGLSprite()
{
}

HRESULT EGLGraphics::EGLSprite::LoadEGLSprite(LPDIRECT3DDEVICE9 pDevice, std::string filename)
{
	if(!pDevice)
	{
		return -1;
	}

	if(this->sprite)
	{
		sprite->Release();
	}

	if(this->spriteTexture)
	{
		this->spriteTexture->Release();
		this->spriteTexture=NULL;
	}

	D3DXCreateSprite(pDevice, &sprite);

	//create a texture surface from a file
	HRESULT result=D3DXCreateTextureFromFile(pDevice, filename.c_str(), &spriteTexture);

	if(FAILED(result))
	{
		return -1;
	}

	if(FAILED(result))
	{
		return -1;
	}

	return 0;
}


HRESULT EGLGraphics::EGLSprite::RenderEGLSprite(LPDIRECT3DDEVICE9 pDevice)
{
	if(!pDevice)
	{
		return -1;
	}

	HRESULT result;
	result=this->sprite->Draw(this->spriteTexture, NULL, ¢er, &position, D3DCOLOR_XRGB(255, 255, 255));

	if(FAILED(result))
	{
		return -1;
	}
	return 0;
}

void EGLGraphics::EGLSprite::ReleaseEGLSprite()
{
	if(sprite)
	{
		this->sprite->Release();
	}
	sprite=0;

	if(spriteTexture)
	{
		this->spriteTexture->Release();
	}
	spriteTexture=0;
}


Demo File
//this is the demo for the EGL library
//include iostream
#include <iostream>
//first include the EGL.h file, as that is the main file
#include "EGL.h"
//include the EGLGraphics.h file
#include "EGLGraphics.h"

//start main
int main(int argc, char* argv[])
{
	//create a SDL_Event, so we can poll for events and handle input
	SDL_Event evnt;

	//get the current keystates
	Uint8* keystates=NULL;

	//CONSTANTS TO HOLD THE WINDOWS WIDTH AND HEIGHT AND ASPECT RATIO
	const int WIDTH=800;
	const int HEIGHT=600;
	const float ASPECTRATIO=(float)WIDTH/(float)HEIGHT;

	//init the window using the built in InitWindow from EGL's namespace
	if(EGL::InitEGLWindow(800, 600)==false)
	{
		std::cout<<"could not create the window"<<std::endl;
	}
	if(EGL::referenceDevice==NULL)
	{
		std::cout<<"the device is null...we need out..."<<std::endl;
		return 1;
	}

	EGLGraphics::EGLSprite dog;
	dog.LoadEGLSprite(EGL::referenceDevice, "sprite.png");

	//turn lighting off
	EGL::referenceDevice->SetRenderState(D3DRS_LIGHTING, false);

	std::cout<<"window is now inited, and about to enter game loop"<<std::endl;
	//before we loop, make sure are device is not null
	if(EGL::referenceDevice==NULL)
	{
		std::cout<<"device is null...QUIT..."<<std::endl;
		return 1;
	}
	D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
	//aim the camera at the orgin (0, 0, 0)
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	//the y-axis is up
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	//view matrix
	D3DXMATRIX view;
	//define where the camera should look at 
	D3DXMatrixLookAtLH(&view, &position, &target, &up);

	//set to transform to the view matrix now
	EGL::referenceDevice->SetTransform(D3DTS_VIEW, &view);

	//projection matrix
	D3DXMATRIX projection;
	D3DXMatrixPerspectiveFovLH(&projection, D3DXToRadian(90), ASPECTRATIO, 1.0f, 1000.0f);
	//projection transform
	EGL::referenceDevice->SetTransform(D3DTS_PROJECTION, &projection);

	//now loop while we are not done
	while(1)
	{
		//while there is a event the check
		while(SDL_PollEvent(&evnt))
		{
			if(evnt.type==SDL_QUIT)
			{
				return 0;
			}
			break;
		}
		keystates=SDL_GetKeyState(NULL);
		EGL::ClearScreen(EGL::referenceDevice, EGL::ClearRGBColor(0, 255, 255));
		EGL::BeginRender(EGL::referenceDevice);
		//draw the dog
		dog.GetSprite()->Begin(D3DXSPRITE_ALPHABLEND);
		dog.RenderEGLSprite(EGL::referenceDevice);
		dog.GetSprite()->End();
		EGL::EndRender(EGL::referenceDevice);
		EGL::Present(EGL::referenceDevice);
	}
	dog.ReleaseEGLSprite();
	//release the device with a homemade templated release function
	EGL::Release<EGL::EGLDevice>(EGL::referenceDevice);
	return 0;
}


Yes, the sprite renders exactly how I want it to. If I could only now seem to smart enough to figure out why DirectX hates me when I add stuff to my graphics namespace (being sarcastic, as I can...but it seems like all my wrappers in there break at first) but not when I add stuff to anything else (Like...I got my window working just fine, and I did lighting just fine also). hehe. So can anyone help me...again! This gets annoying always asking for help, when something turns out to be so simple. Thanks to all who help! EDIT: well, I started to do more debugging and I finally got it to tell me more about the error. It says that there is a missing operator. Chad.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chad Smith
when I go to exit there seems to be a problem. I get a run time error from Visual C++ saying that the program has triggered a break point. Then it shows me the disassembly.
Sounds like you've taken a trip to memory leak city [smile]

Have you checked the debug output in the IDE? If you've got D3D set to debug mode then you should get a nice long list of leaks with their AllocID that you can use to track the source with.

I forget the details, but there are some CRT functions in VS that allow you to get regular memory leak detection that can be very useful if its not a D3D related leak.

The final trick I picked up a while back is using PIX for Windows - usually you can do a full call capture and step through to the very end of your application and see which object is still marked as 'alive' and work back from there...

hth
Jack

Share this post


Link to post
Share on other sites
Well, I have been trying to get this fixed but that error is still there. When I get home later today, I think I am going to post the Debug information Direct3D is trowing at me, as I am pretty new to Direct3D debug information. Maybe someone could help me? :). After further debuging, and playing around with the Disassembly I have figured out that this is a Operator Error. So, I am going to see if maybe I can look for for something...though I don't know what could be causing a memory leak.

Yea...this is also a BUMP.


Chad.

Share this post


Link to post
Share on other sites
One thing I've noticed is this

if(evnt.type==SDL_QUIT)
{
return 0;
}

prevents this

dog.ReleaseEGLSprite();
//release the device with a homemade templated release function
EGL::Release<EGL::EGLDevice>(EGL::referenceDevice);

from executing when you quit.

I don't know if that would cause your problem, but it's not the best thing.

Share this post


Link to post
Share on other sites
ohh...wow...your right. I can't believe I missed that.

*sits there and hits himself* Again!!!!!! Something so simple and I miss it. Yep, that fixed my problem. I think I need to take a break from this code. I've been looking at it for to long, so maybe that is why I missed that problem.

I fixed it by doing a game loop with a boolean variable. Like I should have been doing in the first place. Thanks man!


Chad.

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