• 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
ryan20fun

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

5 posts in this topic

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
[code]
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;
};
[/code]

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

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
[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
[/code]
0

Share this post


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

[code]
#include <cassert>

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

//...
}[/code]
1

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[/color] spritecollide[color="#666600"]( const [/color][color="#660066"]AnimatedSprite&[/color] s1[color="#666600"],[/color] const [color="#660066"]AnimatedSprite&[/color] s2[color="#666600"])



Real workaround: Follow the rule of the three (which might not be too feasible with the sprite pointers.[/color]
1

Share this post


Link to post
Share on other sites
[quote name='Endurion' timestamp='1305351396' post='4810562']
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[/color] spritecollide[color="#666600"]( const [/color][color="#660066"]AnimatedSprite&[/color] s1[color="#666600"],[/color] const [color="#660066"]AnimatedSprite&[/color] s2[color="#666600"])



Real workaround: Follow the rule of the three (which might not be too feasible with the sprite pointers.[/color]
[/quote]

Thanks, its working now, so accessing and useing the varubles i used causes "dangleing pointers" ?
and what is the rule of three ?
0

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)
1

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