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

Author Topic: Question about refreshing altered number on the screen.  (Read 1306 times)

0 Members and 1 Guest are viewing this topic.

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Question about refreshing altered number on the screen.
« on: July 05, 2014, 05:47:31 pm »
Hi there, I've spent some days trying to find a solution for the following problem (in Visual Cpp 10/SFML 2.1), but couldn't find anything helpful on the net. I think its only a small line of code that is needed here.

What the program does: After each click on the red recangle, the int variable "myInt" (Start value = 1) is incremented by one. Every such operation is being recorded on the console. So far, so good up to here.

What I need now, is some line of code (I've tried every possible combination, but no luck :-) ) to force the variable to "refresh" itself by showing its new value on the screen. But I don't know how to do that. Has it something to do with the template? (I've been reading a cpp book for some weeks now, but templates aren't covered there really (and if at all, only briefly)) Any help apperciated!

#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <iostream>

template <typename T>
std::string toString(T arg)
{
        std::stringstream ss;
        ss << arg;                        
        return ss.str();
}

int main()
{
        int myInt = 1;

        sf::RenderWindow mMainWindow(sf::VideoMode(500, 300), "Map");

        sf::RectangleShape rectangle;
        rectangle.setSize(sf::Vector2f(70, 70));
        rectangle.setFillColor(sf::Color::Red);
        rectangle.setPosition(30, 30);
       
        sf::Font myFont;
        myFont.loadFromFile("menufont.otf");

        sf::Text myText(toString<int>(myInt), myFont);

        myText.setPosition(100,100);
        myText.setCharacterSize(50);
        myText.setColor(sf::Color(250,0,0,255));

        while (mMainWindow.isOpen())
        {
        sf::Event event;
        while (mMainWindow.pollEvent(event))
        {
         if (event.type == sf::Event::Closed)
                mMainWindow.close();
        }
        if  (rectangle.getGlobalBounds().contains(mMainWindow.mapPixelToCoords(sf::Mouse::getPosition(mMainWindow))))
                if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
                        myInt++;
                        std::cout << myInt;
                                                                             // code for updating myInt Variable.
        }

       

        mMainWindow.clear();
        mMainWindow.setFramerateLimit(30);
        mMainWindow.draw(myText);
        mMainWindow.draw(rectangle);
        mMainWindow.display();
        }

        return 0;
}
« Last Edit: July 05, 2014, 05:52:12 pm by Bogdan »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Question about refreshing altered number on the screen.
« Reply #1 on: July 05, 2014, 06:10:35 pm »
In your window loop
myText.setString(toString<int>(myInt));
or even better
myText.setString(std::to_string(myInt));
Source:
http://sfml-dev.org/documentation/2.1/classsf_1_1Text.php#a7d3b3359f286fd9503d1ced25b7b6c33


You shouldn't set the framerate limit every frame, it doesn't make sense at all.

For input handling you might want to do this:
    while (mMainWindow.pollEvent(event))
    {
     if (event.type == sf::Event::Closed)
        mMainWindow.close();
     else if(event.type == sf::Event::MouseButtonPressed)
                if(event.mouseButton.button == sf::Mouse::Left)
                {
                     sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y));
                     if(rectangle.getGlobalBounds().contains(mousecoords))                                                    
                     {
                                        //stuff
                     }
                }
    }
« Last Edit: July 05, 2014, 06:37:44 pm by Strelok »

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Question about refreshing altered number on the screen.
« Reply #2 on: July 06, 2014, 06:43:11 am »
Many thanks, your code looks and works really better. As for the frameratelimit, its for the protection of my hardware :-)  I've put it now before the while loop and directly after the sf::window...
Now it looks like this:

#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <iostream>

template <typename T>
std::string toString(T arg)
{
        std::stringstream ss;
        ss << arg;                        
        return ss.str();
}

int main()
{
        sf::RenderWindow mMainWindow(sf::VideoMode(500, 300), "Map");
        mMainWindow.setFramerateLimit(30);

        unsigned long long myInt = 100;


        sf::RectangleShape rectangle;
        rectangle.setSize(sf::Vector2f(70, 70));
        rectangle.setFillColor(sf::Color::Red);
        rectangle.setPosition(30, 30);
       
        sf::Font myFont;
        myFont.loadFromFile("menufont.otf");

        sf::Text myText(toString<unsigned long long>(myInt), myFont);

        myText.setPosition(100,100);
        myText.setCharacterSize(50);
        myText.setColor(sf::Color(250,0,0,255));

        while (mMainWindow.isOpen())
        {
                sf::Event event;
                while (mMainWindow.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                mMainWindow.close();
                        else if(event.type == sf::Event::MouseButtonPressed)
                                        if(event.mouseButton.button == sf::Mouse::Left)
                                        {
                                                sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));
                                                if(rectangle.getGlobalBounds().contains(mousecoords))                                                    
                                                {
                                                        myInt++;
                                                        myText.setString(toString<unsigned long long>(myInt));
                                                }
                                        }
                }


        mMainWindow.clear();
        mMainWindow.draw(myText);
        mMainWindow.draw(rectangle);
        mMainWindow.display();
        }

        return 0;
}




 


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Question about refreshing altered number on the screen.
« Reply #3 on: July 06, 2014, 09:14:36 am »
sf::Text myText(toString<unsigned long long>(myInt), myFont);
Function templates can infer their template arguments:
sf::Text myText(toString(myInt), myFont);

But as Ixrec already mentioned, you should use std::to_string() if the compiler supports it. It's also much more efficient than constructing a whole stream for a single conversion.

And I'd really use curly brackets around every multiline if statement, otherwise it gets quickly unreadable.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything