00001 #include "Terrain.h" 00002 #include "Display.h" 00003 00004 #define GRASS_IMAGE "images/tile_grass.png" 00005 //#define WALL_IMAGE "tile_wall.png" 00006 #define WATER_IMAGE "images/tile_water.png" 00007 00008 map<TerrainType, Terrain::TerrainBaseType*> Terrain::_possibleTypes; 00009 map<TerrainType, SDL_Surface*> Terrain::_imageMap; 00010 int Terrain::_refCount = 0; 00011 00012 Terrain::Terrain(TerrainType type) 00013 { 00014 static bool mapped = false; 00015 if (!mapped) 00016 { 00017 // this is currently a memory leak... 00018 _imageMap[GRASS] = Display::instance().loadImage(GRASS_IMAGE); 00019 _imageMap[WATER] = Display::instance().loadImage(WATER_IMAGE); 00020 00021 mapped = true; 00022 } 00023 00024 // reference counted 00025 if (_refCount == 0) 00026 { 00027 _possibleTypes[GRASS] = new TerrainGrass(); 00028 _possibleTypes[WATER] = new TerrainWater(); 00029 } 00030 00031 _type = _possibleTypes[type]; 00032 _refCount++; 00033 } 00034 00035 Terrain::~Terrain() 00036 { 00037 _refCount--; 00038 if (_refCount == 0) 00039 { 00040 map<TerrainType, TerrainBaseType*>::iterator iter; 00041 for (iter = _possibleTypes.begin(); iter != _possibleTypes.end(); ++iter) 00042 { 00043 SAFE_DELETE((*iter).second); 00044 } 00045 } 00046 } 00047 00048 bool Terrain::isPassable() const 00049 { 00050 return _type->isPassable(); 00051 } 00052 00053 TerrainType Terrain::getType() const 00054 { 00055 return _type->getType(); 00056 } 00057 00058 double Terrain::getMoveMultiplier() const 00059 { 00060 return _type->getMoveMultiplier(); 00061 } 00062 00063 void Terrain::draw(int x, int y) const 00064 { 00065 Display::instance().draw(x, y, _imageMap[getType()]); 00066 }