/* $Id: sprite.hpp,v 1.11.4.1 2006/01/20 11:23:31 chfreund Exp $ */ #ifndef SPRITE_CLASS_DEFINED #define SPRITE_CLASS_DEFINED /**********************************************************/ #include #include "global.hpp" /**********************************************************/ // make sure that "DBG_LEVEL > 0" switches on "_DEBUG_" #ifdef DBG_LEVEL #if DBG_LEVEL > 0 #ifndef _DEBUG_ #define _DEBUG_ #endif // _DEBUG_ #endif // DBG_LEVEL > 0 #endif // DBG_LEVEL /**********************************************************/ using namespace std; /**********************************************************/ //! wrapper class for the SDL_Surface used as sprite /*! This class is simply a wrapper class for the SD_Surface used as * a sprite.
* In addition to an SDL_Surface a Sprite has got a hot spot. This is * simply the point inside or outside the sprite, that is meant to be * the point, at which the drawn picture is located. For example, if * we have a sprite, that draws a ball with radius r. If we have * computed the screen coordinates (X,Y), of the ball in our application, * we do not want the sprite been drawn with its upper left corner at * (X,Y), but at (X-r,Y-r). The hotspot of our ball would be (r,r). */ class Sprite { public: enum { UNSPECIFIED = 0, // nothing COLORKEY = 1<<0, // colorkey is set CONST_ALPHA = 1<<1, // a global alpha value is set VAR_ALPHA = 1<<2 // image has a variable alpha chanal }; Sprite(); Sprite( const Sprite& sprite ); ~Sprite(); // operators const Sprite& operator = ( const Sprite& sprite ); //! \name access functions //@{ //! returns the width of the image int getWidth() const { return m_Surface ? m_Surface->w : 0; } //! returns the height of the image int getHeight() const { return m_Surface ? m_Surface->h : 0; } //! returns the x-component of the hot spot int getHotSpotX() const { return m_HotSpotX; } //! returns the y-component of the hot spot int getHotSpotY() const { return m_HotSpotY; } //! returns the flags (flags are not yet used!) int getFlags() const { return m_Flags; } //! returns a pointer to the attached SDL_Surface SDL_Surface* getSurface() const { return m_Surface; } //! sets the hot spot's x-component void setHotSpotX( const int x ) { m_HotSpotX = x; } //! sets the hot spot's y-component void setHotSpotY( const int y ) { m_HotSpotY = y; } //! sets the hot spot void setHotSpot( const int x, const int y ) { setHotSpotX( x ); setHotSpotY( y ); } //! bool getRGBA( const int x, const int y, Uint8& red, Uint8& green, Uint8& blue, Uint8& alpha ) const; //@} //! \name bl(i)tting stuff, color key, alpha channal //!@{ //! sets the color key /*! Sets the key color that should be treated as transparent * color. For more details about color key and the parameter * useRLEKey, which triggers the RLE encoding of the color key, * see the documentation of the SDL function SDL_SetColorKey. */ bool setColorKey( const Uint32 key, const bool useRLEKey = true ); //! sets the key color specified with its three color components bool setColorKey( const Uint8 red, const Uint8 green, const Uint8 blue, const bool useRLEKey = true ); //! removes the color key bool removeColorKey(); //! sets an alpha value, constant for the whole sprite bool setAlphaValue( const Uint8 alpha ); //! Uint8 getPixelAlpha( const int x, const int y ) const; //@} //! reset the whole class void reset(); //! loads an image from a file bool loadImage( const char* const filename ); //! converts the SDL_Surface to the display format /*! Converts the SDL_Surface to the current display format. * This method calls the SDL function SDL_DisplayFormatAlpha * (if parameter alpha == true) or SDL_DisplayFormat. * According to the SDL documentation this function is called, * whenever you draw the image for the first time, anyway. * An inappropriate call of one of these SDL functions can * deteriorate the drawing performance significantly. To be * honest, better do not call this function and let SDL * decide, how to treat the image. */ bool optimize( const bool alpha = false ); //! simply a call of optimize(true). bool optimizeAlpha() { return optimize(true); } //! colors all gray pixels of the sprite with the specified color /*! Colors all gray pixels of the sprite with the specified * color, respecting their brightness. "tolerance" specifies * the tolerance for the decition, which pixel is seen as * gray, and which not. If the coloring would lead to a pixel * with the key color, the color is slightly changed, so that * it does not get transparent. * \param red the red component of the intended color [0,255] * \param green the green component of the intended color [0,255] * \param blue the blue component of the intended color [0,255] * \param tolerance a pixel is judged to be gray, if its color * components have all equivalent intensity. But this * should not be tested too strictly, since for example * just the conversion form an image file to the display * format might truncate some pixels' value, that cannot * be recognized as grayscale pixels any more. With the * parameter tolerance the maximal difference of two * color components is specified, to jugdge the color * as a grayscale value. */ bool colorGrayPixels( const Uint8 red, const Uint8 green, const Uint8 blue, const int tolerance = 5 ); bool smooth( const real centerweight = 1.0 ); //! \name drawing //@{ // draw on an arbitrary surface void draw( SDL_Surface* background, const int x, const int y, SDL_Rect* srcRect = NULL ) const; //! draws the sprite onto a second sprite /*! This function that draws a sprite onto another one is quite * undeveloped. It definitely works for sprites using color * keys. In combination with alpha chanals it might lead to * non-intended results, like visible key colors at pixels, * that have been overdrawn with a pixel with alpha value > 0 * and < 0xff. * \param target the "background sprite" * \param x x-coordinate of the target position * \param y y-coordinate of the target position * \param srcRect rectangle, that specifies the part of the * sprite, that should be drawn (default NULL draws * the whole sprite) * \param useHotSpot if true, the hot spot of this sprite is * used for calculating the target position * \param sourceSpecifier not used * \param targetSpecifier not used */ bool draw( Sprite& target, const int x, const int y, SDL_Rect* srcRect = NULL, const bool useHotSpot = false, const int sourceSpecifier = UNSPECIFIED, const int targetSpecifier = UNSPECIFIED ) const; //@} protected: SDL_Surface* m_Surface; //! SDL surface keeping the sprite int m_HotSpotX, //! x coordinate of the hot spot m_HotSpotY; //! x coordinate of the hot spot int m_Flags; //! flags for keeping some state informations (NOT YET USED!) }; /**********************************************************/ class SpriteDummy { public: SpriteDummy() {} SpriteDummy( const SpriteDummy& sprite ) {} ~SpriteDummy() {} // operators const SpriteDummy& operator = ( const Sprite& sprite ) { return *this; } // access functions int getWidth() const { return 0; } int getHeight() const { return 0; } int getHotSpotX() const { return 0; } int getHotSpotY() const { return 0; } int getFlags() const { return 0; } SDL_Surface* getSurface() const { return NULL; } void setHotSpotX( const int x ) {} void setHotSpotY( const int y ) {} void setHotSpot( const int x, const int y ) {} bool getRGBA( const int x, const int y, Uint8& red, Uint8& green, Uint8& blue, Uint8& alpha ) const { return true; } // some settings // functions to set the color key bool setColorKey( const Uint32 key, const bool useRLEKey = true ) { return true; } bool setColorKey( const Uint8 red, const Uint8 green, const Uint8 blue, const bool useRLEKey = true ) { return true; } bool removeColorKey() { return true; } bool setAlphaValue( const Uint8 alpha ) { return true; } Uint8 getPixelAlpha( const int x, const int y ) const { return 0; } // reset the whole class void reset() {} // work functions bool loadImage( const char* const filename ) { return true; } bool optimize( const bool alpha = false ) { return true; } bool optimizeAlpha() { return true; } bool colorGrayPixels( const Uint8 red, const Uint8 green, const Uint8 blue, const int tolerance = 5 ) { return true; } bool smooth( const real centerweight = 1.0 ) { return true; } // drawing // draw on an arbitrary surface void draw( SDL_Surface* background, const int x, const int y, SDL_Rect* srcRect = NULL ) const {} // draw on another sprite bool draw( SpriteDummy& target, const int x, const int y, SDL_Rect* srcRect = NULL, const bool useHotSpot = false, const int sourceSpecifier = 0, const int targetSpecifier = 0 ) const { return true; } protected: }; /**********************************************************/ #endif // SPRITE_CLASS_DEFINED