Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

CSPicture.h

Go to the documentation of this file.
00001 #ifndef CSPicture_h
00002 #define CSPicture_h
00003 
00004 // USES SDL_Surface
00005 // USES SDL_Rect 
00006 
00007 #include <string>
00008 #include <vector>
00009 #include "SDLMain.h"
00010 #include "CSLoadable.h"
00011 #include "CSLog.h"
00012 
00013 #ifdef WIN32
00014 #pragma warning(disable : 4786 )
00015 #endif
00016 
00017 class CSBitmap;
00018 class CSPicture;
00019 class CSPictureLoader;
00020 class CSPictureDirectLoader;
00021 
00022 typedef std::vector<CSPicture *> CSPictures;
00023 
00024 /** Class that holds DisplayValues, that are used by each CSPicture.
00025   * Thru this class (structure)
00026   * we have some sort of abstraction where we put something
00027   * on the screen - or on some world coordinates
00028   */
00029 class CSDisplayParams
00030 {
00031     public:
00032         int mXDisplayStart; ///< X startoffset of surface destination (0,0 of the blitting)
00033         int mYDisplayStart; ///< Y startoffset of surface destination (0,0 of the blitting)
00034         int mXWorldStart;   ///< X where the display of the "world" starts
00035         int mYWorldStart;   ///< Y where the display of the "world" starts
00036         int mXPos;          ///< X actual position of this picture in the world
00037         int mYPos;          ///< Y actual position of this picture in the world
00038 };
00039 
00040 const int RLE_YES = 1;
00041 const int RLE_NO = 0;
00042 const int RLE_PERHAPS = 2; // other
00043 
00044 /** Data class that is used for loading a loadable class.
00045   * First the data is loaded via xml into this "DATA"-class.
00046   * From this data - the actual picture is created.
00047   * The data can be reused to create another instance of a
00048   * picture class - though probably not needed...
00049   */
00050 class CSPictureData
00051 {
00052 
00053     public:
00054         SDL_Rect position;  ///< has position and size
00055         char *id;
00056         char *filename;     ///< of bitmap file
00057         int rleUse;
00058         int alpha;
00059 
00060         CSPictureData()
00061         {
00062             rleUse = RLE_PERHAPS;
00063             id = 0;
00064             filename = 0;
00065             position.x=0;
00066             position.y=0;
00067             position.w=0;
00068             position.h=0;
00069             alpha = -1;
00070         }
00071 
00072         ~CSPictureData()
00073         {
00074             if (id != 0) 
00075             {
00076                 free(id);
00077                 id = 0;
00078             }
00079             if (filename != 0) 
00080             {
00081                 free(filename);
00082                 filename = 0;
00083             }
00084         }
00085 };
00086 /** 
00087     CSPicture
00088       CSPicture can only be created thru a loader (CSPictureLoader).
00089       CSPicture are defined by (now ONLY thru) an xml-File, like:
00090         DTD:
00091 \verbatim
00092             <!ELEMENT PICTURE (ID,(X,Y,WIDTH,HEIGHT)?,BITMAP)>
00093             <!ELEMENT ID (#PCDATA)>
00094             <!ELEMENT X (#PCDATA)>
00095             <!ELEMENT Y (#PCDATA)>
00096             <!ELEMENT WIDTH (#PCDATA)>
00097             <!ELEMENT HEIGHT (#PCDATA)>
00098             <!ELEMENT BITMAP (#PCDATA)>
00099 \endverbatim        
00100           Example:
00101 \verbatim
00102             <PICTURE>
00103                 <ID>PacManUp1</ID>
00104                 <X>0</X>
00105                 <Y>96</Y>
00106                 <WIDTH>37</WIDTH>
00107                 <HEIGHT>31</HEIGHT>
00108                 <BITMAP>bitmap\\pac_pix.xml</BITMAP>
00109             </PICTURE>
00110 \endverbatim      
00111 <PRE>
00112       ID               - supposed a unique identifier
00113       X, Y             - position of "picture" on the bitmap
00114       WIDTH, HEIGHT    - Width and Hight (size) of the "picture" on the bitmap
00115       BITMAP           - File to be loaded for the bitmap.
00116 
00117       CSPicture are singletons in the sense, that two "loaded" CSPicture of the 
00118       same "ini" file ARE the same instances. 
00119       (Bitmaps are loaded only once - by there respective loader entity)
00120 
00121       these methods can be used:
00122 
00123         void display(CSDisplayParams *displayParams)
00124 
00125         unsigned int getMaxX()      // get width of the pciture
00126         unsigned int getMaxY()      // get height of the picture
00127         void setSolid(bool b)       // enable/disable transparency
00128         bool isSolid()              // has picture a transparent color
00129         SDL_Surface *getSurface()   // for the font lib only
00130 
00131       (SDLMain::addUpdateRect(mUpdateRegion) - is called after displaying, so at some 
00132        stage - later (main-loop)
00133             void SDLMain::updateScreen()
00134        must be called.)
00135 </PRE>
00136 
00137     Class that holds and can display a picture.
00138     The picture class has no instance relevant data (state independant)
00139     for example: as position \par
00140     Thus one picture can be (re) used severall times!
00141     The data used for printing must come from the outside (CSDisplayParams).
00142 */
00143 class CSPicture
00144 {
00145     friend CSPictureLoader;
00146     friend CSPictureDirectLoader;
00147     private:
00148         static CSPictures mAllPictures;
00149         static void addPicture(CSPicture *picture);
00150         static void removePicture(CSPicture *picture);
00151 
00152         bool mIsDirectLoad;     
00153         std::string mLoadName;
00154         int mRLEUse;                ///< flag whether RLE should be used by this image
00155                                     ///< set via xml ini
00156         int mAlpha;                 ///< the alpha value of this picture
00157                                     ///< can be set via xml-ini
00158 
00159         SDL_Rect mUpdateRegion;     ///< has position and size where the picture will
00160                                     ///< be displayed
00161                                     ///< is set to mPixelPictureRegion.w
00162                                     ///< and mPixelPictureRegion.h
00163         SDL_Rect mPixelPictureRegion; ///< the picture within the image ehich is loaded from disk
00164                                     ///< will be blittet to a surface, where it alone resides
00165                                     ///< this are the coordinates for that
00166                                     ///< note: due to optimization, this region might also
00167                                     ///< be smaller than the actual picture size...
00168                                     ///< if the picture is "outlined" by its transparent
00169                                     ///< color, than the x,y,w,h will be updated
00170                                     ///< to where not only transaprent colors are visible
00171         SDL_Rect mPictureRegion;    ///< position and size of this picture on a 
00172                                     ///< "greater" image
00173                                     ///< - does not change, once set!
00174 
00175         unsigned int mStartPixelsX; ///< like mPixelPictureRegion.x
00176                                     ///< gives the offset from the original
00177                                     ///< picture start to where the optimized
00178                                     ///< picture starts
00179         unsigned int mStartPixelsY; ///< like mPixelPictureRegion.y
00180                                     ///< gives the offset from the original
00181                                     ///< picture start to where the optimized
00182                                     ///< picture starts
00183 
00184         SDL_Surface *mPicture;      ///< never! freed - is handled by BitmapLoader!
00185         bool mIsSolid;              ///< picture has now transparency
00186         char *mId;                  ///< id of picture
00187         char *mFileName;            ///< filename, from which picture was loaded (xml)
00188         int mColorKey;              ///< current color key
00189         int mR;
00190         int mG;
00191         int mB;
00192         double mFactor;
00193         int mScaledWidth;
00194         int mScaledHeight;
00195         bool mIsCreatedAsScaled;
00196         SDL_Surface *mScaledPicture;///< 
00197 
00198         unsigned char **mProjection;///< pixel projection of image (8bit) for easier
00199                                     ///< collision detection
00200         unsigned int mPixelProjectionCount; ///< how many non- transparent pixels
00201                                     ///< are there within this image
00202         int mRLEAccelleration;      ///< "OR" whether RLE acceleration is
00203                                     ///< used when image is blitted
00204         
00205         ///  constructor using "*.xml" file
00206         CSPicture(const std::string &filename, bool isDirectLoad = false);
00207         virtual void initialize(const CSPictureData &data, bool isDirectLoad = false);
00208         static void loadPictureData(const std::string &filename, CSPictureData &data);
00209         
00210         void resetSurface();        ///< converts current surface to display surface
00211                                     ///< used (e.g.) upon fullscreen-toggle
00212         SDL_Surface *convertToCurrentScreen(CSBitmap *image, SDL_Rect rect);
00213         void checkRLEAccelleration(); ///< this is not really well implemented yet :-)
00214         void buildPixelProjection(CSBitmap *image, const SDL_Rect &position);
00215         void buildOptimizeData();
00216 
00217     public:
00218         /// copy constructor
00219         CSPicture(const CSPicture &picture);
00220 
00221         /// destructor
00222         virtual ~CSPicture();   
00223 
00224         static const char *CLASS;
00225         virtual std::string getType() {return (std::string) CLASS;}
00226 
00227         void getRGB(int &r, int &b, int &g);
00228         virtual void display(CSDisplayParams *mDisplayParams);  ///< display this picture on the srceen
00229         virtual void display(int x, int y);                     ///< display this picture on the srceen
00230         virtual void display(SDL_Surface *destination, int x, int y);                       ///< display this picture on the srceen
00231         unsigned int getMaxX(){return mPictureRegion.w;}        ///< get width of the pciture
00232         unsigned int getMaxY(){return mPictureRegion.h;}        ///< get height of the picture
00233         unsigned int getWidth(){return mPictureRegion.w;}       ///< get width of the pciture
00234         unsigned int getHeight(){return mPictureRegion.h;}      ///< get height of the picture
00235         void setSolid(bool b);                                  ///< enable/disable transparency
00236         bool isSolid() {return mIsSolid;}                       ///< has picture a transparent color
00237         void setAlpha(int alpha);
00238         int getColorKey() {return mColorKey;}                   ///< get current transparent color
00239         SDL_Surface *getSurface() {return mPicture;}            ///< for the font lib only
00240         unsigned char **getProjection() {return mProjection;}   ///< for collision detection
00241         unsigned int getPixelProjectionCount()                  ///< for collision detection (optimization)
00242         {return mPixelProjectionCount;}
00243         
00244         static void resetSurfaces();                            ///< resets all surfaces
00245                                                                 ///< loaded via the CSPictureLoader (all!)
00246         void scale(int width, int divideX, int height, int divideY);
00247         SDL_Surface *getScaledPicture(double factor);
00248         SDL_Surface *getScaledPicture(int w, int h);
00249         bool isTransparent(int x, int y);
00250         static CSPicture* createScaledPicture(CSPicture* picture, double factor);   // builds a complete new Picture!
00251 };
00252 
00253 /** Factory Loader-class for CSPictures.
00254   * Factory Loader-class for CSPictures
00255   * that is used to create CSPicture \par
00256   * Uses xml - data-files, produces "singletons"
00257   */
00258 class CSPictureLoader : public Loadable<CSPicture>
00259 {
00260     protected:
00261         virtual CSPicture *create(const std::string &filename)
00262         {
00263             return new CSPicture(filename, false);
00264         }
00265 
00266     public:
00267         static CSPictureLoader INSTANCE;
00268         void scale(char *name, int width, int divideX, int height, int divideY);
00269 };
00270 
00271 /** Factory Loader-class for CSPictures.
00272   * Factory Loader-class for CSPictures
00273   * that is used to create CSPicture \par
00274   * Uses xml - data-files, produces "singletons"
00275   */
00276 class CSPictureDirectLoader : public Loadable<CSPicture>
00277 {
00278     protected:
00279         virtual CSPicture *create(const std::string &filename)
00280         {
00281             return new CSPicture(filename, true);
00282         }
00283 
00284     public:
00285         static CSPictureDirectLoader INSTANCE;
00286         void scale(const char *name, int width, int divideX, int height, int divideY);
00287 };
00288 #endif // CSPicture_h

Generated on Wed Jul 14 00:43:31 2004 for CSLib by doxygen 1.3.6