我有一个 std::vector<sf::Texture>
我这样初始化:
texture.loadFromFile(assets/texture/faceShadow.png);
textures.push_back(texture);
我的文件夹永远是这样的:
assets/texture/faceShadow.png
//texture: 1, 2, ...
//faceShadow: 1 to 9
这里是我目前拥有的 std::cout
的示例显示:
assets/1/1.png - (entry n°0)
assets/1/2.png - (entry n°1)
assets/1/3.png - (entry n°2)
assets/1/4.png - (entry n°3)
assets/1/5.png - (entry n°4)
assets/1/6.png - (entry n°5)
assets/1/7.png - (entry n°6)
assets/1/8.png - (entry n°7)
assets/1/9.png - (entry n°8)
assets/2/1.png - (entry n°9)
assets/2/2.png - (entry n°10)
assets/2/3.png - (entry n°11)
assets/2/4.png - (entry n°12)
assets/2/5.png - (entry n°13)
assets/2/6.png - (entry n°14)
assets/2/7.png - (entry n°15)
assets/2/8.png - (entry n°16)
assets/2/9.png - (entry n°17)
我有纹理和 faceShadow 编号,我正在尝试获取 entryNumber。
textures[entryNumber];
最佳答案
您可以使用模块化算法:
std::string relativePathToAsset(unsigned entry)
{
if(entry > 17)
{
// handle invalid input (if you like)
}
return std::to_string(entry / 9 + 1) + "/" + std::to_string(entry % 9 + 1) + ".png";
}
但我相信还有一种方法可以创建多个纹理,这些纹理与一个图像具有不同的偏移量。如果您正在制作动画,这似乎更合理。
编辑:
在下面的代码中:
return std::to_string(entry / 9 + 1) + "/" + std::to_string(entry % 9 + 1) + ".png";
// ^^^^^^^^^^^^^ ^^^^^^^^^^^^^
// formula for texture formula for faceShadow
你可以这样使用:
int texture = entry / 9 + 1;
int faceShadow = entry % 9 + 1;
最终编辑:
您实际上需要一个与我建议的相反的公式:
textures[(texture - 1) * 9 + (faceShadow - 1)];
关于c++ - 如何计算我的数组条目条目号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33856816/