tarkhani
tarkhani

Reputation: 31

Getting RGBA value of PNG imgae in sdl2

I need RGBA value of image for generating height of my terrain and I'm using sdl for load image I looked around and found SDL_GetRGBA should return those value but every time I run this code my program crash...

SDL_Surface *image = IMG_Load(HightMapAddress);
SDL_LockSurface(image);
Uint32 *pixels = (Uint32 *)image->pixels;
Uint8* RED;
Uint8* GREEN;
Uint8* BLUE;
Uint8* ALPHA;
SDL_GetRGBA(pixels[0], image->format, RED, GREEN, BLUE, ALPHA);

Upvotes: 0

Views: 388

Answers (1)

keltar
keltar

Reputation: 18399

SDL_GetRGBA gets pointers to memory where it should write resulting colours. You passed uninitialised pointers, so said function will attempt to write to unknown location. Luckly it will crash, otherwise you'll stomp some random location in memory.

Correct code would be something like

Uint8 RED;
Uint8 GREEN;
Uint8 BLUE;
Uint8 ALPHA;
SDL_GetRGBA(pixels[0], image->format, &RED, &GREEN, &BLUE, &ALPHA);

Upvotes: 1

Related Questions