Jump to content
  • Advertisement
Sign in to follow this  
kzar

Putting pixels with alpha stuff in sdl

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I was wondering if its possible to put single pixels onto a surface with alpha transparency (in SDL)? I have read the docs but I have only seen a way of setting the alpha channel of a surface not a pixel. Incase it helps here is how I am drawing the pixels without the transparency:
struct Sparticle
{
    float x_vol, y_vol;
    int x_pos, y_pos;
    
    float alpha_decay;
    SDL_Color color;
};

void draw_particle(struct Sparticle particle1)
{
     Uint32 *p, color;
     SDL_Rect src, dest;
     
     color = SDL_MapRGB(screen->format, particle1.color.r, particle1.color.g, particle1.color.b);
     
     SDL_LockSurface(screen);
     
     p = (unsigned int*)screen->pixels + ( particle1.y_pos * (screen->pitch / 4) ) + particle1.x_pos;
     *p = color;

     SDL_UnlockSurface(screen); 
} 


Thanks, David

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by philipptr
i think you could only draw a pixel in RGBA on a surface with RGBA... (so not directly on the screen surface)


Surely making a 1x1 surface to blit each time I want to put a pixel isnt the right way of doing it though? :o

Share this post


Link to post
Share on other sites
Firstly, you'll have to switch over to a buffer bitmap instead of drawing directly to the screen. philipptr is correct. So you need to create an SDL_Surface * that is the same size as the screen, do all of your drawing to it, then finally blit it to the screen in one fell swoop. (Thus is double-buffering born.)

Then you can use the same code you have there, with a simple change from SDL_MapRGB to SDL_MapRGBA. Note that your code is not entirely portable to every conceivable type of pixel data your surface contain; I'd recommend changing the lines "p = (unsigned int*)screen->pixels + ( particle1.y_pos * (screen->pitch / 4) ) + particle1.x_pos; *p = color;" to a call to putpixel() (the source for putpixel can be found in the SDL docs; I disremember exactly where).

Hope that helps,
Twilight Dragon

Share this post


Link to post
Share on other sites
Just got it working, thanks for the tips guys. (Should I blit everything to this double_buffer surface instead of directly to the screen now or does it not matter?)

Share this post


Link to post
Share on other sites
Yes, you should blit everything to the double-buffering surface. For consistency, and it will actually save a bit of processing in the end.

Cheers,
Twilight Dragon

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!