Archived

This topic is now archived and is closed to further replies.

Sprite Class

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

Im currently (trying to) working on a class called CSprite. My aim is to be able to create instances of the class, and call abstract things such as "myObject->NextAnim(TYPE_MOVINGUP)" or whatever else i need. Im using c++ and directdraw7. My question is what kind of private data should i have in there? My main guesses are an LPDDSurface7, bool alive, int x, int y. Im not sure how i would store the animation data. Any help would be appreciated, as well as links to any tutorials and/or other threads with a similar question. thanks!

Share this post


Link to post
Share on other sites
I have been working on the same problem.
My class is still quite dismal.
I have brought quite a few public variables and
made them private with accessor functions.

This really isn''t the best example of what you should do.
But I''m in a learning mode and am very interested in the
replies in this thread.

My sprite class is as such:


  
class SPRITE

{ private:
LPDIRECTDRAWSURFACE7 surface;
BOOL *source_bitmask;
int width, height;
int x_pos, y_pos;
int vel_x, vel_y;
int f_size;
int frame_width, frame_height;
int pixel_hit;
int col_width, col_height;
int col_x_offset, col_y_offset;
RECT *frame_rect;
RECT source_rect;
RECT dest_rect;
DWORD current_time, next_time;

public:
int number_frames, frame_count;
DWORD time_delay;
int value;
BOOL active, bounce, wrap;
BOOL animated, auto_animate;

private:
void Common_Load( int CL_x_pos, int CL_y_pos,
int CL_width, int CL_height,
LPCSTR CL_file_name );
void Create_Bitmask();
USHORT *Get_Surface_Ptr();

public:
void Set(int xp, int yp );
void Draw();
void Move();
short int Rect_Collide ( RECT rect );
short int Point_Collide ( int xp, int yp );
void Make_Hole();
void Rotate( DWORD rotation );
void Destroy();


HRESULT Make( int sprite_x_pos, int sprite_y_pos,
int sprite_width, int sprite_height,
LPCSTR bmp_file_name);

HRESULT Make_Animated( int sprite_x_pos, int sprite_y_pos,
int sprite_frame_width, int sprite_frame_height,
int sprite_frame_number, LPCSTR bmp_file_name );

HRESULT Make_From_Memory( int sprite_x_pos, int sprite_y_pos,
int sprite_width, int sprite_height,
int bitmap_offset_x, int bitmap_offset_y,
HBITMAP &bitmap_bit );

HRESULT Make_Animated_From_Memory( int sprite_x_pos, int sprite_y_pos,
int sprite_frame_width, int sprite_frame_height,
int sprite_frame_number, DWORD bitmap_offset_list [],
HBITMAP &bitmap_bit );


int Get_Pos_X() { return x_pos; };
int Get_Pos_Y() { return y_pos; };
int Get_Vel_X() { return vel_x; };
int Get_Vel_Y() { return vel_y; };

void Set_Pos_X( int px ) { x_pos = px; };
void Set_Pos_Y( int py ) { y_pos = py; };
void Set_Vel_X( int vx ) { vel_x = vx; };
void Set_Vel_Y( int vy ) { vel_y = vy; };

int Get_Width() { return width; };
int Get_Height() { return height; };

friend short int Quick_Collide ( SPRITE *ptr_1, SPRITE *ptr_2 );
friend short int Quick_Collide_NO ( SPRITE *ptr_1, SPRITE *ptr_2 );
friend short int Sprite_Sprite_Collide( SPRITE *ptr_1, SPRITE *ptr_2 );

};//end class SPRITE


short int Quick_Collide ( SPRITE *ptr_1, SPRITE *ptr_2 );
short int Quick_Collide_NO ( SPRITE *ptr_1, SPRITE *ptr_2 );
short int Sprite_Sprite_Collide( SPRITE *ptr_1, SPRITE *ptr_2 );



Don''t take this as an example, it really suxors now.

Guy


Share this post


Link to post
Share on other sites
I have an ''action'' var which determines what animation set runs, each set has a index to an array of graphics and a frame counter.

The graphic array is an array of objects, each object is a large bitmap with a source rect, the source rect is moved around according to the current frame. When you increment the frame it works out looping and returns the new frame.

Other vars are to do with attributes, movement speed, position, items carried etc.

I also have a sound index for each action.

,Jay

Share this post


Link to post
Share on other sites
I have 2 separate Classes CInstance and CSprite, CSprite is the data for a sprite, contains a surface, which in turns contains all the images in a sprite, an array of sequences, a sequence in turn being a struct that contains the name of the sequence as a std::string and a pointer to int that contains a dinamicaly created array of ints, this int array is the order of the frames inside a sequence, for example sequence named "walksouth" should show frames 1,2,3,4 in that order.

So the CSprite Class has a Draw(int nSequence,nFrame,x,y) function which looks for the sequence, the frame, and draws the frame on the specified position, there is also a Draw(char *szSequence,nFrame,x,y) in case you dont want to keep track of sequence numbers.

now the CInstance class keeps the information for one particular instance of the sprite, such as position, speed, state etc, this class contains a pointer to a CSprite object, which points to a particular CSprite object (a monster named Lou would have a pointer to a CSprite that contains monster sequences and images) you set the pointer to the sprite after you have loaded the CSprite with valid data, and after/when you create a new instance:

CSprite Sprite;
Sprite.LoadFrameset("monster.bmp");
CInstance Lou;
Lou.SetSprite(&Sprite);

then CIntance would have a Draw() function which in turns calls CSprite::Draw(...) with the particular information for that instance, this function would be called each frame, or every time the instance needs to be redrawn.

this way you can have a pack of wolves using only one surface, talk about saving memory!.

Share this post


Link to post
Share on other sites