【问题标题】:How to account for spacing between tiles in a tile sheet如何考虑瓷砖表中瓷砖之间的间距
【发布时间】:2025-12-21 20:10:17
【问题描述】:

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

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

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:

(注意:“它应该看起来如何”图像来自平铺编辑器)

【问题讨论】:

  • 更改图块表以删除这些空格会更容易吗?将所有瓷砖尽可能紧凑地保存在一张纸中的标准做法有很多原因(包括易于加工,正如您所发现的那样)。
  • 你说得对,我设法为 gimp 找到了一个 python 脚本并用它来删除所有空格,现在一切正常!

标签: c++ sfml tiled tmx


【解决方案1】:

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

【讨论】: