I'm creating a game which has sprites, at the start of the game before anything is shown all of the sprites which will be used will be loaded and a texture will be created for each of the sprites. My problem is that when I create an object I want to give it the same rendered texture as the texture it corresponds to. e.g. if the new object is a grass tile then give it the same texture as the already loaded grass tile texture.
Nothing goes wrong as such because everything works as intended, the problem is that the game uses way too much memory (around the 150 MB mark) because of the Tile::Draw function, anywhere before the Tile::draw function is called the game only uses around 10 MB (which is just the images which are loaded at the start). So my question is how do I reuse the same texture over and over without having to render it and still draw it to the window. I'll give some of my code for drawing.
Function which draws the sprites.
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();
}
Function which creates the sprite texture.
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);
}
This is the class for each tile object which holds the sprite, texture, and position variables.
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
};
When the tiles are first created at the start of the game they are stored in a vector array so they can be accessed later
std::vector<Tile> tileList;
Here is what I would do when I want to create a new Tile object which will be drawn:
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
I would then later call a draw function to draw all of the tiles to the window
void Map::draw(){
tile.draw(); //call the tiles own draw function so it can be drawn
}
Any help would be greatly appreciated and the actual code even more so.
Thanks.
Aucun commentaire:
Enregistrer un commentaire