YourFace
<span style="color:#9C9C9C;"><strong>Runner Up - T
- Reaction score
- 91
Code:
Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
{
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)surface->pixels;
//Get the requested pixel
return pixels[ ( y * surface->w ) + x ];//<----------------------------------------------------------------------------------------------
}
Here's our functions to get and put pixels. In case you missed the bitmap font tutorial, here's a quick review on how pixel access works:
First thing we do is convert the pixel pointer from type void to 32bit integer so we can properly access them. After all, a surface's pixels are nothing more than an array of 32bit integers. Then we get or set the requested pixel.
You maybe be wondering why I don't just go "return pixels[ x ][ y ]".
The thing is the pixels aren't stored like this:
They're stored like this:
in a single dimensional array. It's because different operating systems store 2D arrays differently (At least I think that's why).
So to retrieve the red pixel from the array we multiply the y offset by the width and add the x offset.
These functions only work for 32-bit surfaces. You'll have to make one of your own if you're using a different format.
My problem is how does returning the Y offset times the surface width plus the x offset get the pixel color I need?