Solution for using only one tiled map file (.tmx) for multi resolution

custom class TMXTiledMap, for example class MyMap

//- MyMap.h ----------------------------------------

class MyMap : public TMXTiledMap
{
public:
static MyMap* create(const std::string& tmxFile);
protected:
bool initWithTMXFile(const std::string& tmxFile);
void editInfo(TMXMapInfo* mapInfo);

};

//- MyMap.cpp ---------------------------------------
MyMap * MyMap::create(const std::string& tmxFile)
{
MyMap *ret = new MyMap();
if (ret->initWithTMXFile(tmxFile))
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}

bool MyMap::initWithTMXFile(const std::string& tmxFile)
{
// //////////////////////////////
// // 1. super init first
// if ( !TMXTiledMap::initWithTMXFile(tmxFile) )
// {
// return false;
// }
// return true;

//--------------------------------------------------
// rewrite initWithTMXFile and add editInfo inside
//--------------------------------------------------
CCASSERT(tmxFile.size()>0, "TMXTiledMap: tmx file should not be empty");

setContentSize(Size::ZERO);

TMXMapInfo *mapInfo = TMXMapInfo::create(tmxFile);

if (! mapInfo)
{
    return false;
}
CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap: Map not found. Please check the filename.");

//EDIT MAP INFO FOR MULTI RESOLUTION....
editInfo(mapInfo);

buildWithMapInfo(mapInfo);
//-----------------------------

// all tiles are aliased by default, let's set them anti-aliased
for (const auto& child : getChildren())
{
    static_cast<SpriteBatchNode*>(child)->getTexture()->setAntiAliasTexParameters();
    static_cast<SpriteBatchNode*>(child)->setBlendFunc(BlendFunc::ALPHA_PREMULTIPLIED);

}


return true;

}

void MyMap::editInfo(TMXMapInfo* mapInfo)
{
//detect for seting scaleMap and dirMap for each resolution
//for example

string dirMap;
float scaleMap;
Size frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
   dirMap = "medium";
   scaleMap = 1;
#else
   if (frameSize.width <= 320) //iphone 2G,3G
   {
       dirMap = "medium";
       scaleMap = 1;
   }
   else if (frameSize.width == 750)// iphone 6
   {
       dirMap = "iphone6";
       scaleMap = 1.15;
   }
   else if (frameSize.width <= 768) //iphone4, iphone 5, ipad
   {
       dirMap = "medium";
       scaleMap = 1;
   }
   else //iphone 6+, ipad hd
   {
       dirMap = "large";
       scaleMap = 2;
   }
#endif


Size tilesize = mapInfo->getTileSize();
tilesize.width *= scaleMap;
tilesize.height *= scaleMap;
mapInfo->setTileSize(tilesize);

auto& tilesets = mapInfo->getTilesets();
if (tilesets.size()>0)
{
    TMXTilesetInfo* tileset = nullptr;
    for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter)
    {
        tileset = *iter;
        if (tileset)
        {
            string str = tileset->_sourceImage;
            
            string dir = str.substr(0, str.find_last_of("/") + 1);
            string imagename = str.substr(str.find_last_of("/1"));
            tileset->_sourceImage = dir + dirMap + "/maps" + imagename;
            
            tileset->_margin *= scaleMap;
            tileset->_spacing *= scaleMap;

            tileset->_tileSize.width *= scaleMap;
            tileset->_tileSize.height *= scaleMap;
        }
    }
}

}


with the Objects Layer => remember multi with scaleMap after get x,y.

//--------------------------------------------------------------------------------
And now you only need only one .tmx (but with multi tileset .png for best graphic performance).

:slight_smile:

4 Likes