I'm trying to create a class that creates quads dynamically using vectors.
The idea is to have a struct with the Curstom vertex:
struct CUSTOMVERTEX {
D3DXVECTOR3 p;
float tu,tv;
};
and a struct of Buffers:
struct BUFFER {
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
LPDIRECT3DINDEXBUFFER9 indexBuffer;
};
i've then created a pointer to vector of type BUFFER:
vector<BUFFER> *buffer;
So the first method of my class creates the primitives: it creates a temporary struct of type BUFFER to store a new index and vertex buffer.
After managing all the work i try to push_back the struct into my buffer vector:
void Sprite::CreatePrimitive(LPDIRECT3DDEVICE9 device,int width,int height,float x,float y, float z)
{
BUFFER tempBuffer;
tempBuffer.vertexBuffer = NULL;
tempBuffer.indexBuffer = NULL;
device->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),D3DUSAGE_WRITEONLY,CUSTOMFVF,D3DPOOL_MANAGED,&tempBuffer.vertexBuffer,NULL);
CUSTOMVERTEX *vertices;
tempBuffer.vertexBuffer->Lock(0,0,(void**)&vertices,0);
vertices[0].p = D3DXVECTOR3 (x,y,z);
vertices[0].tu = 0.0f;
vertices[0].tv = 0.0f;
vertices[1].p = D3DXVECTOR3 (x + width,y,z);
vertices[1].tu = 1.0f;
vertices[1].tv = 0.0f;
vertices[2].p = D3DXVECTOR3 (x + width,y + height,z);
vertices[2].tu = 1.0f;
vertices[2].tv = 1.0f;
vertices[3].p = D3DXVECTOR3 (x,y + height,z);
vertices[3].tu = 0.0f;
vertices[3].tv = 1.0f;
tempBuffer.vertexBuffer->Unlock();
device->CreateIndexBuffer(6*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&tempBuffer.indexBuffer,NULL);
WORD *indices = 0;
tempBuffer.indexBuffer->Lock(0,0,(void**)&indices,0);
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 3;
indices[5] = 0;
tempBuffer.indexBuffer->Unlock();
buffer->push_back(tempBuffer);
}
But when running the program the debug will stop at this line when trying to push_back the struct:
bool _Inside(const _Ty *_Ptr) const
{ // test if _Ptr points inside vector
return (_Ptr < this->_Mylast && this->_Myfirst <= _Ptr);
}
If i try to manage the function by giving 2 different vectors instead of the BUFFER type struct:
vector<LPDIRECT3DVERTEXBUFFER9> vb;
vector<LPDIRECT3DINDEXBUFFER9> ib;
i can push_back the elements of the struct :
ib.push_back(tempBuffer.indexBuffer);
vb.push_back(tempBuffer.vertexBuffer);
This will not give me any problem.
I can actually see that the problem has something to do with the pointers but i cannot figure out what i'm missing. The code works as intended with 2 separated vectors, so i could keep on work with those, but it would be a pain to :
- not know the problem;
- having to loop without an iterator for my drawing method (since i would need 2 different iterator in 1 loop);
Could you point me if and what I am doing wrong?
Thanks in advance