Jump to content
  • Advertisement
Sign in to follow this  

changing the color of a pixel in SDL

This topic is 3088 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

Hello everyone. I'm new to SDL and I have the following simpple question: How can I change the color of a pixel of a surface? I'm using these functions: Uint32 get_pixel( SDL_Surface* surface, int x, int y ) { Uint32 *pixels = (Uint32*)surface->pixels; return pixels[ ( y * surface->w ) + x ]; } void put_pixel( SDL_Surface *surface, int x, int y, Uint32 pixel ) { Uint32 *pixels = (Uint32 *)surface->pixels; pixels[ ( y * surface->w ) + x ] = pixel; } My problem is that get_pixel() gives a Uint32 and I don't know how to change for example the red value by 10. How can I convert it to something that I could handle? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
The Uint32 contains RGB (possibly alpha values too), each in 8 of the 32 bits. It usually looks like this: 0xXXRRGGBB. So to extract the color values you do the following:

red = (pixel >> 16) & 0xff
green = (pixel >> 8) & 0xff
blue = pixel & 0xff


Depending on the format of the back-buffer blue/red could be switched for example. Each color will be in the range [0, 255], and after you have modified the separate channels individually, you rebuild the pixel color with:

pixel = (red << 16) | (green << 8) | blue


Remember that red/green/blue have to stay in [0, 255].

Share this post


Link to post
Share on other sites
Yes, though all x86/x64 based systems have the same endianess. It could still be true though, as the backbuffer format can change. For example on a BGR format the B/R channels are switched compared to an RGB. And some application might have both ARGB and RGBA formats, though I'm not sure if they are both actually used.. at least DirectX only specifies one, though it's referred to both as RGBA (DX10) and ARGB (DX9). They both have the most significant byte as alpha (<< 24).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!