[Solved]Displaying an integer with SDL_TTF

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

[Solved]Displaying an integer with SDL_TTF

Post by XianForce »

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.
User avatar
Bakkon
Chaos Rift Junior
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

Post by Bakkon »

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); 
Cache
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 1
Joined: Tue Jul 07, 2009 8:36 pm

Re: Displaying an integer with SDL_TTF

Post by Cache »

XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Displaying an integer with SDL_TTF

Post by XianForce »

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); 
TTF takes a const char* so that wouldn't work... right?
User avatar
rolland
Chaos Rift Regular
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

Post by rolland »

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...
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Displaying an integer with SDL_TTF

Post by XianForce »

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.
It doesn't work for me =(... says invalid conversion from char to const char*
User avatar
RyanPridgeon
Chaos Rift Maniac
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

Post by RyanPridgeon »

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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Displaying an integer with SDL_TTF

Post by XianForce »

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 =)
Post Reply