C++ 如何反复使用一个渲染的精灵纹理

C++ 如何反复使用一个渲染的精灵纹理,c++,textures,sprite,directx-9,C++,Textures,Sprite,Directx 9,我正在创建一个有精灵的游戏,在游戏开始之前,所有将要使用的精灵都将被加载,并为每个精灵创建一个纹理。我的问题是,当我创建一个对象时,我想给它与它对应的纹理相同的渲染纹理。e、 g.如果新对象是草瓷砖,则为其提供与已加载的草瓷砖纹理相同的纹理 没有任何问题,因为一切都按预期工作,问题是游戏使用了太多的内存(大约150 MB),因为Tile::Draw函数,在调用Tile::Draw函数之前的任何地方,游戏只使用大约10 MB的内存(这只是开始加载的图像)。因此,我的问题是如何反复使用相同的纹理,而

我正在创建一个有精灵的游戏,在游戏开始之前,所有将要使用的精灵都将被加载,并为每个精灵创建一个纹理。我的问题是,当我创建一个对象时,我想给它与它对应的纹理相同的渲染纹理。e、 g.如果新对象是草瓷砖,则为其提供与已加载的草瓷砖纹理相同的纹理

没有任何问题,因为一切都按预期工作,问题是游戏使用了太多的内存(大约150 MB),因为Tile::Draw函数,在调用Tile::Draw函数之前的任何地方,游戏只使用大约10 MB的内存(这只是开始加载的图像)。因此,我的问题是如何反复使用相同的纹理,而不必渲染它,并且仍然将其绘制到窗口。我将给出一些绘图代码

用于绘制精灵的函数

void Tile::Draw(){ //here is where each sprite is drawn 
    sprite->Begin(D3DXSPRITE_ALPHABLEND); 
    sprite->Draw(tex, NULL, NULL, &position, colour); //here is where more memory is used each time a sprite is drawn
    sprite->End();
}
创建精灵纹理的函数

void Tile::CreateTexture(IDirect3DDevice9* device){
    D3DXCreateTextureFromFileEx(device, filePath, getDimensions().width, getDimensions().height, 1, D3DPOOL_DEFAULT,
    D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &tex);

    //Attempt to create the sprite
    D3DXCreateSprite(device, &sprite);
}
这是包含精灵、纹理和位置变量的每个平铺对象的类

class Tile{
public:
    void setIndex(int x, int y) { position.x = x; position.y = y; position.z = 0; } //used for setting the position of the tile
    void setColour(D3DCOLOR colour) { this->colour = colour; } //used for setting the colour value of the object 
    void setTexture(LPDIRECT3DTEXTURE9 *tex) { this->tex = *tex; } //used for setting the texture value of the object
    void setSprite(LPD3DXSPRITE sprite) { this->sprite = sprite; } //used for setting the sprute value of the object
    void CreateTexture(IDirect3DDevice9* device); //used for creating the texture for the object

    LPDIRECT3DTEXTURE9 getTexture() { return tex; } //used for returning the texture of the object
    LPD3DXSPRITE getSprite() { return sprite; } //used for returning the sprite of the object

    void Draw(); //used for drawing the object

protected:
    D3DXVECTOR3 position; //used for storing the position of the object
    LPDIRECT3DTEXTURE9 tex; //used for storing the texture of the object
    LPD3DXSPRITE sprite; //used for storing the sprute value of the object
    D3DCOLOR colour; //used for storing the colour value of the object
};
当游戏开始时第一次创建瓷砖时,它们存储在向量数组中,以便以后可以访问

    std::vector<Tile> tileList;
然后,我会调用一个draw函数来将所有的瓷砖绘制到窗口

void Map::draw(){
    tile.draw(); //call the tiles own draw function so it can be drawn
}
任何帮助都将不胜感激,实际代码更是如此


谢谢。

所以,我不清楚您当前的尝试出了什么问题。你想说什么就说什么,然后扔掉一些代码。该代码中有什么具体错误?我是说,一条真实的线路,发生了一些你不想发生的事情?加上需要调用该代码的代码(至少是草图,但理想情况下是MCVE)。已编辑@YakkAll我想做的是使用相同纹理绘制多个精灵,这样就不会使用越来越多的内存,因为它将在@Yakk上反复绘制相同的渲染纹理
void Map::draw(){
    tile.draw(); //call the tiles own draw function so it can be drawn
}