如何更改图像的 alpha 值而不需要每帧完全重新加载每个像素/优化
所以我试图通过改变每帧的 alpha 值来使图像在 sfml 1.6 中可见。不幸的是,图像没有整体的 alpha 值,所以我必须一个接一个地检查每个像素并更改它的 alpha 值。
然而,这非常慢,所以我想知道如何优化我的简单代码,或者是否有另一种 sfml 特定方法来处理这个问题。
无论如何,这是代码:
每个新帧我都会使用 Recolor a sprite增加的 alpha 值为 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;
}
so I'm trying to bring an image to visibility in sfml 1.6 by changing it's alpha value every frame. unfortunately there isn't an overall alpha value for the image, so i have to go through each pixel, one by one and change it's alpha value.
This is extremely slow however, so I wondering how I could possibly optimize my simple code, or if there was another sfml specific way to handle this.
anyway here's the code:
Each new frame I Recolor a sprite with a added alpha value of 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非我误解了你的问题,否则你应该能够使用 sf:: Drawable::SetColor 为此,给出白色作为参数,但带有不同的阿尔法值。例如,要将
sprite
的 alpha 设置为 50%,您可以执行以下操作:Unless I'm misunderstanding your question, you should be able to use sf::Drawable::SetColor for this, giving white as the argument but with a differing alpha value. For instance, to set
sprite
's alpha to 50%, you could do the following: