I have 2x2 image
Shouldn't the pixels be arranged like this?
1 2 // each number is a pixel
3 4
I'm having trouble accessing a pixel with x and y because when x = 1 and y = 0 i get index 2 but prints the rgb values of pixel 4
so it's something like?
1 2 // each number is a pixel
4 3
Here's the code that I use
index = y + x * s->w;
c = s->format->palette->colors[index]; // c is an SDL_Color and s is an SDL_Surface*
I also use this for loop and still prints the same
for (Uint8 i = *(Uint8 *)s->pixels; i < s->w*s->h; i++) {
c = s->format->palette->colors[i];
printf("%u %u %u %u \n", i, c.r , c.g , c.b);
}
SDL_Surface struct definition from the SDL documentation
typedef struct SDL_Surface {
Uint32 flags; /* Read-only */
SDL_PixelFormat *format; /* Read-only */
int w, h; /* Read-only */
Uint16 pitch; /* Read-only */
void *pixels; /* Read-write */
/* clipping information */
SDL_Rect clip_rect; /* Read-only */
/* Reference count -- used when freeing surface */
int refcount; /* Read-mostly */
/* This structure also contains private fields not shown here */
} SDL_Surface;
pixelsto access pixels,colorslooks like a color table. - Jean-Baptiste Yunès->pixels, not compute it. - HolyBlackCat