Sign in to follow this  
savail

How to create a dynamic array of textures?

Recommended Posts

Hey
It's me again :P. So I have:
[code]LPDIRECT3DTEXTURE9 texture;[/code]
in my header file. In my soruce file I was trying to add memory by this way:
[code]texture = new LPDIRECT3DTEXTURE9[10];
//or
texture = new IDIRECT3DTEXTURE9[10];[/code]
but none of this works. How to create dynamic array of textures?

Share this post


Link to post
Share on other sites
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:
[code]
#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
[/code]

Or:
[code]
std::vector<LPDIRECT3DTEXTURE9> textures(10);

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


...

// on exit
textures[i]->Release();
[/code]

Share this post


Link to post
Share on other sites
[quote name='savail' timestamp='1302978228' post='4799201']
Hey
It's me again :P. So I have:
[code]LPDIRECT3DTEXTURE9 texture;[/code]
in my header file. In my soruce file I was trying to add memory by this way:
[code]texture = new LPDIRECT3DTEXTURE9[10];
//or
texture = new IDIRECT3DTEXTURE9[10];[/code]
but none of this works. How to create dynamic array of textures?
[/quote]
By using [url="http://www.cplusplus.com/reference/stl/vector/"]std::vector[/url].
[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)[/size]


[code]
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[i];
}

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

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
[quote name='savail' timestamp='1302983905' post='4799237']
Thanks a lot for answers! BTW Servant is that a must to delete firstly elements of dynamic array and only after this clear it?
[/quote]
[color=#1C2837][size=2]Yes, you [b]must[/b] call 'delete' first, and then clear it.[/size][/color]
[color=#1C2837][size=2][quote][/size][/color]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.[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[size="2"][color="#1C2837"]But hey, why not wrap it into a function like this:[/color][/size]
[size="2"][color="#1C2837"][code]void ClearTextureVector(std::vector <LPDIRECT3DTEXTURE9> &myVector)[/color][/size]
[size="2"][color="#1C2837"]{[/color][/size]
[size="2"][color="#1C2837"] for(int i = 0; i < myVector.size(); i++)[/color][/size]
[size="2"][color="#1C2837"] {[/color][/size]
[size="2"][color="#1C2837"] //Either 'delete myVector[i];' Or however you are supposed to destroy DirectX textures. I don't know.[/color][/size]
[size="2"][color="#1C2837"] }[/color][/size]
[size="2"][color="#1C2837"]
[/color][/size]
[size="2"][color="#1C2837"] myVector.clear();[/color][/size]
[size="2"][color="#1C2837"]}[/color][/size][color=#1C2837][size=2][/code][/size][/color]
[size="2"][color="#1C2837"]And you can use it like this:[/color][/size]
[size="2"][color="#1C2837"][code]std::vector <LPDIRECT3DTEXTURE9> textureVector;[/color][/size]
[size="2"][color="#1C2837"]LPDIRECT3DTEXTURE9 texture = /* However you are supposed to create DirectX textures */;[/color][/size]
[size="2"][color="#1C2837"]textureVector.push_back(texture);[/color][/size]
[size="2"][color="#1C2837"]
[/color][/size]
[size="2"][color="#1C2837"]ClearTextureVector(textureVector);[/code][/color][/size]
[size="2"][color="#1C2837"]
[/color][/size]
[size="2"][color="#1C2837"]Note: I don't use DirectX, so I don't know the proper way to create or destroy [/color][/size][color=#1C2837][size=2]LPDIRECT3DTEXTURE9s. If you are supposed to call myTexture->release() or something, do that in the function instead.[/size][/color]

Share this post


Link to post
[quote name='savail' timestamp='1302983905' post='4799237']
Thanks a lot for answers! BTW Servant is that a must to delete firstly elements of dynamic array and only after this clear it?
[/quote]
Yes, you must call 'delete' first, and then clear it.
[quote]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:
[code]void ClearTextureVector(std::vector <LPDIRECT3DTEXTURE9> &myVector)
{
for(int i = 0; i < myVector.size(); i++)
{
//Either 'delete myVector[i];' Or however you are supposed to destroy DirectX textures. I don't know.[/color][/size]
}

myVector.clear();
}[/code]

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

ClearTextureVector(textureVector);[/code]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this