0
votes

I have this image of size 256 × 256 that I want to center, but for some odd reason it isn't working.

Image of game running

As you can see it's not centered. Here is the code:

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

Some other code:

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);

Ignore the player sprite. I'm having a problem with the 'InventorySprite'.

It's my first time posting, so excuse me if I forgot to include something.

1

1 Answers

0
votes

All you Need to do is to set the texture position in relation to the window width an height. Here the pseudo Code:

texturePosition.X = (windowWidth - textureWidth) / 2;
texturePosition.Y = (windowHeight - textureHeight) / 2;