SDL_SetColors (segfault)

Hello,

Trying my hand at a bit of coding with the SDL libraries. I am relatively
new to C programming (just finished up a class), although I have been
programming in perl for several years.

I am starting with a very simple program to print some text (using
SDL_ttf) in a window. The program compiles ok, (compile parameters
included below), however when the call to SDL_SetColors is made, the
program segaults. Attached is the program (example.c) and below is
the output on the console.

  • Compile:

$ gcc example.c sdl-config --libs --cflags -lSDL_ttf
$ ./a.out
All things initialized correctly
about to call SDL_SetColors
Fatal signal: Segmentation Fault (SDL Parachute Deployed)
$

  • sdl-config output (to get an idea of what flags are being used aside
    from -lSDL_ttf):

$ sdl-config --libs --cflags
-L/usr/lib -lSDL -lpthread -L/usr/X11R6/lib -lXxf86dga -lXxf86vm -lXv
-I/usr/include/SDL -D_REENTRANT
$

All help is greatly appreciated.

See below for relevant source code example.–
Nick Jennings

-------------- next part --------------
A non-text attachment was scrubbed…
Name: example.c
Type: text/x-csrc
Size: 1978 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020104/4d7bb842/attachment.c

If I understand correctly, FreeType (and therefore SDL_ttf) can anti-alias
text, but only onto a solid color background. What is the solution to
anti-alias text onto a non-solid background (e.g. an image)?

If it can’t be done precisely, what is the next best way? I’m thinking:
determine the “average” color onto which the text will be written and use
that as the background color (e.g. in the call to TTF_RenderText_Shaded.)
That’s the only thing that comes to mind, but I don’t know how good that
solution is.

Thanks,

Walter Rader

See below for relevant source code example.
screen = SDL_SetVideoMode(800, 600, 16, SDL_DOUBLEBUF);
if ((screen = NULL)) {
printf(“Unable to set video mode: %s\n”, SDL_GetError());
exit(3);
}

This is why you crash, = instead of == :slight_smile:

Bye,
Gabry (gabrielegreco at tin.it)

shouldn’t TTF_RenderText_Blended do the trick? this renders the text with an
alpha mask ( if I remember right ).

/Mange—
“Let the programmers be many and the managers few – then all will be
productive.”

  • The Tao of Programming

----- Original Message -----
From: wrader@ocf.berkeley.edu (Walter B. Rader)
To:
Sent: Friday, January 04, 2002 10:56
Subject: [SDL] SDL_ttf: anti-aliasing text on non-solid background?

If I understand correctly, FreeType (and therefore SDL_ttf) can anti-alias
text, but only onto a solid color background. What is the solution to
anti-alias text onto a non-solid background (e.g. an image)?

If it can’t be done precisely, what is the next best way? I’m thinking:
determine the “average” color onto which the text will be written and use
that as the background color (e.g. in the call to TTF_RenderText_Shaded.)
That’s the only thing that comes to mind, but I don’t know how good that
solution is.

Thanks,

Walter Rader


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

Nick Jennings wrote:

screen = SDL_SetVideoMode(800, 600, 16, SDL_DOUBLEBUF);
[…]
SDL_SetColors(screen, colors, 0, 5);

SetColors is for indexed surfaces. You are calling it for a 16-bit surface
which has no palette. It shouldn’t crash though, so something else may be
wrong as well

for ( i = 0; i < COLORS; ++i ) {
colors[i].r = white.r + (irdiff)/4;
colors[i].r = white.g + (i
gdiff)/4;
colors[i].r = white.b + (i*bdiff)/4;
}

colors[i].r 3 times? copy/paste is bad :wink: this could be the problem because
SDL_SetColors could try to access the g and b values but there arent!

for ( i = 0; i < COLORS; ++i ) {
colors[i].r = white.r + (irdiff)/4;
colors[i].g = white.g + (i
gdiff)/4;
colors[i].b = white.b + (i*bdiff)/4;
}

could go!

