我正在创建一个有精灵的游戏,在游戏开始之前会显示所有将要使用的所有精灵,并为每个精灵创建一个纹理。我的问题是,当我创建一个对象时,我想给它与它对应的纹理相同的渲染纹理。例如如果新对象是草地砖,则给它与已经加载的草地砖纹理相同的纹理。
因为一切按预期运行,所以没有出错,问题是游戏使用了太多的内存(大约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);
}
这是包含精灵,纹理和位置变量的每个tile对象的类。
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;
当我想创建一个将要绘制的新Tile对象时,我会做什么:
Tile tile; //create a new tile object
tile = tileList[0] //give the new object the exact same values of the already created tile which includes, sprite, texture, etc
然后我会调用绘图函数将所有图块绘制到窗口
void Map::draw(){
tile.draw(); //call the tiles own draw function so it can be drawn
}
非常感谢任何帮助,实际代码更是如此。
感谢。