SFML - 精灵没有正确居中

时间:2018-04-25 01:58:19

标签: c++ sprite sfml

我有这个256×256大小的图像,我想要居中,但由于一些奇怪的原因,它无法正常工作。

Image of game running

正如你所看到的那样,它没有居中。这是代码:

int main()
{
    int w = 720;
    int h = 360;

其他一些代码:

sf::RenderWindow window(sf::VideoMode(w, h), "RPG Game", sf::Style::Default);
sf::Texture texture;
texture.loadFromFile("WalkCycle.png");
sf::Texture inventory;
inventory.loadFromFile("Inventory.png");
sf::Sprite player(texture);
sf::Sprite inventorySprite(inventory);

sf::Vector2u textureSize = texture.getSize();
textureSize.x /= 9;
textureSize.y /= 4;

player.setTextureRect(sf::IntRect(0, 0, textureSize.x, textureSize.y));
player.setOrigin(textureSize.x/2, textureSize.y/2);
player.setPosition(w/2, h/2);
inventorySprite.setTextureRect(sf::IntRect(0, 0, 256, 256));
inventorySprite.setOrigin(256/2, 256/2);
inventorySprite.setPosition(w/2, h/2);

//Game loop

if(sf::Keyboard::isKeyPressed(sf::Keyboard::E))
        inventoryOpen = true;

if(inventoryOpen)
        window.draw(inventorySprite);

忽略玩家精灵。我遇到了'InventorySprite'的问题。

这是我第一次发帖,所以请原谅我,如果我忘了包含一些东西。

1 个答案:

答案 0 :(得分:0)

您需要做的就是相对于窗口宽度和高度设置纹理位置。这里是伪代码:

Violates when: storage.googleapis.com/storage/total_bytes is above a threshold of 1 for greater than 2 minutes