Jump to content
  • Advertisement
Sign in to follow this  
directXtreme

Help with 2D game

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

I'm trying to make a 2D game and I was wondering how i would go about deleting sprites while the game is running. or hiding them or something when they get hit with a bullet or something cause I tried releasing them while the game was running but that didn't work out so good. Also, if u can give me some pointers on how to add multiple levels, that be great. I've been having problms developing it. there's still collision to take care of although that's sorta easy. my main concern right now is the deleting of objects while the game is running or hiding them or something. Any feed back will help.

Share this post


Link to post
Share on other sites
Advertisement
Well, assuming when you released the sprite, you still tried to make a draw call on it... yes, that wouldn't work out so well. If you make a draw call on something that has had all its resources released, you're going to get memory access violations.

Look into a data structure to hold all onscreen sprites. STL template classes seem to work well for this type of thing, and require not reinventing the wheel. That will allow you to delete objects from the data structure, and as a result, since you base your render loop on drawing each sprite that is in your data structure...

Or you could just do something along the lines of creating a class that holds a character, which could be as simple as something like this...

class Character{
public:
bool isDead();
some definitions to set the sprite and screen position of this character
some definitions to kill the character etc...

And then you just make a simple check to see if the character "isDead()" before doing a draw call on it...

But hey, I've never programmed a 2D game or anything.

Share this post


Link to post
Share on other sites
the things is I have a struct that holds the positions and width of the sprite and everything about it. The book Jhonathan made really help except that he never went into deleting sprites off the screen while the game was running. So what would I do? should I add a Bool to the sprite struct? any help would be apprirciated.

Share this post


Link to post
Share on other sites
If I may ask: are you trying to delete the character (or item, backdrop, etc.) or the image that represents it? For instance, let's say you were trying to delete a monster, are you deleting an instance of a struct that details the monster's position, stats and the like (and which refers to an image of that monster which would be displayed), or are you deleting the image itself?

Share this post


Link to post
Share on other sites
If you're planning on reusing the image at any point in the same game, don't bother deleting it. Even if you aren't, deleting it probably isn't essential (unless the memory consumption of unused images is high) - deleting them at the end makes more sense, as it keeps your code more organised.

Instead, don't draw the image during each render cycle - but this *may* be harder to make out if you're partly using someone elses (a book, for instance) code, as they may have abstracted the drawing routines away from the obvious places.

Share this post


Link to post
Share on other sites
Quote:
Original post by Thaumaturge
If I may ask: are you trying to delete the character (or item, backdrop, etc.) or the image that represents it? For instance, let's say you were trying to delete a monster, are you deleting an instance of a struct that details the monster's position, stats and the like (and which refers to an image of that monster which would be displayed), or are you deleting the image itself?


I'm deleteing the image of the sprite. Maybe i should unload the sprite struct also. I don't know.

Share this post


Link to post
Share on other sites
Quote:
Original post by MrSparky
If you're planning on reusing the image at any point in the same game, don't bother deleting it. Even if you aren't, deleting it probably isn't essential (unless the memory consumption of unused images is high) - deleting them at the end makes more sense, as it keeps your code more organised.

Instead, don't draw the image during each render cycle - but this *may* be harder to make out if you're partly using someone elses (a book, for instance) code, as they may have abstracted the drawing routines away from the obvious places.


the way to draw the image is to use sprite_handler->Draw() function. The book said to make a vector using the X/Y positions of the sprite then use the draw function to draw it using the ID3DXSprite interface.

Share this post


Link to post
Share on other sites
Quote:
"I'm deleteing the image of the sprite. Maybe i should unload the sprite struct also. I don't know."


Actually, I would say that the opposite is probably a better idea.

Instead of deleting the image, delete the struct - note that the struct itself shouldn't have the image itself contained within it, unless you plan on making changes to individual entities, but should rather point to or have some reference to a single image (or images, in the case of an animation) elsewhere. Of course, in the case of sprites that have multiple direction versions (such as a view from the right, from the back, etc.), just either use some system (such as an offset) to specify the correct image or animation, or have a pointer or other reference for each direction.

Thus you would keep one image or animation for each set of identical objects. For instance, if all health packs look the same you should have one health pack image (or, again, animation) per direction from which a health pack can be viewed in your game.

When you want to delete an individual health pack, delete only the struct that refers to that particular individual, and leave the images to be cleaned up at the end of the game or level.

Share this post


Link to post
Share on other sites
Quote:
Original post by Thaumaturge
Quote:
"I'm deleteing the image of the sprite. Maybe i should unload the sprite struct also. I don't know."


Actually, I would say that the opposite is probably a better idea.

Instead of deleting the image, delete the struct - note that the struct itself shouldn't have the image itself contained within it, unless you plan on making changes to individual entities, but should rather point to or have some reference to a single image (or images, in the case of an animation) elsewhere. Of course, in the case of sprites that have multiple direction versions (such as a view from the right, from the back, etc.), just either use some system (such as an offset) to specify the correct image or animation, or have a pointer or other reference for each direction.

Thus you would keep one image or animation for each set of identical objects. For instance, if all health packs look the same you should have one health pack image (or, again, animation) per direction from which a health pack can be viewed in your game.

When you want to delete an individual health pack, delete only the struct that refers to that particular individual, and leave the images to be cleaned up at the end of the game or level.


see, the way it has it in the book is like this:

Struct SPRITE
{
int x,y;
int width, height;
int movex, movey;
ect....
}

for the image I use LPDIRECT3DTEXTURE9 image;

then I use a function that was in the book:

LPDIRECT3DTEXTURE9 LoadTexture(char *filename, D3DCOLOR transcolor)
{
//the texture pointer
LPDIRECT3DTEXTURE9 texture = NULL;

//the struct for reading bitmap file info
D3DXIMAGE_INFO info;

//standard Windows return value
HRESULT result;

//get width and height from bitmap file
result = D3DXGetImageInfoFromFile(filename, &info);
if (result != D3D_OK)
return NULL;

//create the new texture by loading a bitmap image file
D3DXCreateTextureFromFileEx(
d3ddev, //Direct3D device object
filename, //bitmap filename
info.Width, //bitmap image width
info.Height, //bitmap image height
1, //mip-map levels (1 for no chain)
D3DPOOL_DEFAULT, //the type of surface (standard)
D3DFMT_UNKNOWN, //surface format (default)
D3DPOOL_DEFAULT, //memory class for the texture
D3DX_DEFAULT, //image filter
D3DX_DEFAULT, //mip filter
transcolor, //color key for transparency
&info, //bitmap file info (from loaded file)
NULL, //color palette
&texture ); //destination texture

//make sure the bitmap textre was loaded correctly
if (result != D3D_OK)
return NULL;

return texture;
}

image = LoadTexture("car.bmp",D3DCOLOR_XRGB(255,0,255));

you get the idea.



Share this post


Link to post
Share on other sites
Aah - in that case, how does one specify what a given instance of the SPRITE struct should look like - i.e. what it's a sprite of? Is "image" a member of SPRITE? Or is there only one type of sprite in the game?

If "image" is a member of SPRITE, then that would seem to imply that you load an image from file every time that you create a new sprite, even if you've loaded the same type of sprite previously. If so, then the should be deleted with the struct indeed.

Of course, it may be that something is still trying to access your sprite after the image has been deleted, come to think of it - are you removing the SPRITEs from their vector when they are supposed to be removed from play? If not, your drawing code might be reaching them and trying to access freed memory, which could, I daresay, be the source of your problems...

My apologies for my apparently earlier misunderstanding, however. ^^;

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!