Jump to content
  • Advertisement
Sign in to follow this  
wicked357

Scoring system using SDL

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

If you intended to correct an error in the post then please contact us.

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;
}

//load file
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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by NinjaMonkeyPirate
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:

*** 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by jyk
You 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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!