Tinting image->pixels

Hi,

I’ve been playing with sdl for the past few days, and being new to games/graphics
programming, in particular working with image data, have come up against a
couple of things where i’m not sure how to handle them.

What i’m doing is loading an image & using that as a sprite for display. A large amount
of the objects I intend to display will look the same, except they will use different
colours. So I had thought that it would be a good idea to save on disk space and create
each object from a base image by creating each differently coloured object by extracting
each pixel and modifying the colour component, putting it into a new array of pixels and
calling SDL_CreateRGBSurfaceFrom(…) on the new pixel data.

If anyone could offer some pointers, or thoughts, or even some fundamentals that I
may be over looking, it would be much appreciated.

Gef :]–
“Taking risks and failing is an important part of the
creative process. Denying one’s self of this experience
is to enter the realm of the mediocre.”
– Lum The Mad/The Qoutable R, The Scratchware Manifesto

For example, I use this :
I’m taking a grey sprite and coloring it with r,g,b with mix with background

don’t forget to use the pitch value when accessing the buffer
also, check Lock and Unlock to know if SDL_Surfaces need to be locked

Murlock

// r,g,b color
// screen: dest
// sprite: sprite in gray colour
int PrintSpriteColor(int r, int g, int b, int x, int y, SDL_Surface *screen,
SDL_Surface *sprite )
{
int ox,i,x1;
SDL_Surface *copy=screen;

PLetter=(unsigned char )sprite->pixels;
int skip = sprite->pitch-(sprite->w
sprite->format->BytesPerPixel);
for(int iy=0; iyh;iy++)
{
for(int ix=0; ixw;ix++)
{
s=((double)*PLetter)/255.0;
d=1.0-s;
switch(copy->format->BitsPerPixel)
{
case 8:
PDest=(unsigned char )copy->pixels+((y+iy)copy->pitch+(ix+x1));
SDL_GetRGB(
((Uint8
)PDest), copy->format, &rs, &gs, &bs );
break;
case 32:
PDest=(unsigned char *)copy->pixels+((y+iy)copy->pitch+(ix+x1)4);
SDL_GetRGB(
((Uint32
)PDest), copy->format, &rs, &gs, &bs );
break;
default:
Log(“Error: PrintColor can’t works in others bpp that 8 and 32”);
exit(-1);
}

 m_Value = SDL_MapRGB(copy->format, (Uint8)( s*r + d*rs),

(Uint8)(sg+dgs), (Uint8)(sb+dbs) );
switch(copy->format->BitsPerPixel)
{
case 8:
((Uint8)PDest)=(Uint8)m_Value;
break;
case 16:
((Uint16)PDest)=(Uint16)m_Value;
break;
case 24:
Log(“Error: PrintColor can’t works in others bpp that 8 and 32”);
exit(-1);
break;
case 32:
((Uint32)PDest)=(Uint32)m_Value;
break;
}
PLetter++;
}
PLetter+=skip;
}
}> ----- Original Message -----

From: gefdavis@dingoblue.net.au (Geoff Davis)
To: “SDL-List”
Sent: Tuesday, September 18, 2001 3:33 AM
Subject: [SDL] Tinting image->pixels

Hi,

I’ve been playing with sdl for the past few days, and being new to
games/graphics
programming, in particular working with image data, have come up against a
couple of things where i’m not sure how to handle them.

What i’m doing is loading an image & using that as a sprite for display. A
large amount
of the objects I intend to display will look the same, except they will
use different
colours. So I had thought that it would be a good idea to save on disk
space and create
each object from a base image by creating each differently coloured object
by extracting
each pixel and modifying the colour component, putting it into a new array
of pixels and
calling SDL_CreateRGBSurfaceFrom(…) on the new pixel data.

If anyone could offer some pointers, or thoughts, or even some
fundamentals that I
may be over looking, it would be much appreciated.

Gef :]


“Taking risks and failing is an important part of the
creative process. Denying one’s self of this experience
is to enter the realm of the mediocre.”
– Lum The Mad/The Qoutable R, The Scratchware Manifesto


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

Hi,

