How do I draw a single pixel with alpha value to screen?

How do I draw a single pixel with alpha value to screen?

Hi,

I work now on this game:
PacDude 100% VeR5.0
(4-way scrolling Pac-Man styled game)
http://www.silentheroproductions.com/PacDude100PercenT.htm

I wish to have a little map so that the player can see the uneaten pellets.
How would I draw a single pixel with alpha value to the the screen using
SDL?

Thanks…

Jesse “JeZ+Lee” Palser, BS, A+, Network+, MCP
SLNTHERO at aol.com
Silent Hero Productions®
Video Game Design Studio
http://www.SilentHeroProductions.com

Hello !

How do I draw a single pixel with alpha value to screen?

Here you find all the infos you need :

http://www.libsdl.org/cgi/docwiki.cgi/Pixel_Access

CU

Unfortunately, the function under that link doesn’t do alpha blending. If you want to do that, that function is a good place to start, but you’ll have to write some code to take care of the alpha values.>----- Original Message ----

From: Torsten Giebl
Subject: Re: [SDL] How do I draw a single pixel with alpha value to screen?

Hello !

How do I draw a single pixel with alpha value to screen?

Here you find all the infos you need :

http://www.libsdl.org/cgi/docwiki.cgi/Pixel_Access

Here’s the code from Sprig’s internal call. The switch entry for
SPG_DEST_ALPHA is probably the functionality you want with 32-bit
surfaces. That’s the same effect as from SDL_BlitSurface().

Jonny D

