我正在制作一款只使用带有黑白像素的PNG的游戏 . 但有时我会想要将白色像素的颜色更改为不同的颜色,如绿色(#00FF00) .
我怎么会这样做呢?
编辑:好的,我找到了解决方案
这是一个简单的功能:
void setColor(SDL_Surface *surface, SDL_Color color) {
Uint16 *pixels = (Uint16 *) surface->pixels; // Get the pixels from the Surface
// Iterrate through the pixels and chagne the color
for (int i = 0; i w * surface->h); i++) {
if (pixels[i] == SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF)) // Search for white pixels
pixels[i] = SDL_MapRGB(surface->format, color.r, color.b, color.g);
}
}
需要记住的一点是,如果使用的是32位表面,请将“Uint16”更改为“Uint32”;对于8位表面,请将“Uint8”更改为“Uint32” .
我不确定这段代码有多快,但它完成了工作 .
1 回答
这取决于你正在尝试设置颜色的确切内容 .
没有更多信息,立即想到的两个API是“SDL_SetColors()”和“SDL_SetPalette()” .
但真正的答案是“它取决于” .