我正在开发一个使用SDL的2D游戏。由于某些系统具有弱的CPU和强大的GPU,因此我有一个后端渲染器,除了纯粹的SDL /软件外还使用OpenGL。
简化版本的渲染器接口如下:
class Renderer {
public:
virtual void render_surface(SDL_Surface* surface) = 0;
virtual void render_text(const std::string& text, const Font& font) = 0;
};
但这里有一个问题:当我用OpenGL绘制表面时,我需要反复调用
glBindTexture
,这样会浪费很多时间。目前,我有一个基于表面内存地址的愚蠢缓存,但它显然无法处理动态生成的表面,例如在render_text
中。我能想到的唯一正确的解决方案是完全改变接口,并让调用者明智地缓存纹理:
class Renderer {
public:
virtual Texture load_surface(SDL_Surface* surface) = 0;
virtual Texture load_text(const std::string& text, const Font& font) = 0;
virtual void render_texture(const Texture& texture) = 0;
};
但是在我看来,这种方法有些丑陋,并且对于软件渲染器来说必须进行伪造。
还有其他什么我可以做的吗?