void _PutPixelAlpha(SDL_Surface *surface, Sint16 x, Sint16 y, Uint32 color, Uint8 alpha)
{

if(x>=SPG_clip_xmin(surface) && x<=SPG_clip_xmax(surface)
&& y>=SPG_clip_ymin(surface) &&
y<=SPG_clip_ymax(surface)){
Uint32 Rmask =
surface->format->Rmask, Gmask = surface->format->Gmask,
Bmask = surface->format->Bmask, Amask =
surface->format->Amask;
Uint32 R,G,B,A=SDL_ALPHA_OPAQUE;
Uint32* pixel;
switch (surface->format->BytesPerPixel) {
case 1: { /* Assuming 8-bpp */

                Uint8 *pixel = (Uint8 *)surface->pixels + y*surface->pitch + x;
                
                Uint8 dR = surface->format->palette->colors[*pixel].r;
                Uint8 dG = surface->format->palette->colors[*pixel].g;
                Uint8 dB = surface->format->palette->colors[*pixel].b;
                Uint8 sR = surface->format->palette->colors[color].r;
                Uint8 sG = surface->format->palette->colors[color].g;
                Uint8 sB = surface->format->palette->colors[color].b;
                
                dR = dR + ((sR-dR)*alpha >> 8);
                dG = dG + ((sG-dG)*alpha >> 8);
                dB = dB + ((sB-dB)*alpha >> 8);
            
                *pixel = SDL_MapRGB(surface->format, dR, dG, dB);
                
        }
        break;

        case 2: { /* Probably 15-bpp or 16-bpp */        
            
                Uint16 *pixel = (Uint16 *)surface->pixels + y*surface->pitch/2 + x;
                Uint32 dc = *pixel;
            
                R = ((dc & Rmask) + (( (color & Rmask) - (dc & Rmask) ) * alpha >> 8)) & Rmask;
                G = ((dc & Gmask) + (( (color & Gmask) - (dc & Gmask) ) * alpha >> 8)) & Gmask;
                B = ((dc & Bmask) + (( (color & Bmask) - (dc & Bmask) ) * alpha >> 8)) & Bmask;
                if( Amask )
                    A = ((dc & Amask) + (( (color & Amask) - (dc & Amask) ) * alpha >> 8)) & Amask;

                *pixel= R | G | B | A;
                
        }
        break;

        case 3: { /* Slow 24-bpp mode, usually not used */
            Uint8 *pix = (Uint8 *)surface->pixels + y * surface->pitch + x*3;
            Uint8 rshift8=surface->format->Rshift/8;
            Uint8 gshift8=surface->format->Gshift/8;
            Uint8 bshift8=surface->format->Bshift/8;
            Uint8 ashift8=surface->format->Ashift/8;
            
            
            
                Uint8 dR, dG, dB, dA=0;
                Uint8 sR, sG, sB, sA=0;
                
                pix = (Uint8 *)surface->pixels + y * surface->pitch + x*3;
                
                dR = *((pix)+rshift8); 
                dG = *((pix)+gshift8);
                dB = *((pix)+bshift8);
                dA = *((pix)+ashift8);
                
                sR = (color>>surface->format->Rshift)&0xff;
                sG = (color>>surface->format->Gshift)&0xff;
                sB = (color>>surface->format->Bshift)&0xff;
                sA = (color>>surface->format->Ashift)&0xff;
                
                dR = dR + ((sR-dR)*alpha >> 8);
                dG = dG + ((sG-dG)*alpha >> 8);
                dB = dB + ((sB-dB)*alpha >> 8);
                dA = dA + ((sA-dA)*alpha >> 8);

                *((pix)+rshift8) = dR; 
                *((pix)+gshift8) = dG;
                *((pix)+bshift8) = dB;
                *((pix)+ashift8) = dA;
                
        }
        break;

        case 4: /* Probably 32-bpp */
            pixel = (Uint32*)surface->pixels + y*surface->pitch/4 + x;
            Uint32 dc = *pixel;
            R = color & Rmask;
            G = color & Gmask;
            B = color & Bmask;
            A = 0;  // keep this as 0 to avoid corruption of non-alpha surfaces
            
            
            switch(SPG_GetBlend())
            {
                case SPG_COMBINE_ALPHA:  // Blend and combine src and dest alpha
                    if( alpha != SDL_ALPHA_OPAQUE ){
                        R = ((dc & Rmask) + (( R - (dc & Rmask) ) * alpha >> 8)) & Rmask;
                        G = ((dc & Gmask) + (( G - (dc & Gmask) ) * alpha >> 8)) & Gmask;
                        B = ((dc & Bmask) + (( B - (dc & Bmask) ) * alpha >> 8)) & Bmask;
                    }
                    if(Amask)

A = ((((dc & Amask) >> surface->format->Ashift) +
alpha) >> 1) << surface->format->Ashift;
break;
case SPG_DEST_ALPHA: // Blend and keep dest alpha
if( alpha != SDL_ALPHA_OPAQUE ){
R = ((dc & Rmask) + (( R - (dc & Rmask) ) * alpha >> 8)) & Rmask;
G = ((dc & Gmask) + (( G - (dc & Gmask) ) * alpha >> 8)) & Gmask;
B = ((dc & Bmask) + (( B - (dc & Bmask) ) * alpha >> 8)) & Bmask;
}
if(Amask)
A = (dc & Amask);
break;
case SPG_SRC_ALPHA: // Blend and keep src alpha
if( alpha != SDL_ALPHA_OPAQUE ){
R = ((dc & Rmask) + (( R - (dc & Rmask) ) * alpha >> 8)) & Rmask;
G = ((dc & Gmask) + (( G - (dc & Gmask) ) * alpha >> 8)) & Gmask;
B = ((dc & Bmask) + (( B - (dc & Bmask) ) * alpha >> 8)) & Bmask;
}
if(Amask)
A = (alpha << surface->format->Ashift);
break;
case SPG_COPY_SRC_ALPHA: // Direct copy with src alpha
if(Amask)
A = (alpha << surface->format->Ashift);
break;
case SPG_COPY_DEST_ALPHA: // Direct copy with dest alpha
if(Amask)
A = (dc & Amask);
break;
case SPG_COPY_COMBINE_ALPHA: // Direct copy with combined alpha
if(Amask)

A = ((((dc & Amask) >> surface->format->Ashift) +
alpha) >> 1) << surface->format->Ashift;
break;
case SPG_COPY_NO_ALPHA: // Direct copy, alpha opaque
if(Amask)
A = (SDL_ALPHA_OPAQUE << surface->format->Ashift);
break;
case SPG_COPY_ALPHA_ONLY: // Direct copy of just the alpha
R = dc & Rmask;
G = dc & Gmask;
B = dc & Bmask;
if(Amask)
A = (alpha << surface->format->Ashift);
break;
case SPG_COMBINE_ALPHA_ONLY: // Blend of just the alpha
R = dc & Rmask;
G = dc & Gmask;
B = dc & Bmask;
if(Amask)

A = ((((dc & Amask) >> surface->format->Ashift) +
alpha) >> 1) << surface->format->Ashift;
break;
case SPG_REPLACE_COLORKEY: // Replace the colorkeyed color
if(!(surface->flags & SDL_SRCCOLORKEY) || dc != surface->format->colorkey)
return;
if(Amask)
A = (alpha << surface->format->Ashift);
break;
}

            *pixel = R | G | B | A;
        break;
    }
}

}_________________________________________________________________
See what people are saying about Windows Live. Check out featured posts.
http://www.windowslive.com/connect?ocid=TXT_TAGLM_WL_connect2_082008

