[Solved]Displaying an integer with SDL_TTF
Moderator: Coders of Rage
[Solved]Displaying an integer with SDL_TTF
Well I want to display points in a game, but how would I get an integer to display with SDL_ttf?
Last edited by XianForce on Wed Jul 08, 2009 11:08 am, edited 1 time in total.
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: Displaying an integer with SDL_TTF
http://www.lazyfoo.net/SDL_tutorials/lesson07/index.php
Follow that and use something like:
Follow that and use something like:
Code: Select all
char text[255];
sprintf(text, "Score: %d", player.getScore());
message = TTF_RenderText_Solid(font, text, textColor);
Re: Displaying an integer with SDL_TTF
C/C++ - sprintf:
http://www.cplusplus.com/reference/clib ... o/sprintf/
C++ - stringstream:
http://www.cplusplus.com/reference/iost ... ingstream/
C++ & Boost - lexical_cast:
http://www.boost.org/doc/libs/1_39_0/li ... l_cast.htm
http://www.cplusplus.com/reference/clib ... o/sprintf/
C++ - stringstream:
http://www.cplusplus.com/reference/iost ... ingstream/
C++ & Boost - lexical_cast:
http://www.boost.org/doc/libs/1_39_0/li ... l_cast.htm
Re: Displaying an integer with SDL_TTF
TTF takes a const char* so that wouldn't work... right?Bakkon wrote:http://www.lazyfoo.net/SDL_tutorials/lesson07/index.php
Follow that and use something like:
Code: Select all
char text[255]; sprintf(text, "Score: %d", player.getScore()); message = TTF_RenderText_Solid(font, text, textColor);
- rolland
- Chaos Rift Regular
- Posts: 127
- Joined: Fri Dec 21, 2007 2:27 pm
- Current Project: Starting an Android app soon
- Favorite Gaming Platforms: PS1, N64
- Programming Language of Choice: C++
- Location: Michigan, US
Re: Displaying an integer with SDL_TTF
I think that would work. If I remember correctly, that's how I did it when I was porting Pickin' Sticks to SDL.
I'll write a signature once I get some creativity and inspiration...
Re: Displaying an integer with SDL_TTF
It doesn't work for me =(... says invalid conversion from char to const char*rolland wrote:I think that would work. If I remember correctly, that's how I did it when I was porting Pickin' Sticks to SDL.
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: Displaying an integer with SDL_TTF
Are you sure you're doing it right?
Because if it said invalid conversion from char, you aren't doing what Bakkon said. You're supposed to be passing a char*, and an array of chars can be passed as a char*
so with Bakkon's code
char text[255]; // create a char* that points to 255 chars
sprintf(text, "Score: %d", player.getScore());
message = TTF_RenderText_Solid(font, text, textColor); // text is a char* because its an array of chars
If you still can't get your head around it, it could be written like this
char* text; // create a char*
text = new char[255]; // allocate memory (C++ style)
sprintf(text, "Score: %d", player.getScore());
message = TTF_RenderText_Solid(font, text, textColor);
If you still can't get your head around it, revise pointers.
Because if it said invalid conversion from char, you aren't doing what Bakkon said. You're supposed to be passing a char*, and an array of chars can be passed as a char*
so with Bakkon's code
char text[255]; // create a char* that points to 255 chars
sprintf(text, "Score: %d", player.getScore());
message = TTF_RenderText_Solid(font, text, textColor); // text is a char* because its an array of chars
If you still can't get your head around it, it could be written like this
char* text; // create a char*
text = new char[255]; // allocate memory (C++ style)
sprintf(text, "Score: %d", player.getScore());
message = TTF_RenderText_Solid(font, text, textColor);
If you still can't get your head around it, revise pointers.
Re: Displaying an integer with SDL_TTF
that was a typo... there was supposed to be the asterisk after char...
So really it was:
invalid conversion from char* to const char*, but I tried it again and it worked... I don't know what I did differently, cause that was the method I tried before I even posted this thread, well thanks for the help guys =)
So really it was:
invalid conversion from char* to const char*, but I tried it again and it worked... I don't know what I did differently, cause that was the method I tried before I even posted this thread, well thanks for the help guys =)