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


SDL get pixel probs

Recommended Posts

CoiN    122
Lo All, I'm using a function called getpixel() from one of the tutorials on gamedev, I was going to use to obtain a pixel in my collision detection but I'm having an annoying problem with it. Once I've called the function I am checking the returned color structure to see of the r, g, b are all 255 and if so it should mean that the pixel is part of the test surface, as I am using the test surface as a the surface param to getpixel(). It works, but it also seems to also return an r, g, b of 255 for certain parts of the screen which are totally blank, these spots seem to stay the same each time. Here is the code I use to call the function and test for collision (very basic, its just a test so far):
color = GetPixel(test, rect.x, rect.y);
if(color.r == 255 && color.g == 255  && color.b == 255)
     printf("Detected! %d %d", rect.x, rect.y);
The rect structure, is what I use to hold the position of my ship. And here is the getpixel() function:
SDL_Color GetPixel ( SDL_Surface* pSurface , int x , int y ) 
  SDL_Color color ;
  Uint32 col = 0 ;

  //determine position

  char* pPosition = ( char* ) pSurface->pixels ;

  //offset by y

  pPosition += ( pSurface->pitch * y ) ;

  //offset by x

  pPosition += ( pSurface->format->BytesPerPixel * x ) ;

  //copy pixel data

  memcpy ( &col , pPosition , pSurface->format->BytesPerPixel ) ;

  //convert color

  SDL_GetRGB ( col , pSurface->format , &color.r , &color.g , &color.b ) ;
  return ( color ) ;
Any Ideas? Sorry for the length of the post. Thanks in advance.... CoiN [edited by - coin on October 3, 2002 6:21:16 PM]

Share this post

Link to post
Share on other sites