Wow, all that to draw alittle pixel, amazing!

Found a better way, thanks…

JeZ+Lee
of
Silent Hero Productions®

Jonathan Dearborn wrote:> Here’s the code from Sprig’s internal call. The switch entry for

SPG_DEST_ALPHA is probably the functionality you want with 32-bit
surfaces. That’s the same effect as from SDL_BlitSurface().

Jonny D

void _PutPixelAlpha(SDL_Surface surface, Sint16 x, Sint16 y, Uint32
color, Uint8 alpha)
{
if(x>=SPG_clip_xmin(surface) && x<=SPG_clip_xmax(surface) &&
y>=SPG_clip_ymin(surface) && y<=SPG_clip_ymax(surface)){
Uint32 Rmask = surface->format->Rmask, Gmask =
surface->format->Gmask, Bmask = surface->format->Bmask, Amask =
surface->format->Amask;
Uint32 R,G,B,A=SDL_ALPHA_OPAQUE;
Uint32
pixel;
switch (surface->format->BytesPerPixel) {
case 1: { /* Assuming 8-bpp */

                Uint8 *pixel = (Uint8 *)surface->pixels + 

y*surface->pitch + x;

                Uint8 dR = surface->format->palette->colors[*pixel].r;
                Uint8 dG = surface->format->palette->colors[*pixel].g;
                Uint8 dB = surface->format->palette->colors[*pixel].b;
                Uint8 sR = surface->format->palette->colors[color].r;
                Uint8 sG = surface->format->palette->colors[color].g;
                Uint8 sB = surface->format->palette->colors[color].b;
               
                dR = dR + ((sR-dR)*alpha >> 8);
                dG = dG + ((sG-dG)*alpha >> 8);
                dB = dB + ((sB-dB)*alpha >> 8);
           
                *pixel = SDL_MapRGB(surface->format, dR, dG, dB);
               
        }
        break;

        case 2: { /* Probably 15-bpp or 16-bpp */       
           
                Uint16 *pixel = (Uint16 *)surface->pixels + 

y*surface->pitch/2 + x;
Uint32 dc = *pixel;

                R = ((dc & Rmask) + (( (color & Rmask) - (dc & 

Rmask) ) * alpha >> 8)) & Rmask;
G = ((dc & Gmask) + (( (color & Gmask) - (dc &
Gmask) ) * alpha >> 8)) & Gmask;
B = ((dc & Bmask) + (( (color & Bmask) - (dc &
Bmask) ) * alpha >> 8)) & Bmask;
if( Amask )
A = ((dc & Amask) + (( (color & Amask) - (dc &
Amask) ) * alpha >> 8)) & Amask;

                *pixel= R | G | B | A;
               
        }
        break;

        case 3: { /* Slow 24-bpp mode, usually not used */
            Uint8 *pix = (Uint8 *)surface->pixels + y * 

surface->pitch + x*3;
Uint8 rshift8=surface->format->Rshift/8;
Uint8 gshift8=surface->format->Gshift/8;
Uint8 bshift8=surface->format->Bshift/8;
Uint8 ashift8=surface->format->Ashift/8;

                Uint8 dR, dG, dB, dA=0;
                Uint8 sR, sG, sB, sA=0;
               
                pix = (Uint8 *)surface->pixels + y * 

surface->pitch + x*3;

                dR = *((pix)+rshift8);
                dG = *((pix)+gshift8);
                dB = *((pix)+bshift8);
                dA = *((pix)+ashift8);
               
                sR = (color>>surface->format->Rshift)&0xff;
                sG = (color>>surface->format->Gshift)&0xff;
                sB = (color>>surface->format->Bshift)&0xff;
                sA = (color>>surface->format->Ashift)&0xff;
               
                dR = dR + ((sR-dR)*alpha >> 8);
                dG = dG + ((sG-dG)*alpha >> 8);
                dB = dB + ((sB-dB)*alpha >> 8);
                dA = dA + ((sA-dA)*alpha >> 8);

                *((pix)+rshift8) = dR;
                *((pix)+gshift8) = dG;
                *((pix)+bshift8) = dB;
                *((pix)+ashift8) = dA;
               
        }
        break;

        case 4: /* Probably 32-bpp */
            pixel = (Uint32*)surface->pixels + y*surface->pitch/4 + x;
            Uint32 dc = *pixel;
            R = color & Rmask;
            G = color & Gmask;
            B = color & Bmask;
            A = 0;  // keep this as 0 to avoid corruption of 

non-alpha surfaces

            switch(SPG_GetBlend())
            {
                case SPG_COMBINE_ALPHA:  // Blend and combine src 

and dest alpha
if( alpha != SDL_ALPHA_OPAQUE ){
R = ((dc & Rmask) + (( R - (dc & Rmask) )

  • alpha >> 8)) & Rmask;
    G = ((dc & Gmask) + (( G - (dc & Gmask) )

  • alpha >> 8)) & Gmask;
    B = ((dc & Bmask) + (( B - (dc & Bmask) )

  • alpha >> 8)) & Bmask;
    }
    if(Amask)
    A = ((((dc & Amask) >>
    surface->format->Ashift) + alpha) >> 1) << surface->format->Ashift;
    break;
    case SPG_DEST_ALPHA: // Blend and keep dest alpha
    if( alpha != SDL_ALPHA_OPAQUE ){
    R = ((dc & Rmask) + (( R - (dc & Rmask) )

  • alpha >> 8)) & Rmask;
    G = ((dc & Gmask) + (( G - (dc & Gmask) )

  • alpha >> 8)) & Gmask;
    B = ((dc & Bmask) + (( B - (dc & Bmask) )

  • alpha >> 8)) & Bmask;
    }
    if(Amask)
    A = (dc & Amask);
    break;
    case SPG_SRC_ALPHA: // Blend and keep src alpha
    if( alpha != SDL_ALPHA_OPAQUE ){
    R = ((dc & Rmask) + (( R - (dc & Rmask) )

  • alpha >> 8)) & Rmask;
    G = ((dc & Gmask) + (( G - (dc & Gmask) )

  • alpha >> 8)) & Gmask;
    B = ((dc & Bmask) + (( B - (dc & Bmask) )

  • alpha >> 8)) & Bmask;
    }
    if(Amask)
    A = (alpha << surface->format->Ashift);
    break;
    case SPG_COPY_SRC_ALPHA: // Direct copy with src alpha
    if(Amask)
    A = (alpha << surface->format->Ashift);
    break;
    case SPG_COPY_DEST_ALPHA: // Direct copy with dest
    alpha
    if(Amask)
    A = (dc & Amask);
    break;
    case SPG_COPY_COMBINE_ALPHA: // Direct copy with
    combined alpha
    if(Amask)
    A = ((((dc & Amask) >>
    surface->format->Ashift) + alpha) >> 1) << surface->format->Ashift;
    break;
    case SPG_COPY_NO_ALPHA: // Direct copy, alpha opaque
    if(Amask)
    A = (SDL_ALPHA_OPAQUE <<
    surface->format->Ashift);
    break;
    case SPG_COPY_ALPHA_ONLY: // Direct copy of just
    the alpha
    R = dc & Rmask;
    G = dc & Gmask;
    B = dc & Bmask;
    if(Amask)
    A = (alpha << surface->format->Ashift);
    break;
    case SPG_COMBINE_ALPHA_ONLY: // Blend of just the
    alpha
    R = dc & Rmask;
    G = dc & Gmask;
    B = dc & Bmask;
    if(Amask)
    A = ((((dc & Amask) >>
    surface->format->Ashift) + alpha) >> 1) << surface->format->Ashift;
    break;
    case SPG_REPLACE_COLORKEY: // Replace the
    colorkeyed color
    if(!(surface->flags & SDL_SRCCOLORKEY) || dc
    != surface->format->colorkey)
    return;
    if(Amask)
    A = (alpha << surface->format->Ashift);
    break;
    }

              *pixel = R | G | B | A;
          break;
      }
    

    }
    }


