SDL_ttf

Really? The following code works absolutely fine on my machine;

----- 8< -----
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>

int
main ( void )
{
TTF_Font *font;
char *filename;

// This works
if ( TTF_Init() == -1 )
{
fprintf ( stderr, “Unable to initialize SDL_TTF: %s\n”,
TTF_GetError () );
return 1;
}

// This is where it burns
filename = “timesi.ttf”;
font = TTF_OpenFont ( filename, 9 );

if ( font == NULL )
{
fprintf ( stderr, “Unable to open default TTF font: %s\n”,
TTF_GetError () );
return 1;
}

TTF_CloseFont ( font );
TTF_Quit ();

return 0;
}
----- 8< -----

Compiled with;

gcc -O2 -Wall -lSDL -lSDL_ttf -o test test.c

I’m using SDL 1.2.7, ttf version 2.0.6 and gcc version 3.3.3 on Gentoo.
The timesi.ttf font I used is from the corefonts package available from
http://corefonts.sourceforge.net/

HTH,

MattOn Fri, 2004-04-16 at 05:08, Coleman Nitroy wrote:

Documentation for SDL_ttf seems a it lacking so I am stabbing the the
dark here, and from the demo and documentation I was able to scrap
together this for initializing SDL_ttf, but it crashes and burns
every-time:

[Code]

TTF_Font *font
char *filename

// This works
if( TTF_Init() == -1 ) {
fprintf(stderr, “Unable to initialize SDL_TTF: %s\n”, TTF_GetError());
return 1;
}

// This is where it burns
filename = FreeMono.ttf";
font = TTF_OpenFont(filename, 9);
if( font == NULL ) {
fprintf(stderr, “Unable to open default TTF font: %s\n”,
TTF_GetError());
return 1;
}

[/Code]


http://www.mattsscripts.co.uk/

  • A great source for free CGI and stuff

It is one of the superstitions of the human mind to have imagined that
virginity could be a virtue.
– Voltaire

is the ttf in the same directory you ran the test app from ?

I am Developing in MacOSX, which can be a bit annoying when it comes
to which directory the app is actually run from. I will check for the
files existence before trying to open it then. Is this going to get
fixed in a new version of SDL_ttf?On Apr 16, 2004, at 2:23 AM, Mike Frysinger wrote:


Coleman Nitroy
nitroy.com/cole

is the ttf in the same directory you ran the test app from ?

I tested for the files existence with PHYSFS and I am 100% sure that
the file exists, I even tried giving it an explicit path
(/home/me/project/file.ttf) and it still crashes and burns, could this
be a OSX issue? I do not have access to my linux box because I am in
the middle of a move right now. Could it also be a font issue, would it
crash if I try to open a font at a size it could not handle? I have
tried two different fonts, and will try more, but it seems futile.On Apr 16, 2004, at 2:23 AM, Mike Frysinger wrote:


Coleman Nitroy
nitroy.com/cole

when you say ‘crashes and burns’ i assume you mean like ‘segfault’ ?

what does gdb have to say about the whole thing ?
-mikeOn Friday 16 April 2004 08:22 am, Coleman Nitroy wrote:

On Apr 16, 2004, at 2:23 AM, Mike Frysinger wrote:

is the ttf in the same directory you ran the test app from ?

I tested for the files existence with PHYSFS and I am 100% sure that
the file exists, I even tried giving it an explicit path
(/home/me/project/file.ttf) and it still crashes and burns, could this
be a OSX issue? I do not have access to my linux box because I am in
the middle of a move right now. Could it also be a font issue, would it
crash if I try to open a font at a size it could not handle? I have
tried two different fonts, and will try more, but it seems futile.

when you say ‘crashes and burns’ i assume you mean like ‘segfault’ ?

Yes, segfault, i’ve given up the idea of using SDL_ttf and gone with
the much smaller and simpler SFont.On Apr 16, 2004, at 12:59 PM, Mike Frysinger wrote:


Coleman Nitroy
nitroy.com/cole

It’s been a while, but the last time I tried to use SDL_ttf on my OSX machine,
I had the same problem. That was with 10.2 (and probably >6 months ago), so
things may have changed, but basically it didn’t work.On Friday 16 April 2004 12:06 pm, Coleman Nitroy wrote:

On Apr 16, 2004, at 12:59 PM, Mike Frysinger wrote:

