00001 #ifndef CSTILEMAP_H
00002 #define CSTILEMAP_H
00003
00004
00005
00006
00007 #include <string>
00008 #include <vector>
00009 #include "CSTypes.h"
00010 #include "SDLMain.h"
00011 #include "CSTileSet.h"
00012 #include "CSSprite.h"
00013 #include "CSLoadable.h"
00014 #include "CSLog.h"
00015
00016 class CSTileMapLoader;
00017 class CSTileMap;
00018 typedef std::vector<CSTileMap *> CSTileMaps;
00019
00020 class CSTileMapData
00021 {
00022 public:
00023 char *id;
00024 char *tileSet;
00025 StringVector *rows;
00026 bool isChangebale;
00027
00028 CSTileMapData()
00029 {
00030 id = 0;
00031 isChangebale = false;
00032 tileSet = 0;
00033 rows = new StringVector();
00034 }
00035
00036 ~CSTileMapData()
00037 {
00038 if (id != 0)
00039 {
00040 free (id);
00041 id = 0;
00042 }
00043 if (tileSet != 0)
00044 {
00045 free (tileSet);
00046 tileSet = 0;
00047 }
00048 if (rows != 0)
00049 {
00050 for (StringVector::iterator iter = rows->begin(); iter != rows->end(); iter++)
00051 {
00052 char *help = (char *) *iter;
00053 free(help);
00054 }
00055 free(rows);
00056 }
00057 }
00058 };
00059
00060 class CSTileMap
00061 {
00062 friend CSTileMapLoader;
00063 private:
00064 int sx;
00065 int sy;
00066 int ex;
00067 int ey;
00068
00069 CSSprites mSprites;
00070 SDL_Surface *mScaledMap;
00071
00072 char *mId;
00073 CSTileSet *mTileSet;
00074 CSTile ***mMap;
00075 StringVector *mRows;
00076 unsigned int mXSize;
00077 unsigned int mYSize;
00078 unsigned int mHeight;
00079 unsigned int mWidth;
00080 unsigned int mWorldHeight;
00081 unsigned int mWorldWidth;
00082 bool mActive;
00083 bool mScaleDisplay;
00084
00085 int mOffsetX;
00086 int mOffsetY;
00087 bool mIsChangeable;
00088 double mFactor;
00089 bool mIsBackground;
00090 bool mIsParallax;
00091 unsigned int mDisplayWidth;
00092 unsigned int mDisplayHeight;
00093 unsigned int mWorldNo;
00094 bool mIsSolid;
00095 int mR;
00096 int mG;
00097 int mB;
00098 CSDisplayParams mDisplayParams;
00099
00100
00101 CSTileMap(const std::string &filename);
00102 static void loadTileMapData(const std::string &filename, CSTileMapData &data);
00103
00104 void initialize(const CSTileMapData &data);
00105 void initialize();
00106
00107 public:
00108 CSTileMap(const CSTileMap &tileMap);
00109 virtual ~CSTileMap();
00110 static const char *CLASS;
00111 virtual std::string getType() {return (std::string) CLASS;}
00112
00113 void update();
00114 void display();
00115 void setSolid(bool b);
00116 void setChangeable(bool b) {mIsChangeable = b;}
00117 bool isChangeable(void) {return mIsChangeable;}
00118 bool isSolid() {return mIsSolid;}
00119 void setSecPerFrame(float secPerFrame);
00120
00121 void setWorldNo(unsigned int no){mWorldNo = no;}
00122 void setDisplayOffset(int xOffset, int yOffset);
00123 void setMapPosition(int x, int y);
00124 void addSprite(CSSprite *sprite);
00125 void removeSprite(CSSprite *sprite);
00126 CSTiles getTouchingTiles(int x,int y, int w, int h);
00127
00128 void setScaleDisplay(bool b){mScaleDisplay = b;}
00129 bool isScaleDisplay(void){return mScaleDisplay;}
00130 void addScaledSprites(double factor, const SDL_Rect &srcRect, SDL_Surface *destination, const SDL_Rect &displayRect);
00131 SDL_Surface *getScaledMap(double factor, bool isSolid);
00132 void resetScaledMap();
00133
00134 void setOffset(int offsetX, int offsetY);
00135 unsigned int getHeight(){return mHeight;}
00136 unsigned int getWidth(){return mWidth;}
00137 unsigned int getTileHeight(){return mYSize;}
00138 unsigned int getTileWidth(){return mXSize;}
00139 bool isBackground(void) {return mIsBackground;}
00140 void setBackground(bool b);
00141 void scaleMapToFit(int width, int height);
00142 void setParallax(bool b) {mIsParallax = b;}
00143 void setWorldMax(unsigned int width, unsigned int height) {mWorldWidth = width;mWorldHeight = height;}
00144 void setDisplaySize(unsigned int w, unsigned int h) {mDisplayWidth = w;mDisplayHeight = h;}
00145 void setTile(unsigned int x, unsigned int y, char idChar);
00146 void setTile(unsigned int x, unsigned int y, CSTile *tile, bool center = true);
00147 bool isBorderTile(CSTile *tile)
00148 {
00149
00150 if ((tile->getX()==0) || (tile->getY()==0)) return true;
00151 if ((tile->getX()==mXSize-1) || (tile->getY()==mYSize-1)) return true;
00152 return false;
00153 }
00154 CSTile *getTileAtPixel(unsigned int x, unsigned int y);
00155 CSTile *getTile(unsigned int tx, unsigned int ty) {return mMap[ty][tx];}
00156 CSTileSet *getTileSet() {return mTileSet;}
00157
00158 void setActive(bool active)
00159 {
00160 mActive = active;
00161 CSSprites::iterator iter = mSprites.begin();
00162 while (iter != mSprites.end())
00163 {
00164 CSSprite *sprite = *iter;
00165 if (mWorldNo == sprite->getWorldPos())
00166 {
00167 sprite->setActive(mActive) ;
00168 }
00169 iter++;
00170 }
00171 }
00172 };
00173
00174 class CSTileMapLoader : public Loadable<CSTileMap>
00175 {
00176 protected:
00177 virtual bool isSingleCreate(void) const
00178 {
00179 return false;
00180 }
00181
00182 virtual CSTileMap *create(const std::string &filename)
00183 {
00184 return new CSTileMap(filename);
00185 }
00186
00187 public:
00188 static CSTileMapLoader INSTANCE;
00189 };
00190
00191 #endif // CSTILEMAP_H