   SDL - Pixel Color Detection At Point
  How do I find the color of a pixel at a coordinate?Like for example, I want to find the color of the pixel at the position of the mouse.How would I do it?   
  Are you using SDL's software rendering functionality, or are you using hardware rendering via (e.g.) OpenGL or Direct3D?   ;  SDL's software rendering   ;Quote:SDL's software renderingI've never really done much with SDL's software rendering, but I would imagine there's some way to access the contents of the frame buffer.If you can get access to the raw frame buffer data, then it should be a simple matter of doing the necessary arithmetic to find the bytes in the array corresponding to the pixel at the mouse position, and then reading those bytes to see what the color is.Sorry I can't provide more detail than that - perhaps someone who has more experience with SDL's software rendering functionality can offer some additional information.   ;  SDL Stores the information about the screen surface in a regular SDL_Surface object, that stores the pixel data in a single dimension array.  You would need to convert the mouse coordinates from an x,y pair to a single value.Unfortunately it's a bit more complex than that.  The pixel data is pointed to by a void* and it's up to you to determine the pixel offset and stride based on the values that SDL gives you in the surface's SDL_PixelFormat object.#include <SDL/SDL.h>struct pix32{unsigned char r, g, b, a;};int main( int argc, char* args[] ){SDL_Init(SDL_INIT_EVERYTHING);SDL_Surface *screen;screen = SDL_SetVideoMode(800, 600, 32, NULL);SDL_Event event;bool quit = false;while(!quit){while(SDL_PollEvent(&event)){if(event.type== SDL_QUIT){quit = true;}}pix32 color = {255, 0, 255, 255};SDL_FillRect(screen,&screen->clip_rect, *((Uint32*)&color) );pix32 colorin; // the value of colorin should be R 255, G 0, B 255, A 255 after this callcolorin = ((pix32*)screen->pixels)[0];SDL_Flip(screen);}    return 0;    }This code assumes that the image is stored in RGBA format (not BGR or RGB) with a bit size of 32 per pixel.This is an extremely quick example and by no means is the proper way of doing it, it's just for brevity and conveying the concept.  You SHOULD use the pixelformat objects R,G,B masks and R,G,B shifts and write a function that works independently of the surface's pixelformat.Read this article for a much more thorough example and better code.[Edited by - zerothrillz on May 30, 2010 1:30:31 AM]