when you say ‘crashes and burns’ i assume you mean like ‘segfault’ ?

Yes, segfault, i’ve given up the idea of using SDL_ttf and gone with
the much smaller and simpler SFont.


Coleman Nitroy
nitroy.com/cole


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

It’s been a while, but the last time I tried to use SDL_ttf on my OSX machine,
I had the same problem. That was with 10.2 (and probably >6 months ago), so
things may have changed, but basically it didn’t work.

when you say ‘crashes and burns’ i assume you mean like ‘segfault’ ?

Yes, segfault, i’ve given up the idea of using SDL_ttf and gone with
the much smaller and simpler SFont.

I used SDL_ttf extensively on Redhat 7, 8, and 9 and now use it on
Debian. My students have used it on Mac OS 10+ and Windows 2k & XP. I’ve
never seen an SDL_ttf failure that wasn’t due to a coding error on the
part of the programmer. OTOH, the version of FreeType that ships with RH
has all the patented parts turned off (for obvious reasons) so you can
get some really bad looking text from fonts that depend on those
features.

SDL_ttf just works and it works well.

	Bob PendletonOn Mon, 2004-04-19 at 09:22, David Bishop wrote:

On Friday 16 April 2004 12:06 pm, Coleman Nitroy wrote:

On Apr 16, 2004, at 12:59 PM, Mike Frysinger wrote:


Coleman Nitroy
nitroy.com/cole


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


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

±--------------------------------------+

It’s been a while, but the last time I tried to use SDL_ttf on my OSX
machine, I had the same problem. That was with 10.2 (and probably >6
months ago), so things may have changed, but basically it didn’t work.

when you say ‘crashes and burns’ i assume you mean like ‘segfault’ ?

Yes, segfault, i’ve given up the idea of using SDL_ttf and gone with
the much smaller and simpler SFont.

I used SDL_ttf extensively on Redhat 7, 8, and 9 and now use it on
Debian. My students have used it on Mac OS 10+ and Windows 2k & XP. I’ve
never seen an SDL_ttf failure that wasn’t due to a coding error on the
part of the programmer. OTOH, the version of FreeType that ships with RH
has all the patented parts turned off (for obvious reasons) so you can
get some really bad looking text from fonts that depend on those
features.

SDL_ttf just works and it works well.

To be clear, SDL_ttf works perfectly on every linux machine I’ve used it. I
just was unable to get it working on OSX. Admittedly it may have been
(probably was?) my fault, due to misconfiguring something, like using the sdl
package vs. installing from source into /usr/[lib|include], or something.
But it definetly didn’t work. Maybe I’ll try again tonight, see if it’s
gotten better with 10.3 and the newest sdl.

D.A.BishopOn Monday 19 April 2004 9:18 am, Bob Pendleton wrote:

On Mon, 2004-04-19 at 09:22, David Bishop wrote:

On Friday 16 April 2004 12:06 pm, Coleman Nitroy wrote:

On Apr 16, 2004, at 12:59 PM, Mike Frysinger wrote:

I used SDL_ttf extensively on Redhat 7, 8, and 9 and now use it on
Debian. My students have used it on Mac OS 10+ and Windows 2k & XP.
I’ve
never seen an SDL_ttf failure that wasn’t due to a coding error on the
part of the programmer. OTOH, the version of FreeType that ships with
RH
has all the patented parts turned off (for obvious reasons) so you can
get some really bad looking text from fonts that depend on those
features.

Well, with 10.3 and the latest version of SDL_ttf, it does not work, my
code works perfect in linux (tested just recently) and I even tried a
example program, something is weird with SDL_ttf on osx, SDL_ttf does
have some bugs that relate to indirectly, like the problem of when an
error occurs loading a font it does not return null it just crashes
your program, which seems a little fishy to be honest.

SDL_ttf just works and it works well.

Not on OSX with the current version, something is b0rked or f0rked.On Apr 19, 2004, at 11:18 AM, Bob Pendleton wrote:


Coleman Nitroy
nitroy.com/cole

Ok, so currently I am having an issue using SDL_TTF. I am using
TTF_RenderText_Solid. I have the font loaded and the color set. The issue
I am having involves creating a char * that contains specific text and an
int.
Example:

char *temp;
int num = 30;

temp = "HP: " + num;
TTF_RenderText_Solid(Font Used, temp, Color Used);

