I wrote this function that I want to use in a program, but for some reason, it fails despite nothing going wrong:
std::deque <std::deque <bool> > load_image(std::string & image_name){
SDL_Surface * image = open_image(image_name);
if (!image)
exit(3);
Uint32 * pixels = (Uint32 *) image -> pixels;
std::deque <std::deque <bool> > grid(HEIGHT, std::deque <bool>(WIDTH, false));
for(int y = 0; y < std::min(image -> h, HEIGHT); y++)
for(int x = 0; x < std::min(image -> w, WIDTH); x++)
grid[y][x] = (pixels[(image -> w * y) + x] == 0);
SDL_FreeSurface(image);
return grid;
}
I'm simply trying to copy whether or not the pixel is black into the grid
. When I run grid[y][x]
and (pixels[(image -> w * y) + x] == 0)
separately, the program runs fine. When I do grid[y][x] = (pixels[(image -> w * y) + x] == 0);
, the program crashes somewhere in the middle of the image.
I'm pretty sure (image -> w * y) + x
gets the correct pixel, no matter what x
and y
are limited to, so what am I not seeing??
You forgot to mention whether it crashes reading or writing memory. Also you could try debugging it - either via VisualStudio/gdb or by dumping values of
y
andx
intostderr
/OutputDebugString
.Nope.
To address single pixel use:
or
Pixel is not guaranteed to be 32bit. Also, if this is not a software surface, you'll need to lock it. See documentation for SDL_Surface and SDL_PixelFormat