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

Author Topic: Printing Variables  (Read 3284 times)

0 Members and 1 Guest are viewing this topic.

tpih

  • Newbie
  • *
  • Posts: 1
    • View Profile
Printing Variables
« on: December 19, 2011, 09:46:00 am »
I have searched around google but found nothing. How do I print variables to the screen? I have a score that I would like the user to be able to see.

Thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Printing Variables
« Reply #1 on: December 19, 2011, 09:58:47 am »
The question is not how to draw numbers on screen, but rather how to convert your numbers to strings that you can draw with SFML.

-> Google "c++ int to string" ;)
Laurent Gomila - SFML developer

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
Printing Variables
« Reply #2 on: December 19, 2011, 03:39:13 pm »
Can be as easy as:
Code: [Select]
char out[16];
int score = 5;
sprintf(out, "Score: %i", score)
sf::Text text(out);
renderWindow.Draw(text);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Printing Variables
« Reply #3 on: December 19, 2011, 03:48:52 pm »
Nop, this code is C and unsafe.

There's only one good solution (with the current standard and no third-party libraries):
Code: [Select]
#include <sstream>

std::ostringstream oss;
oss << score;
sf::Text text(oss.str());
Laurent Gomila - SFML developer

Stupebrett

  • Newbie
  • *
  • Posts: 15
    • View Profile
Printing Variables
« Reply #4 on: December 20, 2011, 11:04:12 pm »
If your compiler supports C++11, you can simply use the to_string function.