如何计算磁贴纸中磁贴之间的间距

时间:2019-07-10 17:53:56

标签: c++ sfml tiled tmx

我的磁贴纸具有64x64的磁贴,但是在每个磁贴之间有10px的间隙,在设置图像中的纹理矩形以绘制该磁贴时,我需要考虑该间隙

我尝试在设置纹理矩形时简单地添加空间,但是图像看起来仍然失真

for (auto y = 0u; y < map.getTileCount().y; ++y)
            {
                for (auto x = 0u; x < map.getTileCount().x; ++x)
                {
                    auto posX = static_cast<float>(x * map.getTileSize().x); 
                    auto posY = static_cast<float>(y * map.getTileSize().y);
                    sf::Vector2f position(posX, posY);

                    tileSprite.setPosition(position); 

                    auto tileID = tiles[y * map.getTileCount().x + x].ID; //the id of the current tile

                    if (tileID == 0)
                    {
                        continue; //empty tile
                    }

                    auto i = 0;
                    while (tileID < tileSets[i].getFirstGID())
                    {
                        ++i;
                    }

                    auto relativeID = tileID - tileSets[i].getFirstGID(); 

                    auto tileX = relativeID % tileSets[i].getColumnCount();
                    auto tileY = relativeID / tileSets[i].getColumnCount();

                    textureRect.left = tileX * tileSets[i].getTileSize().x; //i am guessing this is where
                    // i should account for the spacing 
                    textureRect.top = tileY * tileSets[i].getTileSize().y;

                    tileSprite.setTexture(mTextureHolder.get(Textures::SpriteSheet));
                    tileSprite.setTextureRect(textureRect);

                    mMapTexture.draw(tileSprite);
                }
            }

代码本身正在工作,并以正确的尺寸绘制图块,如果我使用没有任何间距的普通64x64图块集,则最终图像看起来正确,但是将包含图块的间距切出。 设置纹理矩形时,如何在图块之间添加间隙?

this is how it looks:

this is how it should look:

(注意:“外观”图像来自Tiled编辑器)

1 个答案:

答案 0 :(得分:0)

用我发现的python脚本删除空格并gimp解决了这个问题,但是,如果有人知道如何解决空格问题,请随时回答,因为有一天我可能会需要它