所以我試圖通過改變它的每幀的alpha值的圖像在sfml 1.6中的可見性。不幸的是,圖像並沒有一個整體的alpha值,所以我必須逐個檢查每個像素並更改它的alpha值。如何更改圖像的alpha值,而不是完全重新加載每個像素每幀/優化
然而,這是非常緩慢的,所以我想知道我怎麼可能優化我的簡單代碼,或者如果有另一個sfml特定的方式來處理這個。
反正這裏是代碼:
每一個新的幀我重新着色與1.7添加α值一個精靈。
// @Return Ptr: a pointer to the stack allocated image so the
// user can deallocate it later
sf::Image* RecolorSprite(sf::Sprite& sprite, sf::Color filter, bool subtract){
// the image has to survive so it's put ont he stack
sf::Image* image = new sf::Image;
*image = *sprite.GetImage();
RecolorImage(*image, filter, subtract);
sprite.SetImage(*image);
return image;
}
void RecolorImage(sf::Image& image, sf::Color filter, bool subtract){
for(int x= 0; x< image.GetWidth(); x++){
for(int y= 0; y< image.GetHeight(); y++){
if(subtract){
sf::Color pixel = image.GetPixel(x, y);
SubtractColor(pixel, filter);
image.SetPixel(x, y, pixel);
}
else
image.SetPixel(x, y, image.GetPixel(x, y) + filter);
}
}
}
// int used to stop illegal operations on unsigned chars
void SubtractColor(sf::Color& col1, sf::Color& col2){
int diff = ((int)col1.r) - ((int)col2.r);
if(diff >= 0)
col1.r -= col2.r;
else
col1.r = 0;
diff = ((int)col1.g) - ((int)col2.g);
if(diff >= 0)
col1.g -= col2.g;
else
col1.g = 0;
diff = ((int)col1.b) - ((int)col2.b);
if(diff >= 0)
col1.b -= col2.b;
else
col1.b = 0;
diff = ((int)col1.a) - ((int)col2.a);
if(diff >= 0)
col1.a -= col2.a;
else
col1.a = 0;
}
爲什麼不與所有不同的顏色/阿爾法預先創建的精靈。精靈的精靈。這樣你只需要使用正確的精靈而不是在運行時重新計算圖像。您將在開始時使用更多的空間加載所有的精靈過渡,但在渲染循環過程中會更快。 –