SDL-ttf (PATCH)

Nick Whitelegg wrote:

I attempt to use TTF_Initialized (a global variable representing whether
or not the SDL_TTF library is initialised or not, declared as a static int
in SDL_ttf.c) in the constructor of my TTFont class, to determine whether
or not the SDL_TTF library is initialised or not. I declare it as extern
in the header for my TTFont class.

you’re stuck here. there’s nothing you can do if you want access to this
variable. because the variable is declared “static int TTF_initialized =
0;” this means it is ‘private’ to the SDL_ttf.c source, there’s no way for
you to access it from your own program.

now you can safely call TTF_Init() multiple times and there will be no
problems. you can call TTF_Quit() multiple times too and it will not die,
but the first call to TTF_Quit will really uninitialize the library, so any
other code that still thinks it has initialized the library will surely
cause trouble.

SDL has a “SDL_WasInit” function, here is a patch to add a similar
"TTF_WasInit" function. hopefully you’ll see it in the next release.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: TTF_WasInit.patch
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020903/c10c4989/attachment.asc

Pete Shinners wrote:

now you can safely call TTF_Init() multiple times and there will be no
problems.

i take it back, i’m unsure if you can call TTF_Init() multiple times. it
appears to always init the freetype library. that’d be another easy patch i
guess, but with the TFF_WasInit it won’t matter much, since you can just
check yourself first.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1On Tuesday 03 September 2002 17:20, Pete Shinners wrote:

Nick Whitelegg wrote:

I attempt to use TTF_Initialized (a global variable representing
whether or not the SDL_TTF library is initialised or not, declared as a
static int in SDL_ttf.c) in the constructor of my TTFont class, to
determine whether or not the SDL_TTF library is initialised or not. I
declare it as extern in the header for my TTFont class.

you’re stuck here. there’s nothing you can do if you want access to this
variable. because the variable is declared “static int TTF_initialized =
0;” this means it is ‘private’ to the SDL_ttf.c source, there’s no way
for you to access it from your own program.

now you can safely call TTF_Init() multiple times and there will be no
problems. you can call TTF_Quit() multiple times too and it will not die,
but the first call to TTF_Quit will really uninitialize the library, so
any other code that still thinks it has initialized the library will
surely cause trouble.

SDL has a “SDL_WasInit” function, here is a patch to add a similar
"TTF_WasInit" function. hopefully you’ll see it in the next release.

  • From a design POV I think it would be better to use TTF_initialized as a
    counter of how often TTF_Init() has been called, then only shutting down
    when the counter reaches 0 again.
    This way, you’ll have to remember whether TTF was init when you started up
    so you’ll know whether to call the Quit function or not.

cu,
Nicolai
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9dNagsxPozBga0lwRAi5zAKDKb+csJ9NjiKMEUR/D3tse/qZCigCfT7A0
XfS3LVYztbrMW01obb83cHA=
=p81D
-----END PGP SIGNATURE-----

Hello.

Nicolai Haehnle wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[ … ]

  • From a design POV I think it would be better to use TTF_initialized as a
    counter of how often TTF_Init() has been called, then only shutting down
    when the counter reaches 0 again.
    This way, you’ll have to remember whether TTF was init when you started up
    so you’ll know whether to call the Quit function or not.

SCNR, but what if someone’s code calls it too much and the variable overflows?
I know, I know 2^31 is quite a lot, but all it takes is one messed-up condition
in while().

From code consistency POV it would be much better if TTF_Init() on initialized library
as well as TTF_Quit on uninitialized crashed the program on the spot.

This would help design one’s program more sanely too.–

./lxnt

Thanks, I’ve applied your patch and made it so you can call TTF_Init()
multiple times safely (as long as you call TTF_Quit() as many times.)

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1On Tuesday 03 September 2002 18:59, Alexander Sabourenkov wrote:

Hello.

Nicolai Haehnle wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[ … ]

  • From a design POV I think it would be better to use TTF_initialized
    as a counter of how often TTF_Init() has been called, then only
    shutting down when the counter reaches 0 again.
    This way, you’ll have to remember whether TTF was init when you started
    up so you’ll know whether to call the Quit function or not.

SCNR, but what if someone’s code calls it too much and the variable
overflows? I know, I know 2^31 is quite a lot, but all it takes is one
messed-up condition in while().

From code consistency POV it would be much better if TTF_Init() on
initialized library as well as TTF_Quit on uninitialized crashed the
program on the spot.

This would help design one’s program more sanely too.

Yes and no. Think WinSock for example. That’s another library that needs
Init and Cleanup calls. They deliberately added reference counting, and for
a reason: not all the code that is executed during the lifetime of a
program is written by the programmer of that program. In the WinSock case,
there might be a utility library (e.g. for the HTTP protocol) that needs to
access sockets, too. This library needs to work correctly whether the
program it’s used in initializes WinSock or not.
So unless you’re doing something that can essentially be done only once
(which is really rare; SDL_Init could count as such an example), you should
support some form of reference counting.

About the 2^31: Seriously, if there’s an infinite loop that continually
calls code that calls TTF_Init, then the code in between is likely to
allocate memory (such as C++ classes). The heap will overflow long before
2^31 ever becomes an issue (and why don’t you use an unsigned int anyway?
;)). You can still abort() when the reference counter wraps to 0 if you’re
really paranoid.

cu,
Nicolai
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9dPMLsxPozBga0lwRAtLgAKCP0+XvFKBuKhWtEZpiYfIEnHSU7wCfdmb9
3QGiUZktQLIXEL439bIASOI=
=GOW6
-----END PGP SIGNATURE-----

SCNR, but what if someone’s code calls it too much and the variable overflows?
I know, I know 2^31 is quite a lot, but all it takes is one messed-up condition
in while().

From code consistency POV it would be much better if TTF_Init() on initialized library
as well as TTF_Quit on uninitialized crashed the program on the spot.

This would help design one’s program more sanely too.

Thanks for the feedback on this. I have to admit, global variables and
the ins and outs of them were never my strong point, I never knew that
by making a global variable “static” it became private!

As far as I’m concerned, the TTF_WasInit() would be very useful indeed,
it would be a bit awkward to have to include a specially-patched version
of the SDL_ttf class with my library.

Thanks,
Nick

In C anyway, yes… It’s not private, per-se, but sybol-less… Without
a symbol, no code in any other object file can access it. This is what
static used to mean. In C++ static now has several meanings, depending
on context. With globabl variables, static has the same meaning it did
in C… in a class it refers to a member that can be accessed indepently
of a specific object of that class. In a function it refers to a local
variable that only gets initialized once (when the function first
executes), and doesn’t get destroyed until the program exits…

Hope that helped,

-LorenOn Tue, 2002-09-03 at 13:29, Nick Whitelegg wrote:

Thanks for the feedback on this. I have to admit, global variables and
the ins and outs of them were never my strong point, I never knew that
by making a global variable “static” it became private!

Loren Osborn <linux_dr at yahoo.com> wrote:

Thanks for the feedback on this. I have to admit, global variables
and the ins and outs of them were never my strong point, I never
knew that by making a global variable “static” it became private!

In C anyway, yes… It’s not private, per-se, but sybol-less…
Without a symbol, no code in any other object file can access it.
This is what static used to mean. In C++ static now has several
meanings, depending on context. With globabl variables, static has
the same meaning it did in C…

[…]

In a
function it refers to a local variable that only gets initialized once
(when the function first executes), and doesn’t get destroyed until
the program exits…

this is also true for C not only for C++.

JFYI …
clemens> On Tue, 2002-09-03 at 13:29, Nick Whitelegg wrote: