【发布时间】:2014-07-27 02:39:19
【问题描述】:
作为一种简单的方法来渲染除颜色之外的多个相同纹理,我将一个纯白色圆圈加载到 SDL_Texture 中,然后调用 SDL_SetTextureColorMod() 给它我想要制作圆圈的颜色。
如果纹理是单独的(示例 1),这一切都可以正常工作,但如果我共享 SDL_Texture 以便多个对象都引用它,这意味着必须在对象渲染之前的每个渲染帧调用 SDL_SetTextureColorMod()纹理,因为它上次提供的颜色可能已被另一个对象更改(示例 2)。
是否在每个渲染帧都调用SDL_SetTextureColorMod(),因为可能有相当多的对象共享一个纹理,会导致严重的性能问题?
需要它的原因是系统是使用具有基本引用计数的共享纹理功能设计的(我知道使用智能指针可能有更好的方法来做到这一点,但这不是这里讨论的主题)。让每个对象拥有自己的SDL_Texture 副本会更好吗?这样它只需设置一次颜色(或在需要更改时)而不是每个渲染帧?
示例 1:
SDL_Texture* tex1;
SDL_Texture* tex2;
SDL_Texture* tex3;
...
// All 3 instances have their own SDL_Texture
MyObject A(tex1);
MyObject B(tex2);
MyObject C(tex3);
...
// A call to set the color of the texture is only required once for each class
示例 2:
SDL_Texture* tex;
...
// All 3 instances share the SDL_Texture
MyObject A(tex);
MyObject B(tex);
MyObject C(tex);
...
// A call to set the color of the texture must be made before rendering
// each object to ensure that any other object has not set a different color.
// E.g if the draw order was A, B, C and the color was set to be different
// for each object then before rendering B, each frame it would need to set
// the color again otherwise it would have the color of A and the same
// for the others
编辑:这也将扩展到 SDL_SetTextureAlphaMod() 和 SDL_SetTextureBlendMode() 或其他类似功能
作为参考,我使用的是 SDL2。
更新:最初认为对函数的调用只是更新颜色混合标志。我做了更多的调查,这是部分正确的,可以在 -http://pastebin.com/pMjgVkmM
中的函数中看到但是,如果渲染器有一个分配给SetTextureColorMod() 的函数(大多数情况下似乎都是这种情况),那么它会映射到一个函数 'SW_SetTextureColorMod()' - http://pastebin.com/qYtxD0TH
这反过来又调用SDL_SetSurfaceColorMod() - http://pastebin.com/GrsVibAz
我担心此时可能会调用 SDL_InvalidateMap,尽管我不确定 - http://pastebin.com/r0HGJYHT
【问题讨论】:
-
我建议在这篇文章中添加 C 或 C++ 标签以启用语法高亮