No sure what to do. Getting errors
’ invalid operands of types const char*' andconst ’

-Jamie

Quoth Jamie Bernier , on 2005-05-02 23:15:40 -0400:

char *temp;
int num = 30;

temp = "HP: " + num;

This is not an SDL problem. C strings do not work like that.

In C, you can statically allocate a buffer and use snprintf:

char buffer[256];
snprintf(buffer, 256, “HP: %d”, num);
/* buffer now contains the string you want */

In C++, you can use an ostringstream:

std::ostringstream ss;
ss << "HP: " << num;
// now use ss.str().c_str() to get a C-style string from it

—> Drake wilson
-------------- 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/20050503/e1e12ea6/attachment.pgp

char *temp;
int num = 30;
temp = "HP: " + num;

    > No sure what to do. Getting errors
    > ' invalid operands of types `const char*' and `const '

Nothing to do with SDL_TTF. You can’t add a const char* ("HP: ") and an
int (num). You must convert num to a string first; or do something like
this : sprintf(sTemp, “HP: %d”, num);

Hey. Currently I have a working SDL environment set up in cygwin on Windows XP.
The version of SDL I am using is 1.2.11 (the only version I could find on the
SDL site). Anyways, today I wanted to install SDL_ttf, but when I ran the
configure script, near the end it gave me the error:

checking for SDL - version >= 1.2.4… no
*** Could not run SDL test program, checking why…
*** The test program failed to compile or link. See the file config.log for the
*** exact error that occured. This usually means SDL was incorrectly installed
*** or that you have moved SDL since it was installed. In the latter case, you
*** may want to edit the sdl-config script: /bin/sdl-config
configure: error: *** SDL version 1.2.4 not found!

Now, I do understand the error, but verson 1.2.11 seems to be the only one on
the SDL site. http://www.libsdl.org/download-1.2.php
Since 1.2.11 seems to be the main version, it should work with SDL_ttf.

Thanks.

Hello !

Hey. Currently I have a working SDL environment set up in cygwin on
Windows XP.
The version of SDL I am using is 1.2.11 (the only version I could find on
the SDL site). Anyways, today I wanted to install SDL_ttf, but when I ran
the configure script, near the end it gave me the error:

checking for SDL - version >= 1.2.4… no *** Could not run SDL test
program, checking why… *** The test program failed to compile or link.
See the file config.log for the
*** exact error that occured. This usually means SDL was incorrectly
installed *** or that you have moved SDL since it was installed. In the
latter case, you *** may want to edit the sdl-config script:
/bin/sdl-config
configure: error: *** SDL version 1.2.4 not found!

Please type in sdl-config and see if that works.
To get a better idea why the detection failed,
look at the created config.log and search
there for a failed test.

CU

:frowning: :frowning: :frowning:
can someone show me (or us) a piece of code that draws sometihng and
renders some text on that screen?
i need just a basic example,i couldnt manage to use SDL_ttf.–
Just say the words , your wish is my command…

Hi!

Look this page:

http://lazyfoo.net/SDL_tutorials/lesson07/index.php

Hello,
I have a weird issue. I am trying to implement SDL_ttf on my Linux Fedora Core 8 system. When I try draw a string I get an error message “Text has zero width”. As far as I can tell I am setting things up properly but when I make the call to do the rendering it returns with this message. Any input would be a great help.

Here is the function I am using…

int DrawString(char* font_name, const char * text, int x, int y, int quality, Uint32 ft_color, Uint32 bk_color, int size, Uint32 attributes, int buffer){
TTF_Font * temp;
SDL_Surface * text_surface;
SDL_Color color, back_color;
SDL_Rect dest, srcr;
SDL_PixelFormat * fmt;
Uint32 color_key;

temp = TTF_OpenFont((char*) font_name, (int) size);
if( temp!=0 ){
printf(“Could not load the font: %s\n”, font_name);
return -1;
} else {
//Set the color.
//color
color.r = 0xFF;
color.g = 0x33;
color.b = 0x19;
//back_color
back_color.r = 0xFF;
back_color.g = 0x33;
back_color.b = 0x19;
//Set the attibutes
//TTF_SetFontStyle(temp, attributes);
//Render the font on a temp SDL_surface
switch (quality){
case 0: //Normal Rendering
text_surface = TTF_RenderText_Solid(temp, text, color);
break;
case 1://Shaded Rendering. This will create a ‘box’ around the text
text_surface = TTF_RenderText_Shaded(temp, text, color, back_color);
break;
case 2://Blended Rendering. This is the highest quality, but is the slowest.
text_surface = TTF_RenderText_Blended(temp, text, color);
break;
default: //Normal Rendering
text_surface = TTF_RenderText_Solid(temp, text, color);
break;
}
if(!text_surface)printf(“The rendering did not work ERROR : %s\n”, TTF_GetError());
//Now draw the string.
color_key = SDL_MapRGB(text_surface->format, 255, 0, 255);
SDL_SetColorKey(text_surface, SDL_SRCCOLORKEY | SDL_RLEACCEL ,color_key);
//Draw the image on the given buffer.
dest.w=text_surface->w;
srcr.w=text_surface->w;
dest.h=text_surface->h;
srcr.h=text_surface->h;
dest.x=x; dest.y=y;
if((dest.w+dest.x <= SCREENWIDTH)){
SDL_BlitSurface((SDL_Surface*)text_surface, &srcr,(SDL_Surface*) surface_buffer[buffer], &dest);
} else {
SDL_BlitSurface((SDL_Surface*)text_surface, NULL,(SDL_Surface*) surface_buffer[buffer], &dest);
}
if(buffer==VO_BUFFER) {
SDL_UpdateRect((SDL_Surface*)surface_buffer[buffer], dest.x, dest.y, dest.w, dest.h);
}
//free up the temp memory
SDL_FreeSurface(text_surface); //Free up unused memory
TTF_CloseFont(temp);
return 0;
}
return 0;
}

Thanks,
Walt.

Oops. I forgot to run the TTF_Init(). Issue solved.

Thanks,
Walt.________________________________________
From: sdl-bounces@lists.libsdl.org [sdl-bounces at lists.libsdl.org] On Behalf Of Walt Crowley [WaltC at fleetwoodgaming.com]
Sent: Thursday, August 07, 2008 9:38 AM
To: sdl at lists.libsdl.org
Subject: [SDL] SDL_ttf

Hello,
I have a weird issue. I am trying to implement SDL_ttf on my Linux Fedora Core 8 system. When I try draw a string I get an error message “Text has zero width”. As far as I can tell I am setting things up properly but when I make the call to do the rendering it returns with this message. Any input would be a great help.

Here is the function I am using…

int DrawString(char* font_name, const char * text, int x, int y, int quality, Uint32 ft_color, Uint32 bk_color, int size, Uint32 attributes, int buffer){
TTF_Font * temp;
SDL_Surface * text_surface;
SDL_Color color, back_color;
SDL_Rect dest, srcr;
SDL_PixelFormat * fmt;
Uint32 color_key;

temp = TTF_OpenFont((char*) font_name, (int) size);
if( temp!=0 ){
printf(“Could not load the font: %s\n”, font_name);
return -1;
} else {
//Set the color.
//color
color.r = 0xFF;
color.g = 0x33;
color.b = 0x19;
//back_color
back_color.r = 0xFF;
back_color.g = 0x33;
back_color.b = 0x19;
//Set the attibutes
//TTF_SetFontStyle(temp, attributes);
//Render the font on a temp SDL_surface
switch (quality){
case 0: //Normal Rendering
text_surface = TTF_RenderText_Solid(temp, text, color);
break;
case 1://Shaded Rendering. This will create a ‘box’ around the text
text_surface = TTF_RenderText_Shaded(temp, text, color, back_color);
break;
case 2://Blended Rendering. This is the highest quality, but is the slowest.
text_surface = TTF_RenderText_Blended(temp, text, color);
break;
default: //Normal Rendering
text_surface = TTF_RenderText_Solid(temp, text, color);
break;
}
if(!text_surface)printf(“The rendering did not work ERROR : %s\n”, TTF_GetError());
//Now draw the string.
color_key = SDL_MapRGB(text_surface->format, 255, 0, 255);
SDL_SetColorKey(text_surface, SDL_SRCCOLORKEY | SDL_RLEACCEL ,color_key);
//Draw the image on the given buffer.
dest.w=text_surface->w;
srcr.w=text_surface->w;
dest.h=text_surface->h;
srcr.h=text_surface->h;
dest.x=x; dest.y=y;
if((dest.w+dest.x <= SCREENWIDTH)){
SDL_BlitSurface((SDL_Surface*)text_surface, &srcr,(SDL_Surface*) surface_buffer[buffer], &dest);
} else {
SDL_BlitSurface((SDL_Surface*)text_surface, NULL,(SDL_Surface*) surface_buffer[buffer], &dest);
}
if(buffer==VO_BUFFER) {
SDL_UpdateRect((SDL_Surface*)surface_buffer[buffer], dest.x, dest.y, dest.w, dest.h);
}
//free up the temp memory
SDL_FreeSurface(text_surface); //Free up unused memory
TTF_CloseFont(temp);
return 0;
}
return 0;
}

Thanks,
Walt.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi List,

its quiet an expert list and I feel a little clumbsy to post here…
Anyway :slight_smile:

I`ve got Vsiual C express working.
It compiles SDL programms and it can make use of the SDL_mixer(the sound mixer).

