Sign in to follow this  
zlatko_bre_1985

Sprite Class suggestions

Recommended Posts

HI I am rewritting my sprite classes to a new one. Here is idea : //this are just the sketch classes i havent written anyone //cos i dont want to 4 time redesign my sprite class (this is 3rd) There are 3 classes: Image,Animation,Sprite In image class ill put everything related to the image like: class Image: { RECT rect; //position of sprite in texture int RENDER(); bool rotate; float angle; int Rotate(float ANGLE); ... } In animation class i will put stuff related to series of images like this: //ill make a progie that asks for image number,duration,and #image after this class Animation { int numberOfImages; int currentImage; vector<Image *> Images; //(array) this stores image & attribs vector<int > ImageTimeDuration;//(array) duration of displaying current image vector<int > nextImage;//(array) after timeElapsed > time {currentImage = nextImage.at(currentImage);} dword time; void TimeElapsed() //used for getting time elapsed since last call and to control currentImage; } And finaly Sprite class: class Sprite { int numberOfAnimations; int CurrentAnimation; vector<Animation *> Animations; vector<STD::STRING> AnimationNames;//used for play(string NAME); int play(string NAME); int Play(){Animations.at(CurrentAnimation)->Images.at(currentImage)->RENDER(); } So if you have any sugestions on design,implementation,functions required,... please shout.

Share this post


Link to post
Share on other sites
What language are you writing in?

why not have a simple CSprite class with various methods, variables and functions?

like:

class CSprite
{

private:

int x;
int y;
int z;
int h;
int w;
int d;

int textureID;

pubilic:

void Render();
void Update();
void Animation();
void etcetera.....

};

And within each method have the appropriate code for each. if you were really smart you would have other classes inheriting from the sprite class.

Spencer

Share this post


Link to post
Share on other sites
like spencerholmes says, you should have a cSprite class, and have other stuff inheriting from it. Animation could be easily added with an STL vector, or a linked list, etc. Whatever floats your boat really, without knowing your requirements its kinda hard to make suggestions.

Share this post


Link to post
Share on other sites
I think the way when everything togheter is much more robust then this method.I started to rewrite my classs cos i have to implement basic collision checks one with transparent sprite and one with nontransparent (where whole image is a sprite).So it seemed to me that is a waste of space so i could build animated and static as one class(static would be animated with one frame) and have two such classes one that has trancparency and one that has not.And ill when have time ill implement in only transparent class pixel collisoin detection using masks and that will be unnecesary in nontransparent.I tried with inheriting but ... I dont know i didnt like.

Oooh and I am using c++ with directX.



[Edited by - zlatko_bre_1985 on July 10, 2005 4:56:53 AM]

Share this post


Link to post
Share on other sites
In my animation class I have a std::vector<> of images (like you suggest).

Then in my base sprite class I have a std::map<> of pointers to animations class. This way my sprites can have multiple animations that can be played depending on the state of the sprite.

I also have enums for the sprite state and the sprite direction.

Just something to think about.

Share this post


Link to post
Share on other sites
Quote:
Original post by garyfletcher
In my animation class I have a std::vector<> of images (like you suggest).

Then in my base sprite class I have a std::map<> of pointers to animations class. This way my sprites can have multiple animations that can be played depending on the state of the sprite.

I also have enums for the sprite state and the sprite direction.

Just something to think about.


One thing you might consider is representing animations as a std::vector of image IDs instead - and some way of translating the ID into an image. If you're working with sprite sheets, the ID might translate into a position on the sprite sheet (simple example: let's say we have sheets of n frames, laid out horizontally and all the same size x*y so that the image is 10x*y; to draw the image, you would take in the ID and the source image, and draw the (x*ID, 0, x*(ID+1), y) portion of it.) The same animation data could be made to work with multiple sheets, if they were laid out the same way.

Then, each Sprite would have a pointer to the actual Image (depending on what character is represented by that Sprite), and a pointer to an Animation (depending on what the character is doing), where the Animations would be retrieved from the map. Then every animation sequence can combine with any image, and "just work".

Of course, if your image data isn't quite so regular, you will have to improvise something else. There are a lot of possible designs here :)

Share this post


Link to post
Share on other sites
Early today i tried to edit my post to add this but i fell a sleep so i didnt :( and here it is;

EDIT:
I thinked for an hour now and i think ill need to explain what am i doing and for what i need that classes.I have to make a some presentation in c++ with directX for school project.
Its called "Use of directX in programing games for windows".So i think to start from history of games and to create some replicas of older games(not whole just some part of the games) to show how game development has evolved and where is dx in it.So for that older games ill go truogh "text RPGs"->;Some Static Sprite game (one maze like shooter from commodore is in my mind)->with animated sprite->.. to finish with functions that dx use for optimizng mesh ,.. lets say stuff from the samples that came with directX.Enough of that

And first i tried is that with inheriting but i bailed cos i didnt know what where to put.(if i have sprite images in one file and i wanted to create animation do i have to create a series of cSprites and put them in cAnimations vector and to each cSprite must have his/hers rect struct which you must enter ..._)
So as you may see i am losing my self so could you push me on the right track PLEASE.How to design a Sprite so i can lets say i make cGameObject that has within it cSprite end every frame i call
for (int i =0;i < #gameobjcts;i++){gameobjcts.at(i)->sprite->render()) and to not have too long parameter list)


OK that was unaltered edit from early today.

Ill try with images ID (i saw that is commonly used) but how to implement 2 things :If sprites are not in row column order and collision (to inherited classes handle their collision or to put coll in base class)

Share this post


Link to post
Share on other sites
Following on from what i previously wrote. For example:

Have a CAlien class inheriting from the CSprite class. Within this class you would have an update which would check for movement and input. You could have something like:

if(CInput::Instance()->GetIfKetDown(DIK_SPACE))
{
_x += 0.05;
}

//that would make the character move sideways

or if you had a special key for an animation or a character special move you could use that too.

Within you Render method you would have an the drawing materials. Remember you can have as many methods as you want so you could have a animation render and a normal render.

I would have a CGame class which would take in all or the other classes methods so when calling CAlien you would do something like:

_palien = new CAlien(x, y, z, h, w, d, textureID, this);

and in the main game loop call it.

_palien->Render();

Easy enough!

P.S if you want me to send something that i have done i will and it will show you how to lay it all out neat and organised.

Spencer

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