Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]TextPosition relative to window size  (Read 1551 times)

0 Members and 1 Guest are viewing this topic.

Kleath

  • Newbie
  • *
  • Posts: 7
    • View Profile
[SOLVED]TextPosition relative to window size
« on: April 28, 2016, 04:44:31 pm »
Hello, I'm making a Pong Game, and for this i made a MainMenu gamestate. There's a text for Start Game and one for End Game. Now i made this do change the color of the text if I'm moving my mous over it:

if (txtStartGame.getGlobalBounds().contains(
      sf::Mouse::getPosition(game.window).x,
      sf::Mouse::getPosition(game.window).y) &&
      txtStartGame.getColor() != sf::Color::Green)
        {
            //changecolor
        }

now this works great, but only if I dont change the size of my window. If I change the size of the window, i have to move the mouse on another spot to change the color of the text i want to change.

im making the window like this:

window.create(sf::VideoMode(1280, 720), "Pong");

and the text im making like this:

        txtStartGame.setFont(font);
   txtStartGame.setString("Start Game");
   txtStartGame.setCharacterSize(30);
   txtStartGame.setPosition(400.f, 500.f);


So what am I doing wrong ?

(Sorry for my bad English by the way xD)
« Last Edit: April 28, 2016, 05:05:01 pm by Kleath »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: TextPosition relative to window size
« Reply #1 on: April 28, 2016, 04:52:28 pm »
Hello.

When you resize the window, it doesn't resize the (default if you're not using any) sf::View.
You can convert the pixel coordinates of your mouse to its coordinates in the world with mapPixelToCoords.

Kleath

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: TextPosition relative to window size
« Reply #2 on: April 28, 2016, 05:04:43 pm »
Thanks man, helped me a lot ;D