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

CSTileSet.cpp

Go to the documentation of this file.
00001 /**
00002 */
00003 
00004 #ifdef WIN32
00005 #pragma warning(disable : 4786 )
00006 #endif
00007 
00008 #include "CSTileSet.h"
00009 #include "CSXMLHelper.h"
00010 
00011 CSTileSetLoader CSTileSetLoader::INSTANCE;
00012 const char *CSTileSet::CLASS = "CSTileSet";
00013 
00014 CSTileSet::CSTileSet(const std::string &filename)
00015 {
00016     static char *functionName="CSTileSet";
00017     LOG_ENTER 
00018     CSTileSetData data;
00019     loadTileSetData(filename, data);
00020     initialize(data);
00021     LOG_EXIT
00022 }
00023 
00024 // not done
00025 CSTileSet::CSTileSet(const CSTileSet &set)
00026 {
00027     static char *functionName="CSTileSet";
00028     LOG_ENTER 
00029     initialize();
00030     mId = strdup(set.mId);
00031     mIsSolid = set.mIsSolid;
00032     mIsOneSize = set.mIsOneSize;
00033     mXSize = set.mXSize;
00034     mYSize = set.mYSize;
00035     LOG_EXIT
00036 }
00037 
00038 void CSTileSet::initialize(void)
00039 {
00040     static char *functionName="initialize";
00041     LOG_ENTER 
00042     mId = 0;
00043     mTileCount = 0;
00044     mIsOneSize = false;
00045     mXSize = 0;
00046     mYSize = 0;
00047     mIsSolid = false;
00048     mSecPerFrame = 0;
00049     for (int i=0; i< MAX_TILE; i++)
00050     {
00051         mTiles[i] = 0;
00052     }
00053     LOG_EXIT
00054 }
00055 
00056 void CSTileSet::initialize(const CSTileSetData &data)
00057 {
00058     static char *functionName="initialize";
00059     LOG_ENTER 
00060     initialize();
00061     mId = strdup(data.id);
00062     mTileCount = 0;
00063     for (StringVector::iterator iter = data.tileNames->begin(); iter != data.tileNames->end(); iter++)
00064     {
00065         char *tilename = (char *) *iter;
00066         CSTile *tile = CSTileLoader::INSTANCE.load(tilename);
00067         unsigned char value = tile->getIDChar();
00068         mTiles[value] = tile;
00069         mTileCount++;
00070     }
00071     mIsOneSize = data.onesize;
00072     mXSize = data.width;
00073     mYSize = data.height;
00074 
00075     LOG_EXIT
00076 }
00077 
00078 CSTileSet::~CSTileSet()
00079 {
00080     static char *functionName="~CSTileSet";
00081     LOG_ENTER 
00082     if (mId != 0)
00083     {
00084         free(mId);
00085         mId = 0;
00086     }
00087 
00088     for (int i=0; i< MAX_TILE; i++)
00089     {
00090         if (mTiles[i] != 0)
00091         {
00092             delete (mTiles[i]);
00093             mTiles[i] = 0;
00094             mTileCount--;
00095         }
00096     }
00097 
00098     LOG_EXIT
00099 }
00100 
00101 // get a char that is not used by a tile
00102 int CSTileSet::getFreeTile()
00103 {
00104     for (int i=0; i< MAX_TILE; i++)
00105     {
00106         if (mTiles[i] == 0)
00107         {
00108             return i;
00109         }
00110     }
00111     return MAX_TILE;
00112 }
00113 
00114 // add a tile to the tileset, the tile will be using "c" as it's "key"
00115 void CSTileSet::setTile(CSTile* tile, unsigned char c)
00116 {
00117     if (mTiles[c] == 0)
00118     {
00119         if (tile == 0)
00120         {
00121             return;
00122         }
00123         mTiles[c] = tile;
00124         mTileCount++;
00125     }
00126     else
00127     {
00128         mTiles[c] = tile;
00129         if (tile == 0)
00130         {
00131             mTileCount--;
00132         }
00133     }
00134     if (tile != 0)
00135     {
00136         mTiles[c]->setSecPerFrame(mSecPerFrame);
00137         mTiles[c]->setSolid(mIsSolid);
00138         // experimental only for single tile, therefore shouldn't be changed...
00139         // mTiles[c]->scale(mWidth, mTilesX, mHeight, mTilesY);
00140     }
00141 }
00142 
00143 void CSTileSet::setSolid(bool b)
00144 {
00145     static char *functionName="setSolid";
00146     LOG_ENTER 
00147     mIsSolid = b;
00148     for (int i=0; i< MAX_TILE; i++)
00149     {
00150         if (mTiles[i] != 0)
00151         {
00152             mTiles[i]->setSolid(b);
00153         }
00154     }
00155     LOG_EXIT
00156 }
00157 
00158 void CSTileSet::setSecPerFrame(float secPerFrame)
00159 {
00160     static char *functionName="setSecPerFrame";
00161     LOG_ENTER 
00162     mSecPerFrame = secPerFrame;
00163     for (int i = 0; i< MAX_TILE; i++)
00164     {
00165         if (mTiles[i] != 0)
00166         {
00167             mTiles[i]->setSecPerFrame(secPerFrame);
00168         }
00169     }
00170     LOG_EXIT
00171 }
00172 
00173 void CSTileSet::loadTileSetData(const std::string &filename, CSTileSetData &data)
00174 {
00175     static char *functionName="loadTileSetData";
00176     LOG_ENTER 
00177     char tmp[10];
00178     CSXMLHelper xmlSupport(filename, "TILESET");
00179     try
00180     {
00181         if (xmlSupport.getError())
00182         {
00183             LOG_EXIT
00184             throw "error";
00185         }
00186 
00187         data.id = strdup(xmlSupport.getString("ID").c_str());
00188 
00189         int tileCount = xmlSupport.getInt("count(TILES/NAME)");
00190         for (int i=0; i<tileCount; i++)
00191         {
00192             // i+1, predicates in xpath start with 1
00193             std::string a = xmlSupport.getString((std::string)"TILES/NAME["+itoa(i+1,tmp,10)+"]");
00194             data.tileNames->push_back(strdup(a.c_str()));
00195         }
00196         
00197         // One size does not neccessarily mean, that all tiles have the same size
00198         // but within tilemap and replacing of tiles at runtime
00199         // same sizes are assumed to center position of a "new" tile at a 
00200         // previously occupied position
00201         // sizes are used to calculate the position in such an event
00202         int oneSizeCount = xmlSupport.getInt("count(ONE_SIZE)");
00203         if (oneSizeCount>0)
00204         {
00205             data.onesize = xmlSupport.getInt("ONE_SIZE") == 1;
00206             data.width = xmlSupport.getInt("WIDTH");
00207             data.height = xmlSupport.getInt("HEIGHT");
00208         }
00209     }
00210     catch(...)
00211     {
00212         LOG_EXIT
00213         SDLMain::shutdown((std::string)"XML error \"" + filename + "\": " + xmlSupport.getErrorMessage().c_str(), 1);
00214     }
00215     LOG_EXIT
00216 }
00217 
00218 void CSTileSet::scaleToFit(int width, int tilesX, int height, int tilesY)
00219 {
00220     static char *functionName="scaleToFit";
00221     LOG_ENTER 
00222     // scaling is experimental!!!
00223     // only for single tile background usage!
00224     if (mTileCount != 1)
00225     {
00226         LOG_EXIT
00227         return;
00228     }
00229     for (int i=0; i< MAX_TILE; i++)
00230     {
00231         if (mTiles[i] != 0)
00232         {
00233             mTiles[i]->scale(width, tilesX, height, tilesY);
00234         }
00235     }
00236     LOG_EXIT
00237 }
00238 

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