C++ 如何最小化加载向量的时间?

C++ 如何最小化加载向量的时间?,c++,vector,rendering,sdl,C++,Vector,Rendering,Sdl,我目前正在尝试开发一款游戏,但我在地图方面遇到了一些问题 映射的工作方式如下:我们有一个名为Map的类,它将包含一个分片向量 class GMap { private : std::vector <BTiles> TileList; ... 然后,我可以按以下方式打印所有瓷砖: void GMap::Render(SDL_Surface *MainSurface) { for(int i = 0;i < TileList.size();i++) {

我目前正在尝试开发一款游戏,但我在地图方面遇到了一些问题

映射的工作方式如下:我们有一个名为Map的类,它将包含一个分片向量

class GMap
{
private :
    std::vector <BTiles> TileList;
...
然后,我可以按以下方式打印所有瓷砖:

void GMap::Render(SDL_Surface *MainSurface)
{
    for(int i = 0;i < TileList.size();i++)
    {
        TileList[i].OnRender(MainSurface); //I am calling a Render function inside the Tile Class. MainSurface is the primary surface im using to render images.
    }
很抱歉,我没有正确解释最后一个函数,但我认为我的问题不存在

无论如何,如果有人需要更多的信息,在代码的一部分,只要问


谢谢大家!

我发现了这个问题。每个瓷砖都有自己的表面,可以加载相同的图像。这意味着我生成了1024个曲面,并加载了1024个曲面。我解决这个问题的方法是在Map类中创建一个曲面,它将被所有瓷砖使用

所以

变成

   bool BTiles::OnLoad(int BType, int BID)
{
    Type = BType;
    ID = BID;
    return true;
}
在Map类中,我添加了MSurface,它将加载包含所有平铺块的图像。 然后要渲染,我将执行以下操作:

void GMap::Render(SDL_Surface *MainSurface)
{
    for(int i = 0;i < TileList.size();i++)
    {
        TileList[i].OnRender(MainSurface, MSurface, 0, 0);
    }
}
void GMap::Render(SDL_表面*MainSurface)
{
对于(int i=0;i
Msurface是包含图像的曲面

每个瓷砖都将MSsurface作为外表面接收,但它将用于保存所有图像


因此,我没有创建1024个曲面,而是只创建了1个。现在需要2秒钟来加载比以前多得多的内容。它还解决了我不渲染所有分片的问题。

您是否检查了所有
OnLoad
方法是否实际返回
true
?B文件有多复杂?如果您分析代码,它是否会在复制构造函数中花费大量时间?我之所以这么问是因为您至少要将每个BTiles对象从堆栈复制一次到向量存储,并且在向量重新分配时,会再次复制其中的许多对象。如果代码的加载部分是磁盘绑定的,如果将多个贴图部分合并到一个文件中,并且仅对每个磁贴使用相关部分,则可能会节省一些时间。在
OnRender
X和Y中,看起来可疑的是
ID
,而
OnDraw'的参数取决于
Type`。是否正确?所有OnLoad返回true。
void BTiles::OnRender(SDL_Surface *MSurface)
{
    int X = (ID * 16) % M_WIDTH; //Since i am only using the ID to know which position to put a Tile, i use this function to locate which Horizontal Position to put them. M_WIDTH is a global variable that defines the Width of the screen, it is currently 512
    int Y = ((ID * 16) / M_HEIGHT) * 16; //The same but for the Vertical Position. M_HEIGHT is currently also 512
    Surface::OnDraw(MSurface, BSurface, X, Y, (Type * 16) % M_WIDTH, (Type * 16) / M_HEIGHT, 16, 16); //This means Render(On The Primary Surface, using the Images on the BSurface, on the Position X, on the position Y, Where the Tile i want to render starts on the X line, Where the Tile i want to render starts on the Y line, it is 16 bits Width, it is 16 bits Height
}
bool BTiles::OnLoad(int BType, int BID)
{
    if((BSurface = Surface::OnLoad("BTexture.png")) == false)
        return false;
    Type = BType;
    ID = BID;
    return true;
}
   bool BTiles::OnLoad(int BType, int BID)
{
    Type = BType;
    ID = BID;
    return true;
}
void GMap::Render(SDL_Surface *MainSurface)
{
    for(int i = 0;i < TileList.size();i++)
    {
        TileList[i].OnRender(MainSurface, MSurface, 0, 0);
    }
}