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

Author Topic: Cursor position in title bar  (Read 5060 times)

0 Members and 1 Guest are viewing this topic.

2ant

  • Newbie
  • *
  • Posts: 17
    • View Profile
Cursor position in title bar
« on: October 08, 2014, 06:42:52 pm »
Hi everyone, i'm new at using c++ and sfml.

I need to display the cursor position in the title bar next to the title.

I tried this :

sf::Vector2i position = sf::Mouse::getPosition(window);

    sf::String title ;

    title += position.x;
    title += position.y;

    sf::Window::setTitle(title);

My problem is that position.x and position.y give a int and i need to put them in a sf::String.

Can someone help me fix this.

Do you know another way to do it ?

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Cursor position in title bar
« Reply #1 on: October 08, 2014, 06:47:03 pm »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Cursor position in title bar
« Reply #2 on: October 08, 2014, 08:34:49 pm »
Or std::stringstream.

How to convert between integers and strings is basic C++, nothing SFML specific ;)
(the sf::String class is compatible with any standard string format)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

2ant

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Cursor position in title bar
« Reply #3 on: October 08, 2014, 08:43:39 pm »
Thank you for your very usefull answers Strelok and Nexus,

i have done this

sf::Vector2i position = sf::Mouse::getPosition(window);

    string title ( "SFML ");

    std::string posx = std::to_string ( position.x );
    std::string posy = std::to_string ( position.y );

    title += posx;
    title += posy;

    sf::Window::setTitle(title);

but i get this error (i'm using codeblocks):

error: cannot call member function 'void sf::Window::setTitle(const sf::String&)' without object

I don't know what to do to fix it .

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Cursor position in title bar
« Reply #4 on: October 08, 2014, 09:00:58 pm »
window.setTitle(title);
Laurent Gomila - SFML developer

2ant

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Cursor position in title bar
« Reply #5 on: October 08, 2014, 09:22:00 pm »
That solved it.

I feel dumb, i need to continue to learn c++

code with comments :

    sf::Vector2i position = sf::Mouse::getPosition(window); // get the position of the cursor inside the window

        string title ( "SFML ");//create a string with "sfml" writtent in it

        std::string posx = std::to_string ( position.x );//convert the x coordinate from int to string
        std::string posy = std::to_string ( position.y );//convert the y coordinate from int to string

        title += posx;//add the x coordinate after "sfml" in the title string
        title += " ";//add a space in the title string
        title += posy;//add the y coordinate after the space in the title string

        window.setTitle(title);//set the title of the window to what is written in the title string

Thank you very much Laurent for your answer and for your work SFML is awesome !

« Last Edit: October 08, 2014, 09:31:23 pm by 2ant »

2ant

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Cursor position in title bar
« Reply #6 on: October 09, 2014, 07:53:44 am »
SOLVED

 

anything