簡體   English   中英

如何在24位SDL_Surface上設置像素的顏色?

[英]How to set the color of pixel on 24 bit SDL_Surface?

我嘗試使用此功能設置像素的顏色:

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;
}

不幸的是,它無法正常工作,因為我的SDL_Surface每像素具有24位,但是SDL_MapRGB返回了Uint32。 我應該將SDL_Surface轉換為每個像素32位,還是可以將Uint32像素轉換為24位?

您最終需要掩蓋Uint32字節中的3個字節(以pixel為單位),同時保持target_pixel的第4個字節不變(請記住字節序)。

像這樣的東西應該很接近,但不考慮字節順序:

//assumes pixel has 0x00 for unused byte and assumes LSB is the unused byte
*target_pixel = pixel | (*target_pixel & 0xff)

順便說一句,您的target_pixel計算似乎是錯誤的。 您應該乘以每像素字節數,而不是sizeof(Uint32)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM