Archived

This topic is now archived and is closed to further replies.

athos_musketeer

getPixel in SDL

Recommended Posts

im trying to do a getpixel function in SDL.. i read the example but somehow it doesn''t work.. i have a 16 bits depth surface i locked it before getting the pixel. and im doing that: Uint16 *getPixel(SDL_Surface *surf, int x, int y) { Uint16 *pixel = (Uint16 *)surf->pixels + y * surf->pitch + x * 2; return pixel; } the pixel returned by the function i use it in the SDL_GetRGB.. but the r,g,b paramteres doesn''t get the real values.. why is that?

Share this post


Link to post
Share on other sites
The problem is that you are casting your pointer to a Uin16 pointer before adding the offset, try this:

Uint16 *pixel= (Uint16 *)(surf->pixels+y*surf->pitch + x*2);

If you have a Uin16 pointer and add a certain value ''i'' to it, in fact you are adding ''2*i'' to it, since each element has size = 2 bytes.

I hope this helps

Share this post


Link to post
Share on other sites