Jump to content
  • Advertisement
Sign in to follow this  
ryan20fun

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

This topic is 2594 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

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

Share this post


Link to post
Share on other sites
Advertisement
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);

//...
}

Share this post


Link to post
Share on other sites
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:

[color="#000088"]bool spritecollide[color="#666600"]( const [color="#660066"]AnimatedSprite& s1[color="#666600"], const [color="#660066"]AnimatedSprite& s2[color="#666600"])



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

Share this post


Link to post
Share on other sites

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:

[color="#000088"]bool spritecollide[color="#666600"]( const [color="#660066"]AnimatedSprite& s1[color="#666600"], const [color="#660066"]AnimatedSprite& s2[color="#666600"])



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 ?

Share this post


Link to post
Share on other sites
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)

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!