GP2X Including A Font With Sdl


craigix

Mega GP Mania
Joined
Feb 3, 2003
Messages
11,008
Location
England
Website
twitter.com
Does anyone know if it is possible to include a font with an SDL program? And if so how it is done?

Thanks,

Craig
 
Use SDL_ttf library which can load TrueType font files. Documentation is here.

The basics are:
  1. Call TTF_Init()
  2. Load font with TTF_OpenFont
  3. Render font to a surface with TTF_RenderText_Solid or TTF_RenderText_Shaded or TTF_RenderText_Blended
  4. Blit surface
 
I thought he meant he wants to include the font in the binary instead of loading it. I noticed the use of the font lib adds a huge chunk to the size of the program. If I was only going to display fixed size fonts I would just make a bitmap of them and use that instead. In fact that is what I want to have a go at putting together today.
 
There's some nice, easy to follow bitmap font code in the source for FORE (link in my sig) that i, for all intents and purposes, stole from the code of Abe's Adventure that might help you get going.
 
Mr.Jabberwocky posted on Oct 13 2006 at 08:39 AM said:
I thought he meant he wants to include the font in the binary instead of loading it.

Yep, you're right - ignore what I said :)

PS: sterm includes a header file containing its font
 
Last edited by a moderator:
craigix posted on Oct 13 2006 at 03:00 AM said:
Does anyone know if it is possible to include a font with an SDL program? And if so how it is done?
If you want to have a font bitmap compiled into your code and used from there, you can do it using SFont (as criticalbeeb does) but use IMG_Load_RW to get the font bitmap from memory, rather than getting it from a file.
 
Last edited by a moderator:
You use rwops to load any data thats included, but I'm sure I read somewhere once that ttf fonts don't work too well in this way. If you want to use ttf instead of bitmap fonts then you could always try including the font, write the data out to /tmp when the app loads, then use the normal ttf functions to load it back in again.
 
woogal posted on Oct 13 2006 at 09:42 AM said:
You use rwops to load any data thats included, but I'm sure I read somewhere once that ttf fonts don't work too well in this way. If you want to use ttf instead of bitmap fonts then you could always try including the font, write the data out to /tmp when the app loads, then use the normal ttf functions to load it back in again.

That does seem the only way to do it, bit crazy isn't it? ;)
 
Last edited by a moderator:
craigix posted on Oct 13 2006 at 10:42 AM said:
woogal posted on Oct 13 2006 at 09:42 AM said:
You use rwops to load any data thats included, but I'm sure I read somewhere once that ttf fonts don't work too well in this way. If you want to use ttf instead of bitmap fonts then you could always try including the font, write the data out to /tmp when the app loads, then use the normal ttf functions to load it back in again.

That does seem the only way to do it, bit crazy isn't it? ;)

I too had this problem when I first started coding. SDL will not load in a ttf using rwops it wont work with SDL_LoadMusic either but works fine with the loadwav function I did some checking into the libs for sdl and found out that they were incomplete and not implemented the functions are there but they do nothing. :blink:
 
Last edited by a moderator:
TTF_Font *TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize)

src
The source SDL_RWops as a pointer. The font is loaded from this.
freesrc
A non-zero value mean is will automatically close/free the src for you.
ptsize
Point size (based on 72DPI) to load font as. This basically translates to pixel height.

Load src for use as a font, at ptsize size. This is actually TTF_OpenFontIndexRW(src, freesrc, ptsize, 0) This can load TTF and FON formats. Using SDL_RWops is not covered here, but they enable you to load from almost any source. NOTE: src is not checked for NULL, so be careful.

Code:
// load font.ttf at size 16 into font
TTF_Font *font;
sample=TTF_OpenFontRW(SDL_RWFromFile("font.ttf"), 1, 16);
if(!sample) {
	printf("TTF_OpenFontRW: %s\n", TTF_GetError());
	// handle error
}
/*
Note that this is unsafe because we don't check the validity of the SDL_RWFromFile's returned pointer.
*/

i haven't tried this, but i remember gp2x_future_coder having problem while back at http://www.gp32x.de/board/index.php?showtopic=29719

can somebody else verify it? (i don't have my gp2x here (at work))..
 
Last edited by a moderator:
GnoStiC posted on Oct 14 2006 at 09:33 AM said:
TTF_Font *TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize)

src
The source SDL_RWops as a pointer. The font is loaded from this.
freesrc
A non-zero value mean is will automatically close/free the src for you.
ptsize
Point size (based on 72DPI) to load font as. This basically translates to pixel height.

Load src for use as a font, at ptsize size. This is actually TTF_OpenFontIndexRW(src, freesrc, ptsize, 0) This can load TTF and FON formats. Using SDL_RWops is not covered here, but they enable you to load from almost any source. NOTE: src is not checked for NULL, so be careful.

Code:
// load font.ttf at size 16 into font
TTF_Font *font;
sample=TTF_OpenFontRW(SDL_RWFromFile("font.ttf"), 1, 16);
if(!sample) {
	printf("TTF_OpenFontRW: %s\n", TTF_GetError());
	// handle error
}
/*
Note that this is unsafe because we don't check the validity of the SDL_RWFromFile's returned pointer.
*/

i haven't tried this, but i remember gp2x_future_coder having problem while back at http://www.gp32x.de/board/index.php?showtopic=29719

can somebody else verify it? (i don't have my gp2x here (at work))..

This will works fine SDL_RWFromFile("font.ttf"). But this will not SDL_RWFromMem. SDL_RWFromMem is not completed inside of the sdl libs so that is way it doesn't work the same with a few other load from memory functions.
 
Last edited by a moderator:
Back
Top