See what people are saying about Windows Live. Check out featured
posts. Check It Out!
http://www.windowslive.com/connect?ocid=TXT_TAGLM_WL_connect2_082008=


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

Hello !

Wow, all that to draw alittle pixel, amazing!

Found a better way, thanks…

What is the way, other people maybe interested in this ?

CU

Hi,

Had no idea that drawing a pixel was soo darn difficult.
Why does SDL not have a DrawPixel function?

I used small sprite images for this task, thanks

JeZ+Lee
of
Silent Hero Productions®

Torsten Giebl wrote:> Hello !

Wow, all that to draw alittle pixel, amazing!

Found a better way, thanks…

What is the way, other people maybe interested in this ?

CU


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

i would guess that it is because pixel drawing is very slow.On Sat, 23 Aug 2008, Jesse P. wrote:

Hi,

Had no idea that drawing a pixel was soo darn difficult.
Why does SDL not have a DrawPixel function?

I used small sprite images for this task, thanks

JeZ+Lee

In case you were wondering where I got the PutPixel function, it’s here:
Example 2-5. putpixel()

/*

  • Set the pixel at (x, y) to the given value

  • NOTE: The surface must be locked before calling this!
    */
    void putpixel(SDL_Surface surface, int x, int y, Uint32 pixel)
    {
    int bpp = surface->format->BytesPerPixel;
    /
    Here p is the address to the pixel we want to set */
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

    switch(bpp) {
    case 1:
    *p = pixel;
    break;

    case 2:
    *(Uint16 *)p = pixel;
    break;

    case 3:
    if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
    p[0] = (pixel >> 16) & 0xff;
    p[1] = (pixel >> 8) & 0xff;
    p[2] = pixel & 0xff;
    } else {
    p[0] = pixel & 0xff;
    p[1] = (pixel >> 8) & 0xff;
    p[2] = (pixel >> 16) & 0xff;
    }
    break;

    case 4:
    *(Uint32 *)p = pixel;
    break;
    }
    }Cheers!-Dave----- Original Message -----From: slnthero@aol.com (Jesse Palser)
    To: "A list for developers using the SDL library. (includes SDL-announce)"

    Sent: Saturday, August 23, 2008 6:19 AM
    Subject: Re: [SDL] How do I draw a single pixel with alpha value to screen?

