Jump to content
  • Advertisement
Sign in to follow this  
savail

How to create a dynamic array of textures?

This topic is 2593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Hidden

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.

Share this post


Link to post

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!