Gabriele Greco wrote:

    if ((screen = NULL)) {

[…]

This is why you crash, = instead of == :slight_smile:

Good spotting. The interesting thing is the extra pair of parentheses,
perhaps added to “please” his compiler that warned about it :slight_smile:

You hit the nail on the head :slight_smile: thats what I get for coding until 5am.

I did think that compiler message was rather odd, but was anxious to
see my text printed and just wanted to compile :slight_smile:

Now , (thanks to Gabriele), I am able to run the program without
a segfault. However I see no text in the window. Can anyone help me
to determine why? I blindly copied this example from an example program
that printed text black on white, and the only modification to this I
made was to “reverse” the text to white on black. Is there an error in
the code while doing this? (I still do not quite understand the color
settings etc.) Or perhaps is it something I should be doing after
TTF_RenderTextSolid() ?On Fri, Jan 04, 2002 at 12:00:08PM +0100, Mattias Engdeg?rd wrote:

Gabriele Greco wrote:

    if ((screen = NULL)) {

[…]

This is why you crash, = instead of == :slight_smile:

Good spotting. The interesting thing is the extra pair of parentheses,
perhaps added to “please” his compiler that warned about it :slight_smile:


Nick Jennings

Now , (thanks to Gabriele), I am able to run the program without
a segfault. However I see no text in the window. Can anyone help me
to determine why? I blindly copied this example from an example program
that printed text black on white, and the only modification to this I
made was to “reverse” the text to white on black. Is there an error in
the code while doing this? (I still do not quite understand the color
settings etc.) Or perhaps is it something I should be doing after
TTF_RenderTextSolid() ?

Well, TTF_RenderTextSolid() returns a surface with the text rendered on it
(I’m guessing this, just looking at the function prototype)

The relevant part of your code is:

sprintf(string, “Test successful\n”);
TTF_RenderText_Solid(font, string, white);

sleep(5);

exit(0);

This call to TTF_RenderText_Solid() therefore creates a new surface, renders
the string on it in white, and then does nothing with it. What you need to
do is to blit the surface it creates onto the screen. font_surface should be
declared as “SDL_Surface *font_surface;”

font_surface = TTF_RenderText_Solid (font, string, white);

Then to center it on the screen, use an SDL_Rect; declared as “SDL_Rect dest;”

dest.x = (screen->w / 2) - (font_surface->w / 2);
dest.y = (screen->h / 2) - (font_surface->h / 2);
dest.w = font_surface->w;
dest.h = font_surface->h;

SDL_BlitSurface (font_surface, NULL, screen, &dest);

The first NULL is the SDL_BlitSurface() call is the source rectangle; you
can use this to copy a portion of a surface to another surface. Specifying
NULL tells SDL to just blit the entire thing.

Then you need up make sure the screen has been updated:

SDL_UpdateRects (screen, 1, &dest);

And then you should hopefully see something on the screen. Note that you
really should check the return value of TTF_RenderText_Solid(), as it
returns NULL on error, but it’s late and I can’t be bothered. :-)On Fri, 4 Jan 2002, Nick Jennings wrote:

Mike.

Regarding my question about anti-aliasing text onto a non-solid
background…On Fri, 4 Jan 2002, Mange wrote:

shouldn’t TTF_RenderText_Blended do the trick? this renders the text with an
alpha mask ( if I remember right ).

/Mange

Ah yes. You are correct. Unfortunately, when I try to blit the surface
created by TTF_RenderText_Blended onto my main display window, I don’t get
text, but rather solid rectangles where letter should be. I convert the
surface returned by TTF_RenderText_Blended to the display surface, mess
around with the alpha values, but to no avail: the text always comes out
as blocks.

As I’ve come to understand with my programming experience, I’m probably
doing something wrong. =) Any suggestions?

Thanks!!

Walter Rader

Yeah… Warnings are there for a reason - if you don’t understand them,
read the docs, instead of ignoring them. :wink:

Anyway, writing if(NULL == screen) make this mistake impossible to make.
There’s no way to compile if(NULL = screen)… :slight_smile:

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Friday 04 January 2002 12:00, Mattias Engdeg?rd wrote:

Gabriele Greco wrote:

    if ((screen = NULL)) {

[…]

This is why you crash, = instead of == :slight_smile:

Good spotting. The interesting thing is the extra pair of parentheses,
perhaps added to “please” his compiler that warned about it :slight_smile: