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