I currently am trying to code a small game which involves moving a dot around a maze. Rather than building the maze with rectangles, I decided to simply create a PNG image of the maze and load it as a surface and I also got the dot to move around the screen as well.
Now, I have been trying to make sure that the dot (which is more like a small box) to stay on a certain color and prevent it from moving into the walls. I am trying to read the pixel ahead of the movement and see whether it's black (a wall) or it's white (empty space). I have noticed the function SDL_RenderReadPixels, however I can't seem to make it work:
int w, h, access;
void* pixels;
Uint32 format;
const char* s;
SDL_QueryTexture(map->texture, &format, &access, &w, &h);
// Access = 0; width = 900; height = 720
s = SDL_GetPixelFormatName(format);
// Format: ARGB8888
SDL_Log("%s", s);
// Now I got pitch as: 900 * 32 = 28800
w = SDL_RenderReadPixels(map->renderer, NULL, 0, pixels, 28800);
I keep getting a segmentation fault. Now, I am not entirely sure if this would be the easiest method to achieve what I am trying to do. I can't seem to find an easier function within the SDL2 library that accomplishes this either, and I haven't found a solution searching around.
- Is there a reason why this is causing seg faults?
- Is there an easier and more efficient way for me to achieve this in SDL2?
I would like some feedback on my approach. I am still a beginner. The code is in C.
Thanks,
glReadPixels.