I think I have included the SDL_ttf files at the right location because it seems pretty much
the same procedure than SDL_mixer.

The includes and the SDL_ttf initialisation works.
But when I start with
text = TTF_RenderText_Solid( font, “Space…”, textcolor );

The program compiles, links, starts but

I got an exeption and a untreated access

Unbehandelte Ausnahme bei 0x6f4c27f4 in TTF_test.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x00000000.

(sorry its German)

Here is a minimum program to document my trouble…
It has not even the text blitted and it stops where the trouble starts :)-----------------------------
#include “stdafx.h”
#include “TTF_test.h”
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include "SDL_ttf.h"
int main(int argc, char *argv[])

{
SDL_Surface *screen;
SDL_Surface *text = NULL;
TTF_Font *font;
SDL_Color textcolor = { 128, 88, 77 };

//font=TTF_OpenFont(“font.ttf”, 16);
font=TTF_OpenFont(“C:/winnt/Fonts/Verdana.ttf”,18); // both works

screen = SDL_SetVideoMode(640, 480, 24,SDL_SWSURFACE);

TTF_Init();

//When I uncomment this line, the error happens…so far it works
//text = TTF_RenderText_Solid( font, “Space…”, textcolor );

SDL_Delay(500);

TTF_CloseFont(font);
TTF_Quit();
}


