I added code to check the rgb values of my surface, and whenever i call the function it seems to work fine! the only problem is that whenever the mouse goes under y = 400 the entire window crashes... Right now the program outputs the rgb color of the pixel on the screen, and when it goes below 400 one or all of the pixel values are 0, and then the program crashes. I have no ideas why this is happening. Any help is appreciated! Heres the github repo! https://github.com/jcsq6/SDLGame not much has been done except a couple classes to make my life easier in SDL :)
int* texture::getPixel(int x, int y){
static int Color[4];
SDL_PixelFormat *fmt;
Uint32 temp, pixel;
Uint32 *pixels = (Uint32 *)Surface->pixels;
Uint8 red, green, blue, alpha;
fmt = Surface->format;
SDL_LockSurface(Surface);
pixel = pixels[(y * Surface->w) + x];
SDL_UnlockSurface(Surface);
temp = pixel & fmt->Rmask;
temp = temp >> fmt->Rshift;
temp = temp << fmt->Rloss;
red = (Uint8)temp;
temp = pixel & fmt->Gmask;
temp = temp >> fmt->Gshift;
temp = temp << fmt->Gloss;
green = (Uint8)temp;
temp = pixel & fmt->Bmask;
temp = temp >> fmt->Bshift;
temp = temp << fmt->Bloss;
blue = (Uint8)temp;
temp = pixel & fmt->Amask;
temp = temp >> fmt->Ashift;
temp = temp << fmt->Aloss;
alpha = (Uint8)temp;
Color[0] = red;
Color[1] = green;
Color[2] = blue;
Color[3] = alpha;
return Color;
}
it's worth noting that when I call this function, the input y and x values are the mouse's position.
surface->format->BytesPerPixel == 4
? \$\endgroup\$ – JohnFilleau Mar 18 '20 at 2:09BytesPerPixel
I think, and use that to inform what you need to cast theSurface->pixels
to. \$\endgroup\$ – JohnFilleau Mar 18 '20 at 2:13Amask
is. If it's 0, that makes sense. You only have RGB. \$\endgroup\$ – JohnFilleau Mar 18 '20 at 2:15