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

Author Topic: Pause for input and displaying values  (Read 11985 times)

0 Members and 1 Guest are viewing this topic.

Stupebrett

  • Newbie
  • *
  • Posts: 15
    • View Profile
Pause for input and displaying values
« on: April 18, 2011, 09:00:50 pm »
Hello everyone! I'm pretty new to SFML (started about 3 days ago), and I really like it so far! Though there's two problems I can't seem to solve. I've tried to google them, but found no results. My first question is: How do I make the program stop/pause completely until the user for example presses a key? (Like the _getch() function from the conio.h header). My second question is: How do I display a value? For example; int value = 5. How do I display "value" to the window? Any help would be greatly appreciated :)

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: Pause for input and displaying values
« Reply #1 on: April 19, 2011, 03:15:27 am »
Quote from: "Stupebrett"
How do I display a value? For example; int value = 5. How do I display "value" to the window?

You should read the displaying text tutorial here: http://www.sfml-dev.org/tutorials/1.6/graphics-fonts.php
The console is still a viable source for output too if you don't disable it.

Quote from: "Stupebrett"
My first question is: How do I make the program stop/pause completely until the user for example presses a key? (Like the _getch() function from the conio.h header).

You can change your main loop to the following:
Code: [Select]
// At any point, you can call "wait = true;" in order to make the application wait for a key press
bool wait = false;

while (window.IsOpened())
{
    sf::Event event;
    while (window.GetEvent(event)) // This function has been renamed to PollEvent in SFML 2.0
    {
        if (event.Type == sf::Event::Closed)
            window.Close();
        else if (event.Type == sf::Event::KeyPressed)
            // Stop waiting if a key was pressed
            wait = false;

        // Don't process any other events if we're waiting
        if (!wait)
        {
            // Process your own events here
        }
    }

    // Don't process any drawing or logic if we're waiting
    if (!wait)
    {
        // Process your drawing and logic here

        // Example: Draw a randomized rectangle every frame
        window.Draw(sf::Shape::Rectangle(sf::Randomizer::Random(0, 540), sf::Randomizer::Random(0, 380), 100, 100, sf::Color::White));
        static sf::Clock timer;
        if (timer.GetElapsedTime() >= 4)
        {
            // If four seconds have elapsed since the first frame, call "wait = true;"
            wait = true;
        }
    }

    window.Display();
}

Stupebrett

  • Newbie
  • *
  • Posts: 15
    • View Profile
Pause for input and displaying values
« Reply #2 on: April 19, 2011, 04:00:25 pm »
Thanks for answering! The way you did the "wait" program was actually how I did it too :P. I were just wondering if there was an easier way. But I still can't seem to solve the problem with displaying values. I've read the link you posted several times. I've also tried to just put value instead of "text", for example:
Code: [Select]
int value = 5;
sf::String val(value);

But this doesn't work. :/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pause for input and displaying values
« Reply #3 on: April 19, 2011, 04:08:32 pm »
You shouldn't try to "display a number" but rather "convert a number to a string so that it can be displayed".

The latter is pure C++ that is easily found with Google ;)
Laurent Gomila - SFML developer

Stupebrett

  • Newbie
  • *
  • Posts: 15
    • View Profile
Pause for input and displaying values
« Reply #4 on: April 19, 2011, 04:57:27 pm »
I found some code for a header file that could convert a variable to a std::string, so after that I just assigned that string it to a sf::String. Thanks for the help!