How do I get SDL_TTF to print out numbers?

OK, let’s say I’ve got a game where each time you hit something, 1 get’s added
to your current score. My score is an integer/long, but I want to print it out
to the screen using SDL_TTF.

Here’s what SDL_TTF is looking for with TTF_RenderTextSolid()
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg)

problem is, I’d need to do something like this:
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, int myScore, SDL_Color fg)

How do I go about doing this?

OK, let’s say I’ve got a game where each time you hit something, 1
get’s added to your current score. My score is an integer/long, but I
want to print it out to the screen using SDL_TTF.

Here’s what SDL_TTF is looking for with TTF_RenderTextSolid()
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text,
SDL_Color fg)

problem is, I’d need to do something like this:
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, int myScore,
SDL_Color fg)

How do I go about doing this?

char str[128];
SDL_Surface *sur;
sprintf(str, “%8d”, myScore);
sur = TTF_RenderText_Solid(font, str, fg);

This will probably be very slow. You might want to pre-render all the
digits and then blit them.

PallavOn Sun, 27 Mar 2005 08:30:29 +0000 (UTC) James OMeara wrote:

For C:
man 3 printf
Just place the number in a string, and print that.

For C++:
Just << it to a string, then use .c_str( ) to be able to use it in the
RenderText function.

Regards,

Pieter HulshoffOn Sunday 27 March 2005 10:30, James OMeara wrote:

OK, let’s say I’ve got a game where each time you hit something, 1 get’s
added to your current score. My score is an integer/long, but I want to
print it out to the screen using SDL_TTF.
How do I go about doing this?

char str[128];
SDL_Surface *sur;
sprintf(str, “%8d”, myScore);
sur = TTF_RenderText_Solid(font, str, fg);

This will probably be very slow. You might want to pre-render all
the digits and then blit them.

Premature optimization. Measure how much time the straightforward
version actually takes up relative to the rest before you obfuscate
your code (and increase your memory footprint caching two handfuls of
digit-sized surfaces).

b

char str[128];
SDL_Surface *sur;
sprintf(str, “%8d”, myScore);
sur = TTF_RenderText_Solid(font, str, fg);

This will probably be very slow. You might want to pre-render all
the digits and then blit them.

Premature optimization. Measure how much time the straightforward
version actually takes up relative to the rest before you obfuscate
your code (and increase your memory footprint caching two handfuls of
digit-sized surfaces).

I second this philosophy. Though I don’t think in this particular
situation, there’d be anything obfuscated about it. Moreover, for any
decently sized game, this wouldn’t be a large increase of memory.
Though you’re correct to say that it wouldn’t offer that much of a CPU
bonus.On Mar 27, 2005, at 6:35 AM, Brian Raiter wrote:

b


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

For C++:
Just << it to a string, then use .c_str( ) to be able to use it in the
RenderText function.
hmm, I’ve never seen anything about <<'ing it to a string. I googled for
answers, but I’m not coming up with anything, and the code that
I tried didn’t work.

Here’s what I tried

int score;
string theScore;

score=200;

theScore << score;

surface = TTF_RenderText_Solid(font, theScore.c_str(), fg);

I’ve tried this also, and I know it works:
string theScore;
theScore=“25”;
surface = TTF_RenderText_Solid(font, theScore.c_str(), fg);

so it’s just a matter of the <<

Nah! You only need ONE two-handful-of-digits-sized surface! :wink:

-bill!On Sun, Mar 27, 2005 at 03:35:12AM -0800, Brian Raiter wrote:

Premature optimization. Measure how much time the straightforward
version actually takes up relative to the rest before you obfuscate
your code (and increase your memory footprint caching two handfuls of
digit-sized surfaces).

It appears you are correct. I could have sworn I found an article about this
somewhere (new overloaded function?), but I can’t for the life of me refind
it. My apologies for the confusion.

Regards,

PieterOn Monday 28 March 2005 06:35, James OMeara wrote:

For C++:
Just << it to a string, then use .c_str( ) to be able to use it in the
RenderText function.

hmm, I’ve never seen anything about <<'ing it to a string. I googled for
answers, but I’m not coming up with anything, and the code that
I tried didn’t work.

i know alot of people are gonna say “ewww”. call me old school but…

#include <stdio.h>
char Buffer[256];
int Number;

Number=45;

sprintf(Buffer,"%i",Number);

Buffer then contains “45” as a string.

nice and easy, gotta love sprintf (unless your worried about security hehe)> ----- Original Message -----

From: phulshof@xs4all.nl (Pieter Hulshoff)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Sunday, March 27, 2005 10:31 PM
Subject: Re: [SDL] Re: How do I get SDL_TTF to print out numbers?

On Monday 28 March 2005 06:35, James OMeara wrote:

For C++:
Just << it to a string, then use .c_str( ) to be able to use it in the
RenderText function.

hmm, I’ve never seen anything about <<'ing it to a string. I googled for
answers, but I’m not coming up with anything, and the code that
I tried didn’t work.

It appears you are correct. I could have sworn I found an article about
this
somewhere (new overloaded function?), but I can’t for the life of me
refind
it. My apologies for the confusion.

Regards,

Pieter


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Pieter Hulshoff wrote:> On Monday 28 March 2005 06:35, James OMeara wrote:

For C++:
Just << it to a string, then use .c_str( ) to be able to use it in the
RenderText function.

hmm, I’ve never seen anything about <<'ing it to a string. I googled for
answers, but I’m not coming up with anything, and the code that
I tried didn’t work.

