3.2 The lg_sprite class

This video handler is used to display animated objects.

The different sequences are defined by multiple states, added with the add_state method (see below).



lg_sprite Class Definition


class lg_sprite: public lg_video_handler
{
	protected:		
		//whoa, this is some secret stuff...
		//look at the source code for the protected listing....
		
	public:
		lg_sprite();
		~lg_sprite();
		
		int load_sprites(char *fname, int num_frames_x, int num_frames_y, lg_color *transp_color, int flags);
		void add_state(int state, int anim_delay, int flags, int *sequence);
		int get_state();
		void next_frame();
		void prev_frame();
		int get_frame();
		virtual int render();
		virtual char* get_pixel_map ();
		virtual void get_rect_map(lg_rect *rect);		
		virtual void set_state(int state);
		virtual void process_sprites(state_list *p) { }
};


Methods



int load_sprites(char *fname, int num_frames_x, int num_frames_y, lg_color *transp_color, int flags);
char *fname this is the image filename.

int num_frames_x number of sprites per line.

int num_frames_y number of sprites per column.

int flags if 0 is passed, then the whole image will be opaque, and the lg_color *transp_color parameter can be set to NULL. If LF_CO_TRANSPARENT is passed, then lg_color *transp_color will define the color to consider transparent.

This method loads the sprites in memory.

void add_state(int state, int anim_delay, int flags, int *sequence);
int state this is just an id for the state.

int anim_delay this depends on the process_sprites method. Check the process_sprites method below.

int flags unused. Just use 0 for now...

int *sequence this is the frame sequence this state represents. As an example, if you are reading your sprites from an image file that has 3x3 sprites, the first one is numbered 0, the last one on the first line is numbered 2 and the last one in the file is numbered 8. The sequence must end with LGSPRITE_ENDOF_SEQ.

Example:
int my_seq[] = { 0, 1, 2, 3, 4, 5, LGSPRITE_ENDOF_SEQ };
add_state(1, 100, 0, my_seq);

This function adds a state to a sprite.

int get_state();
Returns the current state.

void next_frame();
Changes to the next frame.

void prev_frame();
Changes to the previous frame.

int get_frame();
Returns the current frame.

virtual int render ();
Virtual function. See 2.3 The lg_video_handler class

virtual char* get_pixel_map ();
Virtual function. See 2.3 The lg_video_handler class

virtual void get_rect_map(lg_rect *rect);
Virtual function. See 2.3 The lg_video_handler class

virtual void set_state(int state);
Sets the current state to the state id number int state.

virtual void process_sprites(state_list *p)
This method is called at the beginning of the render method, and it controls the flow of the current state animation. Since this is a virtual function, every class derived from lg_sprite can have its own rule to control the flow of the current animation state. See the section "built-in animation flow sprite classes" below.



Built-in Animation Flow Sprite Classes



The lg_sprite_framebased derived class

class lg_sprite_framebased: public lg_sprite
{
	protected:
		//whoa, this is some secret stuff...
		//look at the source code for the protected listing....
	public:
		lg_sprite_framebased();
		virtual void set_state(int state);
		virtual void process_sprites(state_list *p);
};

When using add_state, the parameter anim_delay parameter refers to how many frames must be printed before changing frames. This count is automatically refreshed every time the render function is called.

The lg_sprite_msecbased derived class

class lg_sprite_msecbased: public lg_sprite
{
	protected:
		//whoa, this is some secret stuff...
		//look at the source code for the protected listing....
		
	public:
		lg_sprite_msecbased();
		void anim_begin();
		virtual void set_state(int state);
		virtual void process_sprites(state_list *p);
};

When using add_state, the parameter anim_delay parameter refers to how much time (in milliseconds) sprite engine must wait in order to change frames.

void anim_begin();

This is used to start the animation.

The lg_sprite_valuebased derived class

class lg_sprite_valuebased: public lg_sprite
{
	protected:
		//whoa, this is some secret stuff...
		//look at the source code for the protected listing....
		
	public:
		lg_sprite_valuebased();
		int get_value();
		void set_value(int v);
		void add_value(int v);
		void sub_value(int v);		
		virtual void set_state(int state);
		virtual void process_sprites(state_list *p);
};

When using add_state, the parameter anim_delay parameter sets a key value. When value is greater than or equal to key value, it's time to change frames.

int get_value();

Returns the current value.

void set_value(int v);

Sets the value to v.

void add_value(int v);

Adds v to value.

void sub_value(int v);

Subtracts v from value.



Back to Home