# Scoring system using SDL

This topic is 3178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to create a scoring system using the true type font (SDL_ttf.lib) in SDL C++, but since it only allows strings to be used how do I get around this so I can display my score being kept in an integer variable? I have looked all over google and found nothing that helps me with this. Anyone able to assist me on this? This is the code for true type font:
SDL_Surface *message = NULL;

TTF_Font *font = NULL;

SDL_Color textColor = {255, 255, 255 };

//init font
if(TTF_Init() == -1)
{
return false;
}

font = TTF_OpenFont("lazy.ttf", 28);

//render text
message = TTF_RenderText_Solid(font, "Some text", textColor);

//clean font
SDL_FreeSurface(message);
TTF_CloseFont(font);



##### Share on other sites
Stringstreams:

#include <string>#include <sstream>std::stringstream ss;ss << "Current Score:  " << myScore;SDL_Surface* message = TTF_RenderText_Solid( font, ss.str().c_str(), textColor );

##### Share on other sites
Converting a integer to a string? sprintf, is your new best friend when it somes to formatting text. In your case, you'd do something like this:

  char buffer[50];//note that 50 is just an arbitrary value in this case  sprintf (buffer, "%i", myScore);//or  sprintf (buffer, "Player: %i, Opponent: %i", playerScore, opponentScore);  SDL_Surface* message = TTF_RenderText_Solid( font, buffer, textColor );

##### Share on other sites
Quote:
 Original post by NinjaMonkeyPirateConverting a integer to a string? sprintf, is your new best friend when it somes to formatting text. In your case, you'd do something like this:*** Source Snippet Removed ***
You might want to give this a read.

@The OP: I'd stick with streams (or boost::lexical_cast) if I were you, but for more information on the topic, read the article linked above (after which you'll be able to weigh the pros and cons of the different approaches that are available, and decide for yourself which is most appropriate for the stated problem).

##### Share on other sites
Thank you guys so much I found something similar but it lacked the instructions before hand to fully handle it this way.

##### Share on other sites
Quote:
 Original post by jykYou might want to give this a read.

<offtopic>

Why the complaint about extra memory allocation for std::stringstream?
void PrettyFormat(int i, char *buf, int buflen){    std::streamstream ss;    ss.rdbuf()->pubsetbuf(buf, buflen);    ss << i;}

Am I missing something? (Though I do agree it's not overly pretty [grin])

</offtopic>

##### Share on other sites
I still prefer using sprintf() (or snprintf() as the article recommends, sorta) to using streams. Why? Clarity. I can look at a printf()'s input and almost immediately know what it's output should be, streams not so much. Of course, you have to be intelligent about your buffer sizes but that's a bit like saying cars are tricky to use because you have to always remember to put gas in them. :p

1. 1
2. 2
JoeJ
14
3. 3
4. 4
5. 5
frob
11

• 13
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632178
• Total Posts
3004604

×