Jump to content
  • Advertisement
Sign in to follow this  
DarioMicera

Vector of a Struct

This topic is 2563 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

An easy question:

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 :D

Share this post


Link to post
Share on other sites
Advertisement
What message are you getting?

Do you point "buffer" at something, or allocate space for it? Is there a reason you're using a pointer to a vector than just a vector?


If i try to manage the function by giving 2 different vectors instead of the BUFFER type struct:
[/quote]
These are vector objects, not pointers. I suspect removing the indirection from the declaration and use of the "buffer" variable should work fine.

Share this post


Link to post
Share on other sites

What message are you getting?

Do you point "buffer" at something, or allocate space for it? Is there a reason you're using a pointer to a vector than just a vector?


If i try to manage the function by giving 2 different vectors instead of the BUFFER type struct:

These are vector objects, not pointers. I suspect removing the indirection from the declaration and use of the "buffer" variable should work fine.
[/quote]

thanks for pointing that out. I'm new to vectors and had messed up with managing it as a pointer.

so it is now:

vector<BUFFER> buffer;

Still the program gives me the same error:


[font="Tahoma, Arial, Helvetica, sans-serif"]First-chance exception 0x00be4329 in Warring Factions.exe: 0xC0000005: access violation reading location 0x00000008.[/font]

It seems like nothing has changed.

Share this post


Link to post
Share on other sites
I'm trying to initialize the buffer's objects to NULL but more exception will be triggered.

It seems related to the fact that my vector has no size, could it be possible?

Because if i try to use an iterator to loop into the buffer vector, it will trigger an exception on the:

size_type size() const
{ // return length of sequence
return (this->_Mylast - this->_Myfirst);
}



or

iterator begin()
{ // return iterator for beginning of mutable sequence
return (iterator(this->_Myfirst, this));
}



Any clue? :S

Share this post


Link to post
Share on other sites

I'm trying to initialize the buffer's objects to NULL but more exception will be triggered.[
[/quote]
Initialise what objects? A std::vector instance is empty initially. Unless you put something in it, there is nothing to initialise. How are you attempting this initialisation?


Because if i try to use an iterator to loop into the buffer vector, it will trigger an exception on the:
[/quote]
If you are using the following loop, and it doesn't work, then you must have corrupted your vector somehow.

for(std::vector<Foo>::iterator i = v.begin() ; i != v.end() ; ++i)
{
// use *i
}



It seems related to the fact that my vector has no size, could it be possible?
[/quote]
It is perfectly legal to call size(), begin() and end() on an empty vector. If this is failing, you must have corrupted your vector.

Share this post


Link to post
Share on other sites


I'm trying to initialize the buffer's objects to NULL but more exception will be triggered.[

Initialise what objects? A std::vector instance is empty initially. Unless you put something in it, there is nothing to initialise. How are you attempting this initialisation?


Because if i try to use an iterator to loop into the buffer vector, it will trigger an exception on the:
[/quote]
If you are using the following loop, and it doesn't work, then you must have corrupted your vector somehow.

for(std::vector<Foo>::iterator i = v.begin() ; i != v.end() ; ++i)
{
// use *i
}



It seems related to the fact that my vector has no size, could it be possible?
[/quote]
It is perfectly legal to call size(), begin() and end() on an empty vector. If this is failing, you must have corrupted your vector.
[/quote]

what i meant by initializing the object of the vector is initializing the vertex buffer and index buffer pointer to NULL since i was suspicious that it could be related to that.

I could clearly state that my vector it's corrupted, because i tried to use that loop.

Also even if i call any vector method (resize, clear, etc) i will be given an exception.


Share this post


Link to post
Share on other sites
Just in case it could help, here is the header file of the class i'm using.

#ifndef SPRITE_H
#define SPRITE_H

#include <d3d9.h>
#include <d3dx9.h>
#include <vector>

using namespace std;


#define CUSTOMFVF D3DFVF_XYZ | D3DFVF_TEX1

class Sprite
{
public:
struct CUSTOMVERTEX {
D3DXVECTOR3 p;
float tu,tv;
};

struct BUFFER {
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
LPDIRECT3DINDEXBUFFER9 indexBuffer;
};

vector<BUFFER> buffer;
void CreatePrimitive(LPDIRECT3DDEVICE9 device,int width,int height,float x,float y,float z);
void DrawPrimitive(LPDIRECT3DDEVICE9 device,LPDIRECT3DTEXTURE9 texture);
void Destroy();
};
#endif



The problem is that even if i create a vector for the vertex buffer and another for the index buffer i will get the same error now.

I've noticed that if i declare the tempBuffer in the header of the class it won't let me initialize, throwing an exception, while if i do it into the method it will work normally.

Share this post


Link to post
Share on other sites
Are you sure you have a valid Sprite instance? Calling Sprite::CreatePrimitive() or Sprite::DrawPrimitve on an null/initialised pointer, or an invalid reference, could have this behaviour. Also your Direct3D calls do not appear to have any error checking.

Share this post


Link to post
Share on other sites

Are you sure you have a valid Sprite instance? Calling Sprite::CreatePrimitive() or Sprite::DrawPrimitve on an null/initialised pointer, or an invalid reference, could have this behaviour. Also your Direct3D calls do not appear to have any error checking.


This sprite class was built before as a non-vector class to test a single instance. While trying to make it more dynamic i made my Sprite object in my main a pointer to the sprite class and that was what was causing the problem.

I'm sorry for the trouble by realizing it was just a stupid error.

Since it was the first time for me using the vectors i was more worried that i could have been wrong in that context :P

Thanks as always everyone, the community it's just so cool :]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!