Sign in to follow this  

(My solution has limitations)TTF_RenderText_Solid convert sstream/string 2const char*

Recommended Posts

Hello, I'm going to show you the function in SDL_TTF of TTF_RenderText_Solid(), then show you how I have my code to draw text on a whim though now I guess it's not, or I wouldn't be having this problem, and what possibly solutions I have "TRIED", yet failed in solving this issue.
SDL_Surface *TTF RenderText Solid(TTF_Font *font, const char *text, SDL_Color fg)

now when looking at this I say OK, what is going to be converted into an image as text must be a const char* Hm... OK.. I tried this, but I don't honestly like it. It works, but I'm having trouble trying to truly comprehend it, for it's something I just saw on a webpage, and through trial and error as I do with most code to learn I got it working, for what I wanted to accomplish.
string c_score;
string p_score;
itoa(cpu_score, (char*)c_score.c_str(), 10);
itoa(player_score, (char*)p_score.c_str(), 10);

//i have this function, which is in my gfx class file that draws text on the //screen for my game
gfx->draw_text(font2, (char*)p_score.c_str(), 100, height - 50, 0, 0, 0, 250, 250, 250);
gfx->draw_text(font2, (char*)c_score.c_str(), width - 100, height - 50, 0, 0, 0, 250, 250, 250);

As you can see it work, but how do I add, and say hey "Player's Score: " append score from integer to const char* I also tried sstream, but unfortunately got nowhere with it. I have this for now, and I guess it will do, but I'd like to know how I can append strings integers together and then convert them to const char* when I need to without going through for loops, calling miscellaneous functions that are truly not needed. THanks for the replies,

Share this post

Link to post
Share on other sites
Eh, no. That definantly does NOT work. The behavior you are engaging in right there is horribly undefined.

first off, to convert integers into string objects is fairly trivial...

int playerScore;
std::ostringstream ss;
ss<<"Player Score: "<<playerScore;

secondly, those itoa calls should not be there. The behavior is completely and utterly undefined. Not to mention, the pointer returned by c_str() is a const value_type*.

Share this post

Link to post
Share on other sites
I tried that earlier before resulting to that, and I got a error in my debug window


can't convert const char* to char*

so I went with the solution that did work. I know it would be ridiculed, but if I can get it to work, and ridiculed where I can get it working again and happily accepted here then I'll be happy.

I'll try your code.

edit: I know it was trivial, for I've done it many of times before in other games, but for some reason thought hey const char* must be const char* not string, sstream, string*, etc... So I tried everything in the world from converting sstream to const char*, but I failed :(

Thanks! ;)

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