Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 EvanClark   Members   -  Reputation: 130

Like
1Likes
Like

Posted 18 January 2013 - 03:00 AM

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?



Sponsor:

#2 HAM   Members   -  Reputation: 176

Like
2Likes
Like

Posted 18 January 2013 - 03:15 AM

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, 18 January 2013 - 03:15 AM.


#3 BitMaster   Crossbones+   -  Reputation: 3903

Like
3Likes
Like

Posted 18 January 2013 - 03:21 AM

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


#4 BaneTrapper   Members   -  Reputation: 1166

Like
2Likes
Like

Posted 18 January 2013 - 04:34 AM

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

#include <stringstream>

Second declare a object of stringstream

std::stringstream objSS;

Third store data intro stringstream

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


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#5 BitMaster   Crossbones+   -  Reputation: 3903

Like
1Likes
Like

Posted 18 January 2013 - 05:44 AM

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

#6 BaneTrapper   Members   -  Reputation: 1166

Like
0Likes
Like

Posted 18 January 2013 - 06:14 AM

<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.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#7 swiftcoder   Senior Moderators   -  Reputation: 9860

Like
1Likes
Like

Posted 18 January 2013 - 09:59 AM

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

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.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS