Need help with pixels.

I am using the SDL library, and came across a way to access pixel data.
I am fairly new to using pixels so i need you to explain to me a few things.

//A pointer to a surface structure
1
2
3
4
5
6
SDL_Surface *w = SDL_SetVideoMode(800,400,8,SDL_SWSURFACE);

//Casting a void pointer pointing to an array of memory locations(the pixels), to a pointer to an array of Uint8's.
//SDL_MapRGB() Returns an Uint32 containing the color 0,0,255,(0/255, i think it returns the transparency too, or else it would rather return a Uint24,not sure).
Then modifying the memory location, in this case the pixel at the middle of the window.
static_cast<Uint8*>(w->pixels)[(200*w->pitch)+400]=SDL_MapRGB(w->format,0,0,255);


What i seem to not understand is that, i cast pixels to Uint8*, which means i
now have a pointer to an array of 8bit numbers, but SDL_MapRGB seemingly returns an Uint32 or Uint24 for a color composed of r,g,b, together without 'a' is 24bits(256=1b,256=1b,256=1b)=3b.
And then i somehow assign that pixel which is of 8bit at chosen location, an Uint24 or 32bit unsigned int.

How does this work?
Each pixel is 8 bits because you passed 8 as the third argument to SDL_SetVideoMode. By returning a 32 bit number SDL_MapRGB can work with pixel formats of 8, 16, 24 or 32 bit pixels. In your case when only 8 bits are used SDL_MapRGB will return a value between 0-255, which is small enough to be stored in a 8 bit number.
Last edited on
Thank you.
So that means that, when specifying 8bpp, and calling the SDL_MapRGB() function with the r,g,b arguments seemingly filling the space of 3 bytes, are if transparency is involved returns a combined value composing 8/4 for each r,g,b or a. So the arguments become more like (r = 0/3,g=0/3,b=3/3), 2bits each, am i correct?
I don't think the pixel format for w in your code can have any transparency. Exactly how the pixel format is made up I don't know, and often it's not necessary to now.
Topic archived. No new replies allowed.