How to create a dynamic array of textures?

Started by
4 comments, last by savail 13 years ago
Hey
It's me again :P. So I have:
LPDIRECT3DTEXTURE9 texture;
in my header file. In my soruce file I was trying to add memory by this way:
texture = new LPDIRECT3DTEXTURE9[10];
//or
texture = new IDIRECT3DTEXTURE9[10];

but none of this works. How to create dynamic array of textures?
Advertisement
You need to use LPDIRECT3DTEXTURE9* (notice the *), in order to have an array of textures. I would recommend using std::vector<LPDIRECT3DTEXTURE9> instead, as it will automatically handle resizing, allocation and deallocation of the array. Remember that you still have to manually call ->Release() on each texture in the vector when you're done with it.

You can do it like this:

#include <vector>

...

std::vector<LPDIRECT3DTEXTURE9> textures;

LPDIRECT3DTEXTURE9 texture;
pDevice->CreateTexture(... &texture ..);

textures.push_back(texture);// add a texture to the end, vector will automatically resize


Or:

std::vector<LPDIRECT3DTEXTURE9> textures(10);

pDevice->CreateTexture(... &textures[0]..);
..
pDevice->CreateTexture(... &textures[9]..);


...

// on exit
textures->Release();

Hey
It's me again :P. So I have:
LPDIRECT3DTEXTURE9 texture;
in my header file. In my soruce file I was trying to add memory by this way:
texture = new LPDIRECT3DTEXTURE9[10];
//or
texture = new IDIRECT3DTEXTURE9[10];

but none of this works. How to create dynamic array of textures?

By using std::vector.
[size="1"](Note: a vector (resizeable storage container) and a vector (mathematical concept describing a line) are two different things, they just happen to use the same name)



std::vector< LPDIRECT3DTEXTURE9 > myDynamicArray;

LPDIRECT3DTEXTURE9 texture = new LPDIRECT3DTEXTURE9;
myDynamicArray.push_back(texture);

OR:

myDynamicArray.push_back(new LPDIRECT3DTEXTURE9);

//Then access it like normal:
myDynamicArray[3] //Access the 4th texture (since it starts counting at [0]).

//Also you can do things like this:
int numberOfTextures = myDynamicArray.size();

Or this:
for(int i = 0; i < numberOfTextures; i++)
{
//Since we only created the textures one at a time and added them to the array, we don't want to
//call delete[], instead we just call regular delete, and delete them one at a time just as we created them one at a time.
delete myDynamicArray;
}

myDynamicArray.clear(); //After deleting the textures, now we empty the dynamic array.

Thanks a lot for answers! BTW Servant is that a must to delete firstly elements of dynamic array and only after this clear it? I wonder if I can just clear it without deleting all elements one by one?

Thanks a lot for answers! BTW Servant is that a must to delete firstly elements of dynamic array and only after this clear it?

[color=#1C2837][size=2]Yes, you must call 'delete' first, and then clear it.
[color=#1C2837][size=2]
I wonder if I can just clear it without deleting all elements one by one?[/quote]
[color=#1C2837][size=2]No, you can't do that, it'll look like it's working fine, but really you are leaking memory, and it's a very bad practice to get into.
[color=#1C2837][size=2]

[color="#1C2837"]But hey, why not wrap it into a function like this:
[color="#1C2837"]void ClearTextureVector(std::vector <LPDIRECT3DTEXTURE9> &myVector)
[color="#1C2837"]{
[color="#1C2837"] for(int i = 0; i < myVector.size(); i++)
[color="#1C2837"] {
[color="#1C2837"] //Either 'delete myVector;' Or however you are supposed to destroy DirectX textures. I don't know.
[color="#1C2837"] }
[color="#1C2837"]

[color="#1C2837"] myVector.clear();
[color="#1C2837"]}[color=#1C2837][size=2]

[color="#1C2837"]And you can use it like this:
[color="#1C2837"]std::vector <LPDIRECT3DTEXTURE9> textureVector;
[color="#1C2837"]LPDIRECT3DTEXTURE9 texture = /* However you are supposed to create DirectX textures */;
[color="#1C2837"]textureVector.push_back(texture);
[color="#1C2837"]

[color="#1C2837"]ClearTextureVector(textureVector);

[color="#1C2837"]

[color="#1C2837"]Note: I don't use DirectX, so I don't know the proper way to create or destroy [color=#1C2837][size=2]LPDIRECT3DTEXTURE9s. If you are supposed to call myTexture->release() or something, do that in the function instead.

Thanks a lot for answers! BTW Servant is that a must to delete firstly elements of dynamic array and only after this clear it?

Yes, you must call 'delete' first, and then clear it.
I wonder if I can just clear it without deleting all elements one by one?[/quote]
No, you can't do that, it'll look like it's working fine, but really you are leaking memory, and it's a very bad practice to get into.

But hey, why not wrap it into a function like this:
void ClearTextureVector(std::vector <LPDIRECT3DTEXTURE9> &myVector)
{
for(int i = 0; i < myVector.size(); i++)
{
//Either 'delete myVector;' Or however you are supposed to destroy DirectX textures. I don't know.
}

myVector.clear();
}


And you can use it like this:
std::vector <LPDIRECT3DTEXTURE9> textureVector;
LPDIRECT3DTEXTURE9 texture = /* However you are supposed to create DirectX textures */;
textureVector.push_back(texture);

ClearTextureVector(textureVector);


Note: I don't use DirectX, so I don't know the proper way to create or destroy LPDIRECT3DTEXTURE9s. If you are supposed to call myTexture->release() or something, do that in the function instead.
Thanks a lot again for help

This topic is closed to new replies.

Advertisement