It would be very nice if somebody could have a look on it because I like to do something on alternate OS

and SDL seems to be a very nice API.

Thanks you very much,

Lars

Things that come to mind:

  • add SDL_Init
  • check font for NULL

The “official” sample code you can use as reference can be found here:
http://hg.libsdl.org/SDL_ttf/file/c1f5f19d8fa9/showfont.c

–AndreasOn 11/26/10 9:54 AM, Lars Br?mer wrote:

Hi List,
its quiet an expert list and I feel a little clumbsy to post here…
Anyway :slight_smile:
I`ve got Vsiual C express working.
It compiles SDL programms and it can make use of the SDL_mixer(the
sound mixer).
I think I have included the SDL_ttf files at the right location
because it seems pretty much
the same procedure than SDL_mixer.
The includes and the SDL_ttf initialisation works.
But when I start with

text = TTF_RenderText_Solid( font, “Space…”, textcolor );

The program compiles, links, starts but

I got an exeption and a untreated access

Unbehandelte Ausnahme bei 0x6f4c27f4 in TTF_test.exe: 0xC0000005:
Zugriffsverletzung beim Lesen an Position 0x00000000.

(sorry its German)

Here is a minimum program to document my trouble…
It has not even the text blitted and it stops where the trouble starts :slight_smile:

#include “stdafx.h”
#include “TTF_test.h”
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include “SDL_ttf.h”

int main(int argc, char *argv[])

{
SDL_Surface *screen;
SDL_Surface *text = NULL;
TTF_Font *font;
SDL_Color textcolor = { 128, 88, 77 };

//font=TTF_OpenFont(“font.ttf”, 16);
font=TTF_OpenFont(“C:/winnt/Fonts/Verdana.ttf”,18); // both works

screen = SDL_SetVideoMode(640, 480, 24,SDL_SWSURFACE);

TTF_Init();

//When I uncomment this line, the error happens…so far it works
//text = TTF_RenderText_Solid( font, “Space…”, textcolor );

SDL_Delay(500);

TTF_CloseFont(font);
TTF_Quit();
}


It would be very nice if somebody could have a look on it because I
like to do something on alternate OS

and SDL seems to be a very nice API.

Thanks you very much,

Lars


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org