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

CSWorld.h

Go to the documentation of this file.
00001 #ifndef CSWORLD_H
00002 #define CSWORLD_H
00003 
00004 // USES SDL_Surface
00005 // USES SDL_Rect 
00006 
00007 #include <string>
00008 #include <vector>
00009 #include "CSTypes.h"
00010 #include "SDLMain.h"
00011 #include "CSTileMap.h"
00012 #include "CSSprite.h"
00013 #include "CSLoadable.h"
00014 #include "CSLog.h"
00015 
00016 class CSWorldLoader;
00017 class CSWorld;
00018 class CSMusic;
00019 
00020 typedef std::vector<CSWorld *> CSWorlds;
00021 
00022 class MapData
00023 {
00024     public:
00025         char *name;                 
00026         int offsetX;
00027         int offsetY;
00028         bool isBackground;
00029         bool isParallax;
00030     MapData()
00031     {
00032         name = 0;
00033         offsetX = 0;
00034         offsetY = 0;
00035         isBackground = false;;
00036         isParallax = false;
00037     }
00038     
00039     ~MapData()
00040     {
00041         if (name != 0)
00042         {
00043             free(name);
00044             name = 0;
00045         }
00046     }
00047 };
00048 
00049 typedef std::vector<MapData *> MapDatas;
00050 class CSWorldData
00051 {
00052     public:
00053         char *id;                   // defining 
00054         char *musicName;
00055         MapDatas *maps;
00056         int layers;
00057 
00058         CSWorldData()
00059         {
00060             musicName = 0;
00061             id = 0;
00062             layers = 0;
00063             maps = new MapDatas();
00064         }
00065 
00066         ~CSWorldData()
00067         {
00068             if (id != 0)
00069             {
00070                 free (id);
00071                 id = 0;
00072             }
00073             if (musicName != 0)
00074             {
00075                 free (musicName);
00076                 musicName = 0;
00077             }
00078             if (maps != 0)
00079             {
00080                 for (MapDatas::iterator iter = maps->begin(); iter != maps->end(); iter++)
00081                 {
00082                     MapData *help = (MapData *) *iter;
00083                     free(help->name);
00084                     help->name = 0;
00085                     delete help;
00086                 }
00087                 delete maps;
00088             }
00089         }
00090 };
00091 
00092 class CSWorld
00093 {
00094     friend CSWorldLoader;
00095     private:
00096         int mXPos;
00097         int mYPos;
00098         SDL_Rect mScaledWorldDestination;
00099         SDL_Rect mMapArea;
00100         int mScaleMapCount;
00101         double mFactor;
00102         char *mId;                          // defining 
00103         CSTileMap **mWorld;
00104         unsigned int mLayers;
00105         int mXOffset;
00106         int mYOffset;
00107         unsigned int mHeight;   // Height in pixel
00108         unsigned int mWidth;    // Width in pixel
00109         unsigned int mWorldNo;
00110         bool mActive;
00111         CSSprites mSprites;
00112         CSMusic *mMusic;
00113 
00114         // constructor using "*.wrl" file
00115         CSWorld(const std::string &filename);
00116         static void loadWorldData(const std::string &filename, CSWorldData &data);
00117 
00118         void initialize(const CSWorldData &data);
00119         void initialize();
00120     
00121     public:
00122         CSWorld(const CSWorld &world);
00123         virtual ~CSWorld();
00124         static const char *CLASS;
00125         virtual std::string getType() {return (std::string) CLASS;}
00126 
00127         void display();
00128         void displayScaled(CSSprite *centerSprite);
00129         void update();
00130         void setSecPerFrame(float secPerFrame);
00131         void setDisplayArea(SDL_Rect area);
00132         void setWorldPosition(int x, int y);
00133         void addSprite(CSSprite *sprite);
00134         void removeSprite(CSSprite *sprite);
00135         void setWorldNo(unsigned int no);
00136 
00137         void scaleBackgroundTiles(int w, int h);
00138         void buildScaledWorld(double factor, const SDL_Rect &srcRect, SDL_Surface *destination, const SDL_Rect &destRect);
00139         void setScaleFactor(double factor) {mFactor = factor;}
00140 
00141         void setScaleDisplayArea(SDL_Rect area);
00142         void resetScaledWorld();
00143 
00144         void setScaleDisplayLayer(bool b, int layer)
00145         {
00146             if (mWorld[layer]->isScaleDisplay() == b)
00147             {
00148                 return;
00149             }
00150             if (b)
00151             {
00152                 mScaleMapCount++;
00153             }
00154             else 
00155             {
00156                 mScaleMapCount--;
00157             }
00158             mWorld[layer]->setScaleDisplay(b);
00159         }
00160         
00161         unsigned int getDisplayXLowerBound() {return mMapArea.x;}
00162         unsigned int getDisplayXUpperBound() {return mMapArea.x + mMapArea.w;}
00163         unsigned int getDisplayYLowerBound() {return mMapArea.y;}
00164         unsigned int getDisplayYUpperBound() {return mMapArea.y + mMapArea.h;}
00165         unsigned int getHeight(){return mHeight;}
00166         unsigned int getWidth(){return mWidth;}
00167         void setTile(unsigned int x, unsigned int y, unsigned int z, char idChar); // old tile deleted!
00168         void setTile(unsigned int x, unsigned int y, unsigned int z, CSTile *tile); // old tile not deleted, but overwritten
00169         CSTile *getTileAtPixel(unsigned int x, unsigned int y, unsigned int z)
00170         {
00171             return mWorld[z]->getTileAtPixel(x, y);
00172         }
00173         CSTile *getTile(unsigned int x, unsigned int y, unsigned int z)
00174         {
00175             return mWorld[z]->getTile(x, y);
00176         }
00177         
00178         unsigned int getLayerCount() {return mLayers;}
00179         CSTileMap *getLayer(unsigned int layer) 
00180         {
00181             if (layer > mLayers) return 0;
00182             return mWorld[layer];
00183         }
00184         int getWorldPositionX() {return mXPos;}
00185         int getWorldPositionY() {return mYPos;}
00186         bool isBorderTile(CSTile * tile, int z) {return mWorld[z]->isBorderTile(tile);}
00187         void setActive(bool active) 
00188         {
00189             mActive = active;
00190             if (mWorld != 0)
00191             {
00192                 for (int l=0; l< mLayers; l++)
00193                 {
00194                     mWorld[l]->setActive(mActive);
00195                 }
00196             }
00197         }
00198 };      
00199 
00200 class CSWorldLoader : public Loadable<CSWorld>
00201 {
00202     protected:
00203         virtual bool isSingleCreate(void) const
00204         {
00205             return false;
00206         }
00207 
00208         virtual CSWorld *create(const std::string &filename)
00209         {
00210             return new CSWorld(filename);
00211         }
00212 
00213     public:
00214         static CSWorldLoader INSTANCE;
00215 };
00216 
00217 #endif // CSWORLD_H

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