Jump to content

  • Log In with Google      Sign In   
  • Create Account


why does doing a simple math check cause a LPD3DXSPRITE / ID3DXSPRITE to break


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
5 replies to this topic

#1 ryan20fun   Members   -  Reputation: 707

Like
0Likes
Like

Posted 13 May 2011 - 11:24 PM

Hi All.
im trying to get this collision detection working(it should), but when i try to check for a collision(dont know if it works) and then when i try to render the sprite i get a unhandled exception here is the code that i use to check for collision
bool spritecollide(AnimatedSprite s1, AnimatedSprite s2)
{
	if (( s1.posx > s2.posx ) &&  ( s1.posx < (s2.posx + s2.framesizex )) && ( s1.posy > s2.posy ) && ( s1.posy < s2.posy + s2.framesizey ))
		return true;
	return false;
};

and here is where the unhandled exception occures
result = sprite->Begin(D3DXSPRITE_ALPHABLEND);

ive attached my sprite class, ok, i was told; You aren't permitted to upload this kind of file, so ill just p[aste the code
#ifndef _SPRITE_H__
#define _SPRITE_H__

#include "stdafx.h"
#include "Global.h"
#include <d3dx9.h>

class AnimatedSprite
{
protected:

public:
	AnimatedSprite()
	{
		texture = NULL;
		sprite = NULL;
		posx = 0;
		posy = 0;
		nFrame = 0;
		framesizex = 64;
		framesizey = 64;
		framesizex_cached = 64;
		framesizey_cached = 64;
		fAnimationTimer = 0.0f;
		return;
	};
	AnimatedSprite(float PosX, float PosY)
	{
		texture = NULL;
		sprite = NULL;
		posx = PosX;
		posy = PosY;
		nFrame = 0;
		framesizex = 64;
		framesizey = 64;
		framesizex_cached = 64;
		framesizey_cached = 64;
		fAnimationTimer = 0.0f;
		return;
	};
	~AnimatedSprite()
	{
		Release();
	};
	void Release()
	{
		if( sprite != NULL ) 
		{
			sprite->Release();
			sprite = NULL;
		}
		if( texture != NULL ) 
		{
			texture->Release();
			texture = NULL;
		}
	};
	//-----------------------------------------------------------------------------
	// Loads the Animated Sprite's Texture
	//-----------------------------------------------------------------------------
	HRESULT Load(IDirect3DDevice9 *D3D9Device/*the device to create the texture with*/)
	{
		D3DXIMAGE_INFO d3dxImageInfo;

		D3DXCreateTextureFromFileEx( D3D9Device,
			L"..\\donut.bmp",
			320, // I had to set width manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE.
			384, // I had to set height manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE.
			1,   // Don't create mip-maps when you plan on using D3DXSPRITE. It throws off the pixel math for sprite animation.
			D3DPOOL_DEFAULT,
			D3DFMT_UNKNOWN,
			D3DPOOL_DEFAULT,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, 1.0f),
			&d3dxImageInfo,
			NULL,
			&texture );

		// Create our sprite...
		D3DXCreateSprite( D3D9Device, &sprite );

