如何在SDL2中排列像素?

问题描述 投票:0回答:1

我有2x2图像

像素不应该像这样排列吗?

1 2 // each number is a pixel
3 4

我在访问x和y的像素时遇到问题,因为当x = 1且y = 0时,我得到索引2但是打印像素4的rgb值

这是什么样的?

1 2 // each number is a pixel
4 3

这是我使用的代码

index = y + x * s->w;
c = s->format->palette->colors[index]; // c is an SDL_Color and s is an SDL_Surface*

我也使用它for循环并仍然打印相同

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文档中的SDL_Surface结构定义

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;
c++ sdl-2
1个回答
0
投票
Uint8 *pixel = (Uint8 *)s->pixels,*index;
index = &pixel[y + x * s->pitch];
c = s->format->palette->colors[*index];

得到它使用@ holyblackcat的建议。

© www.soinside.com 2019 - 2024. All rights reserved.