It appears you are correct. I could have sworn I found an article about this
somewhere (new overloaded function?), but I can’t for the life of me refind
it. My apologies for the confusion.

What you’re talking about are called string streams. Here’s an article:

http://www.fredosaurus.com/notes-cpp/strings/stringstream.html

The end of the page has the code.


Jon

It appears you are correct. I could have sworn I found an article about this
somewhere (new overloaded function?), but I can’t for the life of me refind
it. My apologies for the confusion.

No problem, I sat something about a stream or something, and I’ll see if I can
build upon that. Your c_str() part really helped, though

Thanks!

i know alot of people are gonna say “ewww”. call me old school but…

#include <stdio.h>
char Buffer[256];
int Number;

Number=45;

sprintf(Buffer,"%i",Number);

Buffer then contains “45” as a string.

nice and easy, gotta love sprintf (unless your worried about security
hehe)

This is exactly how I do it! It’s so easy, and quick… But please tell me
about the security issue? I am not terribly experienced with security
issues. I figured quick and easy = good. But maybe it doesn’t in this case?
-Dave

The vulnerability really comes from a lack of bound checking in the
sprintf (and all printf family of functions). There is also ‘format
string’ vulnerabilities, especially when it is possible that a string
is being passed to the format string such as:

#include <stdio.h>

int
main(int argc, char ** argv)
{
char name[40];
printf(“enter your name: “);
scanf(”%s”, &name);
printf(“your name is: %s\n”, name);
return 0;
}

I’ve heard that if you pass format characters through the prompt you
can potentially gain access to other information in the stack.

Given the information above, most people have generally considered all
of the printf family to be unsafe. However, I would not be too worried
about exploits, etc. in a game. If you were working on postfix,
sendmail, or Apache then this is a lot more dangerous because these
overflows could lead to sensitive information being accessed or even
root access.

Despite what I said about this just being a game, keep in mind that
bad habits are hard to break and you may not always be able to
guarantee what type of projects you will work on in the future. So, if
you are just trying to learn some stuff, see if you can find creative
alternatives to printf, if you are working on commercial-quality
software, find an alternative. If you are just doing proof of concept
work, don’t worry about it then.

Just my IMHO :slight_smile:

-WesOn Mon, 28 Mar 2005 08:02:58 -0600, David Olsen wrote:

i know alot of people are gonna say “ewww”. call me old school but…

#include <stdio.h>
char Buffer[256];
int Number;

Number=45;

sprintf(Buffer,"%i",Number);

Buffer then contains “45” as a string.

nice and easy, gotta love sprintf (unless your worried about security
hehe)

This is exactly how I do it! It’s so easy, and quick… But please tell me
about the security issue? I am not terribly experienced with security
issues. I figured quick and easy = good. But maybe it doesn’t in this case?
-Dave

#include <stdio.h>
char Buffer[256];
int Number;

Number=45;

sprintf(Buffer,"%i",Number);

Buffer then contains “45” as a string.

nice and easy, gotta love sprintf (unless your worried about security

This is exactly how I do it! It’s so easy, and quick… But please tell me
about the security issue? I am not terribly experienced with security
issues. I figured quick and easy = good. But maybe it doesn’t in this case?
-Dave

Anytime you write to a memory buffer, there is the potential for a buffer
overflow (unless the function doing the writing checks the buffer size, which
sprintf doesn’t). However, converting an integer or a double into ascii with
sprintf and a buffer size of 256 is not going to overflow.

Overflowing a buffer will scribble data over memory other than the buffer. In
this case it might be the stack, which could corrupt your return address and
then execute random code when returning from the function. This is a popular
exploit among crackers; more effective on Windows than other OSs (simply
because Windows allows ordinary users priviledges that other OSs don’t).

JeffOn Monday 28 March 2005 06:02 am, David Olsen wrote:

Yes, if you use printf this way:

printf(name);

Then name can contain format characters, and they will be parsed and
stack will be read.On Mon, Mar 28, 2005 at 11:08:15AM -0500, Wes Wannemacher wrote:

int
main(int argc, char ** argv)
{
char name[40];
printf(“enter your name: “);
scanf(”%s”, &name);
printf(“your name is: %s\n”, name);
return 0;
}

I’ve heard that if you pass format characters through the prompt you
can potentially gain access to other information in the stack.


Petri Latvala
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050329/9f28b2df/attachment.pgp

James OMeara writes:

It appears you are correct. I could have sworn I found an article about this
somewhere (new overloaded function?), but I can’t for the life of me refind
it. My apologies for the confusion.

No problem, I sat something about a stream or something, and I’ll see if I can
build upon that. Your c_str() part really helped, though

I find building strings from char[]s, ints, FooBars, etc. so common I
wrote this:

//
// -------------------------------------------------------------------
// STR
//
// Macro STR(x << y << z) builds a string by concatenating x, y, z.
// -------------------------------------------------------------------
//

#include
#include

class Str {
public:
Str() {}

template
Str &operator <<(const T &t) {
stream_ << t;
return *this;
}

std::string &str() {
string_ = stream_.rdbuf()->str();
return string_;
}

operator std::string &() {
return str();
}

operator const char *() {
return str().c_str();
}

private:
std::ostringstream stream_;
std::string string_;
};

#define STR(expr) ((Str() << expr).str())

//
// -------------------------------------------------------------------
//

Now you can do:

sur = TTF_RenderText_Solid(font, STR(100), fg);

or:

sur = TTF_RenderText_Solid(font, STR("Level " << level), fg);

or:

sur = TTF_RenderText_Solid(font, STR(minutes << “:” << seconds << “.” << hundredths), fg);

etc.

HTH