I didn’t follow this thread from the begging, but I just examined the code below and wanted to show you my opinion…:
I first saw this in FreeCraft project, it would be fastest to you use pointers to functions, so you would have:
PrintSpriteColor8(…)
PrintSpriteColor16(…)
PrintSpriteColor24(…)
PrintSpriteColor32(…)
(int *PrintSpriteColor)(…);

and on some InitVideo() or something like that… :
switch (Screen->format->BitsPerPixel) {
case 8:
PrintSpriteColor = PrintSpriteColor8;
break;
case 16:
PrintSpriteColor = PrintSpriteColor16;
break;
case 24:
PrintSpriteColor = PrintSpriteColor24;
break;
case 32:
PrintSpriteColor = PrintSpriteColor32;
break;
default:
fprintf(stderr, “Unsupported video mode: %d\n”, Screen->format->BitsPerPixel);
exit(-1);
}

and in another game place, you would call (PrintSpriteColor)();
so you would have only one check and the rest would go in “don’t stop mode”.

not sure 'bout the C syntax, it’s been a long while that I don’t use this kind of thing, but is a good start to you… :slight_smile:

[]sOn Tue, Sep 18, 2001 at 09:33:57AM +0100, Murlock wrote:

For example, I use this :
I’m taking a grey sprite and coloring it with r,g,b with mix with background

don’t forget to use the pitch value when accessing the buffer
also, check Lock and Unlock to know if SDL_Surfaces need to be locked

Murlock

// r,g,b color
// screen: dest
// sprite: sprite in gray colour
int PrintSpriteColor(int r, int g, int b, int x, int y, SDL_Surface *screen,
SDL_Surface *sprite )
{
int ox,i,x1;
SDL_Surface *copy=screen;

PLetter=(unsigned char )sprite->pixels;
int skip = sprite->pitch-(sprite->w
sprite->format->BytesPerPixel);
for(int iy=0; iyh;iy++)
{
for(int ix=0; ixw;ix++)
{
s=((double)*PLetter)/255.0;
d=1.0-s;
switch(copy->format->BitsPerPixel)
{
case 8:
PDest=(unsigned char )copy->pixels+((y+iy)copy->pitch+(ix+x1));
SDL_GetRGB(
((Uint8
)PDest), copy->format, &rs, &gs, &bs );
break;
case 32:
PDest=(unsigned char *)copy->pixels+((y+iy)copy->pitch+(ix+x1)4);
SDL_GetRGB(
((Uint32
)PDest), copy->format, &rs, &gs, &bs );
break;
default:
Log(“Error: PrintColor can’t works in others bpp that 8 and 32”);
exit(-1);
}

 m_Value = SDL_MapRGB(copy->format, (Uint8)( s*r + d*rs),

(Uint8)(sg+dgs), (Uint8)(sb+dbs) );
switch(copy->format->BitsPerPixel)
{
case 8:
((Uint8)PDest)=(Uint8)m_Value;
break;
case 16:
((Uint16)PDest)=(Uint16)m_Value;
break;
case 24:
Log(“Error: PrintColor can’t works in others bpp that 8 and 32”);
exit(-1);
break;
case 32:
((Uint32)PDest)=(Uint32)m_Value;
break;
}
PLetter++;
}
PLetter+=skip;
}
}
—end quoted text—


Marcelo R Leitner
ICQ #: 29966851
Kill’em All … And Justice For All.

Just fixing the syntax from my old mail, the right C syntax for function pointers is:

declaration:
return_value (*func_name)(params);
so,
int (*func_name)(void);
int (*func_name)(unsigned int x);

to call the function:
(*func_name)(params); or
func_name(params);

so we have
int (*PrintSpriteColor)(…);

to initialize:
PrintSpriteColor = PrintSpriteColor8;
PrintSpriteColor = PrintSpriteColor16;
PrintSpriteColor = PrintSpriteColor24;
PrintSpriteColor = PrintSpriteColor32;
PrintSpriteColor = PrintSpriteColorWhatEverYouWantSupport;

to use:
PrintSpriteColor(params);

[]'s–
Marcelo R Leitner
ICQ #: 29966851
Kill’em All … And Justice For All.