# Shape classes

- sge_shape
- sge_surface
- sge_ssprite
- sge_sprite
- sge_screen

sge_shape
This is an abstract base class. Shapes are something that can be drawn and cleared on surfaces. All classes derived from this class MUST provide the methods below.
Constructor:

> No constructor.

Methods:

> virtual void draw(void)
> Draws the shape.
> virtual void clear(Uint32 color)
> Removes the shape by clearing it to a color.
> virtual void clear(SDL_Surface *src, Sint16 srcX, Sint16 srcY)
> Removes the shape by blitting a part of src over it.
> virtual void UpdateRects(void)
> Updates (SDL/sge_UpdateRect) areas that have been cleared or drawn.
> SDL_Rect get_pos(void)
> Returns the current (maybe undrawn) position.
> SDL_Rect get_last_pos(void)
> Returns the last drawn position.
> Sint16 get_xpos(void)
> Sint16 get_ypos(void)
> Returns the upper left corner of shape.
> Sint16 get_w(void)
> Sint16 get_h(void)
> Returns the width and height of the shape.
> SDL_Surface* get_dest(void)
> Returns the surface on which this class operates on.
> Usefull coords in shape
> These methods returns some useful (current) coords in shape.

>     	NW N NE
>     	W  C  E
>     	SW S SE

> Sint16 c_x(void)
> Sint16 c_y(void)
> Sint16 nw_x(void)
> Sint16 nw_y(void)
> Sint16 n_x(void)
> Sint16 n_y(void)
> Sint16 ne_x(void)
> Sint16 ne_y(void)
> Sint16 e_x(void)
> Sint16 e_y(void)
> Sint16 se_x(void)
> Sint16 se_y(void)
> Sint16 s_x(void)
> Sint16 s_y(void)
> Sint16 sw_x(void)
> Sint16 sw_y(void)
> Sint16 w_x(void)
> Sint16 w_y(void)

sge_surface
Derived (public) from sge_shape.
This is the most basic blitting class. You can draw & clear the image and move it around.
Constructor:

> sge_surface(SDL_Surface *dest, SDL_Surface *src, Sint16 x=0, Sint16 y=0)
> Dest is the surface you want to blit to, src is the image.

Methods:

> virtual void move_to(Sint16 x, Sint16 y)
> Moves the image to a new coord.
> virtual void move(Sint16 x_step, Sint16 y_step)
> Moves the image x/y steps to the left/down (or right/up if negative step size).
> SDL_Surface* get_img(void)
> Returns a pointer to the image.

sge_ssprite
Derived (public) from sge_surface.
The next step is to introduce speeds and multiple frames.
Constructor:

> sge_ssprite(SDL_Surface *screen, SDL_Surface *img, Sint16 x=0, Sint16 y=0)
> Img is the first frame to show.
> sge_ssprite(SDL_Surface *screen, SDL_Surface *img, sge_cdata *cdata, Sint16 x=0, Sint16 y=0)
> As above but with collision data [sge_collision].

Methods:

> void set_vel(Sint16 x, Sint16 y)
> void set_xvel(Sint16 x)
> void set_yvel(Sint16 y)
> Sets the speed (pixels/update).
> Sint16 get_xvel(void)
> Sint16 get_yvel(void)
> Returns the current speed.
> virtual bool update(void)
> Move the sprite according to the speeds. Returns true if the position changed.
> void add_frame(SDL_Surface *img)
> void add_frame(SDL_Surface *img, sge_cdata *cdata)
> Adds a new frame to the sprite, with or without collision data [sge_collision]. This resets the playing sequence.
> void skip_frame(int skips)
> void next_frame(void)
> void prev_frame(void)
> void first_frame(void)
> void last_frame(void)
> Change the current frame. Calling next_frame()/prev_frame() is the same thing as calling skip_frame(1)/skip_frame(-1). First_frame()/last_frame() sets the first/last frame in the sequence as the current frame (but does not change the sequence).
> void set_seq(int start, int stop, playing_mode mode=loop)
> void reset_seq(void)
> sge_ssprite::playing_mode get_PlayingMode(void)
> Changes the frame playing sequence. The default is to loop over all frames (next_frame() returns to the first frame when the last frame has been shown). Use set_seq() to set the start and stop frame (frame 0 is the first frame) and playing mode. You can set the following playing modes:
> - sge_ssprite::loop - loops forever.
> - sge_ssprite::play_once - just once then stops on the last frame.
> Use reset_seq() to reset to the default sequence.
> Get_PlayingMode() returns the current mode or sge_ssprite::stop if all frames in the sequence has been shown (only possible if sge_ssprite::play_once was used).
> void set_border(SDL_Rect box)
> void border_bounce(bool mode)
> void border_warp(bool mode)
> The sprite will bounce at the border of the screen as default. You can change the allowed rectangle with set_border() or turn this off completely with border_bounce(false). You can also make the sprite warp (pixel for pixel) at the border with border_warp(true).
> sge_cdata* get_cdata(void)
> Returns the collision map for the current frame (or NULL if no one exist).
> sge_frame* get_frame(void)
> Returns the frame data for the current frame:

>     struct sge_frame{
>       //The image
>       SDL_Surface *img;

>       //Collision data
>       sge_cdata *cdata;
>     };

> std::list<sge_frame*>* get_list(void)
> The linked list with frames is stored in a STL list<> container. This method returns a pointer to this list. If you change anything in the list you *MUST* call reset_seq()!

sge_sprite
Derived (public) from sge_ssprite.
Finally we add timed operations to the sprite. All speeds are now in pixels/second.
Constructor:

> sge_sprite(SDL_Surface *screen, SDL_Surface *img, Sint16 x=0, Sint16 y=0)
> sge_sprite(SDL_Surface *screen, SDL_Surface *img, sge_cdata *cdata, Sint16 x=0, Sint16 y=0)

Methods:

> void set_pps(Sint16 x, Sint16 y)
> void set_xpps(Sint16 x)
> void set_ypps(Sint16 y)
> void set_fps(Sint16 f)
> Sets the speed (pixels/second). Set_fps() sets how fast (frames/second) the frames should be changed.
> Sint16 get_xpps(void)
> Sint16 get_ypps(void)
> Sint16 get_fps(void)
> Returns the current speeds.
> bool update(Uint32 ticks)
> bool update(void)
> Updates the internal status (calculates the new position and changes the current frame if needed). You can let update() call SDL_GetTicks() itself or provide the information. Returns true if the sprite changed position or frame.
> void pause(void)
> Halt the sprite until next call to update().

sge_screen
This class is not finished and might eat your homework.
This class can be used to draw shapes (sge_shape) on screen. This class will detect if doublebuffering or a hardware screen surface is used and act accordingly.
Constructor:

> sge_screen(SDL_Surface *screen)

Methods:

> void add_rect(SDL_Rect rect)
> void add_rect(Sint16 x, Sint16 y, Uint32 w, Uint32 h)
> Adds an rectangle to be updated (with SDL_UpdateRects()) on update() if needed.
> void add_shape(sge_shape *shape)
> void add_shape_p(sge_shape *shape)
> Adds an sge_shape to be drawn and updated (if needed) on update().
> void remove_shape_p(sge_shape *shape)
> Removes an sge_shape from the permanent list.
> void clear_all(void)
> Clears all shapes (even those added with add_shape_p()) and rectangles.
> void update(void)
> Draws all shapes and updates all rectangles and shapes. All shapes and rectangales are then cleared (beside those added with add_shape_p()) from the class.

Copyright © 1999-2003 Anders Lindström
Last updated 030809