Best method for altering surface colors?

I hope this doesn’t sound too much of an obvious/pathetic question,

I’ve got a surface that has a whole load of characters in it, for putting
text onto the screen.

Depending on the message, I want the colors to be different. But asides from
change the palette of the entire surface to the desired color, is there a
better way?

So you get an idea, right now I’m doing,

void setFontColor(int red, int green, int blue)
{
SDL_Color colors[16];
for (int i = 0 ; i < 16 ; i++)
{
colors[i].r = red;
colors[i].g = green;
colors[i].b = blue;
}

SDL_SetPalette(globals.font, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 16);

}

which works, but is probably not the best way of doing things.

Any suggestions?

Cheers.

I hope this doesn’t sound too much of an obvious/pathetic question,

I’ve got a surface that has a whole load of characters in it, for
putting text onto the screen.

Depending on the message, I want the colors to be different. But asides
from change the palette of the entire surface to the desired color, is
there a better way?

I happen to be hacking on just that kind of thing right now, for the
"conemu" library.

So you get an idea, right now I’m doing,

void setFontColor(int red, int green, int blue)
{
SDL_Color colors[16];
for (int i = 0 ; i < 16 ; i++)
{
colors[i].r = red;
colors[i].g = green;
colors[i].b = blue;
}

SDL_SetPalette(globals.font, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 16);
}

which works, but is probably not the best way of doing things.

Any suggestions?

Well, in a quick hack SDL program (not “conemu”), I just forced SDL to
use 24 bpp RGB (which results in s/w emulation, if such a mode cannot be
set). Then I used an 8 bpp grayscale font, stored as 24 bit RGB. Finally,
I hacked a custom s/w blitter that combined each pixel of the font with
(IIRC) two masks using logic operations, before writing to the screen.

This gave antialiased printing with individually selectable background
and foreground colors from an impressive palette of the 8 fundamental RGB
colors! :wink:

It’s possible to implement more sophisticated things, of course, but
short of creating and colorizing new fonts as needed, I can’t really see
how you could do it without hacking your own blitter.

//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 08 March 2002 13:48, Sweeney, Steven (FNB) wrote:

I hope this doesn’t sound too much of an obvious/pathetic question,

I’ve got a surface that has a whole load of characters in it, for putting
text onto the screen.

Depending on the message, I want the colors to be different. But asides
from
change the palette of the entire surface to the desired color, is there a
better way?

This really depends on what kind of font you are using, and what
characteristics it requires. Generally I think it’s almost always best to
handle drawing the characters yourself rather than using a blit (if that’s
what you are doing now).

For me, I nearly always use an 8x16 fixed width bitmap font, and don’t
bother with alpha blending or anti-aliasing or any of that stuff, though I
do use color key type of stuff. What I use specialized code I wrote myself
to just draw all the pixels myself for a character. The core of it looks
like this:

pitch = screen->pitch;
ptr8 = (Uint8 *) screen->pixels + ypos * pitch + xpos;
chr *= w * h;

for (py=0; py<h; py++) {
    for (px=0; px<w; px++) {
        p = font_data[chr++];
        if (p)
            *ptr8 = color;

        ptr8++;
    }

    ptr8 += pitch - w;
}

Shound give you a general idea. I don’t actually store my font data in a
surface, but just in a block of memory at font_data.

-Jason