简体   繁体   English

在SDL2中创建TextureManager的最有效方法

[英]Most efficient way of creating a TextureManager in SDL2

I'm working on a small SDL2 example, and i'm thinking about creating a TextureManager. 我正在研究一个小的SDL2示例,并且正在考虑创建TextureManager。 I have a small abstraction, a class that handles IMG_LoadTexture and SDL_RenderCopy. 我有一个小的抽象,一个处理IMG_LoadTexture和SDL_RenderCopy的类。 During the constructor, i run IMG_LoadTexture and store the SDL_Texture* on a member variable. 在构造函数期间,我运行IMG_LoadTexture并将SDL_Texture *存储在成员变量上。 After that, if the render() method is called, i run SDL_RenderCopy. 之后,如果调用render()方法,则运行SDL_RenderCopy。 I want those abstracted Texture objects to live inside a TextureManager, which has the renderer instance and handle all rendering. 我希望那些抽象的Texture对象驻留在TextureManager中,该纹理管理器具有渲染器实例并处理所有渲染。

What's the best approach? 最好的方法是什么? A vector, with all Texture instances? 一个带有所有Texture实例的向量? How can this be more efficient? 如何提高效率? Is this a proper abstraction? 这是适当的抽象吗?

The type of container that is most efficient relates to how often you add, delete, or access the elements, rather than the type of the elements. 最有效的容器类型与添加,删除或访问元素的频率有关,而不是与元素的类型有关。

vector is best if you access them a lot but don't make changes: O(1) for random access, but up to O(N) for adding (likely less), O(N) for deletions if you care about ordering, O(1) otherwise. 如果您经常访问向量但不进行更改,向量是最佳选择:O(1)用于随机访问,但是如果您关心排序,则最多O(N)用于添加(可能更少),O(N)用于删除,否则为O(1)。

list is good if you make a lot of insertions, but the insertions are all at the ends of the list: O(N) for accesses and deletions, but O(1) to add to either end. 如果您要进行很多插入操作,则list会很好,但是插入操作都在列表的末尾:O(N)用于访问和删除,但是O(1)可添加到任一端。

I could go on (deque, map), but I'd say it's dead certain that access is the most common needs, so vectors are the way to go. 我可以继续进行(双端队列,地图),但是我肯定不能确定访问是最常见的需求,因此矢量是必经之路。

I have to wonder if you're asking something else, though, since you add detail about SDL. 我想知道您是否还在问其他问题,因为您添加了有关SDL的详细信息。 But your question is about what kind of abstraction will give you the most efficiency. 但是您的问题是哪种抽象将为您带来最大的效率。 Is that the question you intended to ask? 那是你想问的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM