Sign in to follow this  
EvanClark

[C++/SDL] How to render integers with SDL_TTF

Recommended Posts

EvanClark    130

Hello i am currently creating my first SDL game and i need a little counter on the side of my screen that displays the score however when i do the normal

   message = TTF_RenderText_Solid( font,score textColor );

i get an error stating i cannot convert a int to char*,How can i overcome this?

Share this post


Link to post
Share on other sites
HAM    176

You might not have provided enough information for someone to help.  But I'll take a stab.  Also I think you have a typo and missed a comma in the code you did post.

 

I am assuming 'score' is an int and the function, TTF_RenderText_Solid is expecting a char* for the second arguement.

 

A simple way to do this would be:

 

 

 

char textBuffer[64];

sprintf(textBuffer, "%d", score);  // Write the int 'score' into a char buffer

TTF_RenderText_Solid(font, textBuffer, textColor);

 


sprintf works just like printf but writes into a char buffer instead of standard out.

 

The error you received was pretty straight forward.  You were trying to use a variable of type 'int' when a variable of type 'char*' was expected.

Edited by HAM

Share this post


Link to post
Share on other sites
BitMaster    8651
The OP tagged the topic as C++, so supplying the native C++ way to do this would have been better (especially since it's clear the OP is still lacking a lot of experience and the *printf-family of C function can get you quickly into difficult to find, obscure bugs if you are careless):

#include <sstream>

...

std::ostringstream oss;
oss << score;
TTF_RenderText_Solid(font, oss.str().c_str(), textColor);

Share this post


Link to post
Share on other sites
BaneTrapper    1531

The OP tagged the topic as C++, so supplying the native C++ way to do this would have been better (especially since it's clear the OP is still lacking a lot of experience and the *printf-family of C function can get you quickly into difficult to find, obscure bugs if you are careless):


#include <sstream>

...

std::ostringstream oss;
oss << score;
TTF_RenderText_Solid(font, oss.str().c_str(), textColor);

Google does not provide info on "ostringstream" is that same or equal to "stringstream" ?

 

Answer to question:

To convert "int" to "char" use stringstream.

First include it

[code]#include <stringstream>[/code]

Second declare a object of stringstream

[code]std::stringstream objSS;[/code]

Third store data intro stringstream

[code]int my_int = 100; char my_char = 'a'; std::string my_string = "Good day sir";   objSS << my_int; objSS << my_char; objSS << my_string;   std::cout << objSS.str() << std::endl; 100aGood day  sir [code] To clear already used stringstream [code] objSS.clear();//This will clear any errors that may occured objSS.str(std::string()); //This will clear your stringstream object allowing for reuse[/code]

Share this post


Link to post
Share on other sites
BitMaster    8651
<blockquote class="ipsBlockquote" data-author="BaneTrapper" data-cid="5022847"><p>Google does not provide info on "ostringstream" is that same or equal to "stringstream" ?</p></blockquote><br /><a data-cke-saved-href="http://en.cppreference.com/w/cpp/io/basic_ostringstream" href="http://en.cppreference.com/w/cpp/io/basic_ostringstream">Linky</a>

Share this post


Link to post
Share on other sites
BaneTrapper    1531
<blockquote class="ipsBlockquote" data-author="BaneTrapper" data-cid="5022847"><p>Google does not provide info on "ostringstream" is that same or equal to "stringstream" ?</p></blockquote><br /><a data-cke-saved-href="http://en.cppreference.com/w/cpp/io/basic_ostringstream" href="http://en.cppreference.com/w/cpp/io/basic_ostringstream">Linky</a>

Thank you for the webpage.

Share this post


Link to post
Share on other sites
swiftcoder    18426

[quote name='BaneTrapper' timestamp='1358505267' post='5022847']
Google does not provide info on "ostringstream" is that same or equal to "stringstream" ?[/quote]

All standard library stream classes come in 'input' and 'output' varieties, prefixed respectively with 'i' and 'o'.

 

The non-prefix version is a bidirectional stream which may be used to interleave both input and output on the same underlying stream object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this