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

Author Topic: How to display numbers in a window?  (Read 21340 times)

0 Members and 1 Guest are viewing this topic.

Quilty

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
How to display numbers in a window?
« on: June 26, 2012, 08:47:00 pm »
Hi everyone :)

I am using SFML 2.0 with Visual Studio 2010 and trying to display some numbers in a window. I want the window to read a certain integer value (int number) and to display this number every time I press a button in the window. However, I want to link this button to a command that states int number = number + 1. Basically the window should update the number every time the button is clicked. I'm not sure if I should use pointers, and I'm a beginner in C++.

I hope you can help me.  :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to display numbers in a window?
« Reply #1 on: June 26, 2012, 09:33:31 pm »
I guess you should keep reading C++ books for a bit longer... ::)

For instance when programming one normally doesn't speak of commands, but statements, function calls, etc. or for your task there's completly no need to use pointers, yet you're thinking about using it.

SFML isn't a library that can teach you all the basics of programming. I guess you better stick with the console and calculate some stuff there for a few month.

And if you want to use SFML, then look at the example given in the documentation, strip it down to only hold the part which uses sf::Text, then search with google for 'int to string', so you know how you get an integer to a string to a sf::Text and then you can start thinking about a button. This can be easily achieve with a sf::RectangleShape and with sf::Mouse and the contains() function of sf::RectangleShape you can check if the user has clicked on the button.

Good luck!
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Quilty

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: How to display numbers in a window?
« Reply #2 on: June 26, 2012, 11:44:15 pm »
Thank you for your suggestions!

I found the following code, but I'm not sure it's the best for SFML 2.0. Other users suggested some other code, but I found this one to be easiest to understand, and it seems to work fine in 2.0.


int score = 123456; // the score to print

sf::Text mytext;
std::stringstream ss;  // #include <sstream>
ss << score;

mytext.setString( ss.str().c_str() );
   

window.draw(mytext);

Quilty

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: How to display numbers in a window?
« Reply #3 on: June 28, 2012, 12:08:31 am »
I tried writing a code. The purpose of it is to display a number on the screen, and then every time a part of the screen is clicked the number is increased by 100. However, this doesn't seem to work, so if you could help me out with this, I'd be grateful.  :)

#include <SFML/Window.hpp>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <SFML/Graphics.hpp>

void HandleMouseClick(sf::Mouse::Button click, int x, int y);


int game = 1000;


int main()
{

       
    sf::RenderWindow window(sf::VideoMode(1024, 768), "My window");
       

        sf::Text mytext;
        std::stringstream ss;  // #include <sstream>
        ss << game;
        mytext.setString( ss.str().c_str() );
        window.draw(mytext);

        window.display();
       

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(event.type == sf::Event::MouseButtonPressed)
                HandleMouseClick(event.mouseButton.button, event.mouseMove.x, event.mouseMove.y);
                                       
                        // "close requested" event: we close the window
            else if (event.type == sf::Event::Closed)
                window.close();
        }
       
        }

        return 0;

        }

void HandleMouseClick(sf::Mouse::Button button, int x, int y)
{
       if(x >= 0 && x < 1023 && y >= 145 && y < 380)
        {
            game = game + 100;

        }

}
« Last Edit: June 28, 2012, 01:01:31 am by Quilty »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to display numbers in a window?
« Reply #4 on: June 28, 2012, 12:48:45 am »
Those little fancy buttons above the textbox you're putting your text in on this forum all have a function. The dropdown box on the outer right which says 'Code' will let you choose 'C++' which will created a code=cpp tag which highlights every code that you put inbetween those tags. ::)

For you problem:

You're calling the draw(), display() function and int to string convertion outside of your gameloop, which means it will get updated, drawn and displayed once and never more.
Also you should look into classes, so you can understand more of C++ and SFML and could write nicer code (e.g. avoid global variables).

Try this:

#include <string>
#include <sstream>
#include <stdlib.h>
#include <SFML/Graphics.hpp>

int game = 1000;

void HandleMouseClick(sf::Mouse::Button click, int x, int y);

int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 768), "My window");
    sf::Text mytext;
    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(event.type == sf::Event::MouseButtonPressed)
                HandleMouseClick(event.mouseButton.button, event.mouseMove.x, event.mouseMove.y);  
            // "close requested" event: we close the window
            else if (event.type == sf::Event::Closed)
                window.close();
        }

        // Update the text
        std::stringstream ss;
        ss << game;
        mytext.setString(ss.str());
       
        // Draw the text
        window.draw(mytext);
       
        // Show the window
        window.display();
    }

    return 0;
}

void HandleMouseClick(sf::Mouse::Button button, int x, int y)
{
    if(x >= 264 && x < 800 && y >= 300 && y < 325)
    {
        game = game + 100;
    }
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Quilty

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: How to display numbers in a window?
« Reply #5 on: June 28, 2012, 12:58:13 am »
Thank you very much for your help! I'll be sure to use the proper tags in the future.

The code you suggested works, but now each number is being drawn over the previous one, so if you can imagine 1000, 1200 etc. all jumbled one over the other. I was hoping that each number would get replaced by the next one. Perhaps there is a way to achieve that? Is there maybe a way to delete the first number and then display the next one as soon as the button is clicked?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to display numbers in a window?
« Reply #6 on: June 28, 2012, 01:08:24 am »
This can easily be achived by calling window.clear(); just before the window.draw(mytext);.

I'm not sure if you just want to achive this or dig deeper into C++/SFML, if so you really should try to understand what's going on, so you can start creating your own code instead of just copying stuff together. ;)

For C++ there are plenty of good and not so good books and tutorials out there.
For SFML I suggest you take a look at the diffrent tutorial sections:
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Quilty

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: How to display numbers in a window?
« Reply #7 on: June 28, 2012, 01:16:01 am »
I'll definitely try and do that. Thanks for the links and all the help. :)