Reputation: 340
I tried to set the color of a pixel using this function:
void set_pixel(SDL_Surface *surface, SDL_Color, int x, int y)
{
Uint32 pixel= SDL_MapRGB(surface->format, color.r, color.g, color.b);
Uint32 *target_pixel = (Uint8 *) surface->pixels + y * surface->pitch +
x * sizeof *target_pixel;
*target_pixel = pixel;
}
Unfortunately it's not working, I guess it's because my SDL_Surface has 24 bit per pixel but SDL_MapRGB returns a Uint32. Should I convert my SDL_Surface to 32 bit per pixel or is there a way to convert the Uint32 pixel to be 24 bit?
Upvotes: 0
Views: 562
Reputation: 7534
You'll end up needing to mask 3 of the Uint32
bytes in pixel
while keeping the 4th byte of target_pixel
unchanged (keep endianness in mind).
something like this should be close, but doesn't account for endianness:
//assumes pixel has 0x00 for unused byte and assumes LSB is the unused byte
*target_pixel = pixel | (*target_pixel & 0xff)
Incidentally, your target_pixel
calculation appears to be wrong. You should be multiplying by the number of bytes-per-pixel, not the sizeof(Uint32)
.
Upvotes: 2