		return S_OK;
	};
	//-----------------------------------------------------------------------------
	// Updates the Animated Sprite
	//-----------------------------------------------------------------------------
	void Update(float DeltaTime/*a snap*/)
	{
		// Increment the sprite's frame number. Our sprite's animation sequence 
		// consists of 30 frames (0-29).
		fAnimationTimer += DeltaTime;

		if( fAnimationTimer > 0.01f )
		{
			++nFrame;

			if( nFrame > 29 )
				nFrame = 0;

			fAnimationTimer = 0.0f;
		}

		// Slowly move our sprite across the screen. This demonstrates how to use
		// the Draw method of a D3DXSPRITE to place a sprite on the screen.
		// posy += 25 * DeltaTime;

		if(wallwalping) // add code to properly position sprite based on its size.
		{
			if( posy > screenwidth )
				posy = 0;
			else if( posy < 0 )
				posy = screenwidth_cached;
			else if( posx > screenheight_cached )
				posx = 0;
			else if( posx < 0 )
				posx = screenheight_cached;
		}
	};
	//-----------------------------------------------------------------------------
	// Renders the Animated Sprite
	//-----------------------------------------------------------------------------
	HRESULT Render(/*IDirect3DDevice9 *D3D9Device*/)
	{
		// Build a source RECT which will copy only a small portion of the texture.
		srcRect.top	= (( nFrame / 5 ) * framesizey_cached);
		srcRect.left   = (( nFrame % 5 ) * framesizex_cached);
		srcRect.bottom = srcRect.top  + framesizey_cached;
		srcRect.right  = srcRect.left + framesizex_cached;

		D3DXVECTOR3 vCenter(/* 0.0f, 0.0f, 0.0f */ framesizex / 2, framesizey / 2, 0);
		D3DXVECTOR3 vPosition( posy, posx, 0.0f );

		result = sprite->Begin(D3DXSPRITE_ALPHABLEND);
		if(SUCCEEDED(result))
		{
			// This demonstrates how to use the Draw method of a D3DXSPRITE 
			// to place a sprite on the screen.
			sprite->Draw(texture,
				&srcRect,
				&vCenter,
				&vPosition,
				D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, 1.0f));
			
			sprite->End();
		}
		else return result;
		return S_OK;
	};

	HRESULT result;
	LPDIRECT3DTEXTURE9 texture;
	LPD3DXSPRITE sprite;
	float posx;
	float posy;
	float framesizex; // this is used for each frame framesizex / 2 do this once
	float framesizey; // this is used for each frame framesizey / 2 do this once
	long framesizex_cached; // hard coded
	long framesizey_cached; // hard coded

	float fAnimationTimer;

	// Render our sprite, which only uses only a single frame from the donut  
	// texture per update. With this will create an animated sprite.
	int nFrame;
	RECT srcRect;
};

#endif

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

Sponsor:

#2 _Sauce_   Members   -  Reputation: 116

Like
1Likes
Like

Posted 13 May 2011 - 11:34 PM

Have you called AnimatedSprite::Load() before calling AnimatedSprite::Update()? The most likely problem is that sprite is NULL.

It wouldn't hurt to modify AnimatedSprite::Update() as follows;

#include <cassert>

void Update(float DeltaTime)
{
  	assert(sprite);

  	//...
}


#3 Endurion   Crossbones+   -  Reputation: 3461

Like
1Likes
Like

Posted 13 May 2011 - 11:36 PM

Easy: You pass sprite objects as copies into your collision check function. The copies get destroyed after the function finished and release the copied sprite instances. Your original sprite objects now carry dangling pointers to already released sprites.



Short workaround: Change the function signature to:

bool spritecollide( const AnimatedSprite& s1, const AnimatedSprite& s2)



Real workaround: Follow the rule of the three (which might not be too feasible with the sprite pointers.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#4 ryan20fun   Members   -  Reputation: 707

Like
0Likes
Like

Posted 13 May 2011 - 11:40 PM

Easy: You pass sprite objects as copies into your collision check function. The copies get destroyed after the function finished and release the copied sprite instances. Your original sprite objects now carry dangling pointers to already released sprites.



Short workaround: Change the function signature to:

bool spritecollide( const AnimatedSprite& s1, const AnimatedSprite& s2)



Real workaround: Follow the rule of the three (which might not be too feasible with the sprite pointers.


Thanks, its working now, so accessing and useing the varubles i used causes "dangleing pointers" ?
and what is the rule of three ?
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

#5 Endurion   Crossbones+   -  Reputation: 3461

Like
1Likes
Like

Posted 14 May 2011 - 12:23 AM

No, not accessing and using. Having copies made is the problem.

When you pass objects by value the compiler actually creates copies of your variables for spritecollide. Once spritecollide is left these copies are destroyed (calling the destructor releasing the sprites). Because the copies contain the same sprite pointers as your original instances they point to a released object now.



The rule of the three:

If you need any of an assignment operator, a copy constructor or a destructor you need all three (to get proper behaviour for all cases)
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#6 ryan20fun   Members   -  Reputation: 707

Like
0Likes
Like

Posted 14 May 2011 - 12:51 AM

Thanks Alot, i have a C++ book and it never mentioned such a thing(well that i saw)
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.




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