Hi,

Had no idea that drawing a pixel was soo darn difficult.
Why does SDL not have a DrawPixel function?

I used small sprite images for this task, thanks

JeZ+Lee
of
Silent Hero Productions®

Torsten Giebl wrote:

Hello !

Wow, all that to draw alittle pixel, amazing!

Found a better way, thanks…

What is the way, other people maybe interested in this ?

CU


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


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

It’s not so hard, and frankly, the GetPixel and PutPixel routines in the
documentation are very useful, and could be adapted easily for your use. In
fact, you wouldn’t have to adapt them at all…
Simply make a call to SDL_MapRGBA (using the display surface’s format
(fmt)):
Uint32 SDL_MapRGBA(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b, Uint8
a);

It returns a Uint32, which is basically a color, which could include any
alpha value you want. Put that in the PutPixel routine, and put it on the
screen. Easy as pie.

-Dave> ----- Original Message -----

From: slnthero@aol.com (Jesse Palser)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Saturday, August 23, 2008 6:19 AM
Subject: Re: [SDL] How do I draw a single pixel with alpha value to screen?

Hi,

Had no idea that drawing a pixel was soo darn difficult.
Why does SDL not have a DrawPixel function?

I used small sprite images for this task, thanks

JeZ+Lee
of
Silent Hero Productions®

Torsten Giebl wrote:

Hello !

Wow, all that to draw alittle pixel, amazing!

Found a better way, thanks…

What is the way, other people maybe interested in this ?

CU


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


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

Hello !

It’s not so hard, and frankly, the GetPixel and PutPixel routines in
the documentation are very useful, and could be adapted easily for your
use. In fact, you wouldn’t have to adapt them at all…
Simply make a call to SDL_MapRGBA (using the display surface’s format (fmt)):
Uint32 SDL_MapRGBA(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

It returns a Uint32, which is basically a color, which could include
any alpha value you want. Put that in the PutPixel routine, and put it
on the screen. Easy as pie.

For first i thought that, too. But PutPixel replaces a pixel
with new RGBA values in the Surface, but if you want to alphablit
a single Pixel you have to do more.

CU