SFML community forums
Help => System => Topic started by: Stupebrett 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 :)
-
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.
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:
// 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();
}
-
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:
int value = 5;
sf::String val(value);
But this doesn't work. :/
-
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 ;)
-
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!