vector stuff

Started by
13 comments, last by Zahlman 17 years, 2 months ago
I want to use a STL vector to fill a buffer with. I change from the normal array to vector but i still dont know how to set up the size of the buffer corectly.



struct Vertex
{
    float x, y, z;
    DWORD color;
};




        vector<Vertex> pointList(6);


	pointList[0].color = (D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ));
	pointList[0].x = 0.0f; pointList[0].y = 0.0f; pointList[0].z = 0.0;

	pointList[0].color = (D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ));
	pointList[0].x = 0.5f; pointList[0].y = 0.0f; pointList[0].z = 0.0;

	pointList[0].color = (D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ));
	pointList[0].x = -0.5f; pointList[0].y = 0.0f; pointList[0].z = 0.0;

	pointList[0].color = (D3DCOLOR_COLORVALUE( 1.0, 1.0, 0.0, 1.0 ));
	pointList[0].x = 0.0f; pointList[0].y = -0.5f; pointList[0].z = 0.0;

	pointList[0].color = (D3DCOLOR_COLORVALUE( 0.0, 1.0, 1.0, 1.0 ));
	pointList[0].x = 0.0f; pointList[0].y = 0.5f; pointList[0].z = 0.0;




//Now the buffer:
//Here I dont know how to size the buffer corectly acording to the vector size.

LPDIRECT3DVERTEXBUFFER9 g_pPointList_VB     = NULL;

Vertex *pVertices = NULL;

MyBuffer.Device->CreateVertexBuffer( 6*sizeof(pointList), 0, D3DFVF_MY_VERTEX,
									                                      D3DPOOL_DEFAULT, &g_pPointList_VB, NULL );


    pVertices = NULL;
    g_pPointList_VB->Lock( 0, sizeof(pointList), (void**)&pVertices, 0 );
    memcpy( pVertices, &pointList, sizeof(pointList.size()) );
    g_pPointList_VB->Unlock();

Mybuffer is just a LPDIRECT3DVERTEXBUFFER9 buffer;



Thnks
Advertisement
Untested:
struct Vertex{    float x, y, z;    DWORD color;};vector<Vertex> pointList(6);pointList[0].color = (D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ));pointList[0].x = 0.0f; pointList[0].y = 0.0f; pointList[0].z = 0.0;pointList[1].color = (D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ));pointList[1].x = 0.5f; pointList[1].y = 0.0f; pointList[1].z = 0.0;pointList[2].color = (D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ));pointList[2].x = -0.5f; pointList[2].y = 0.0f; pointList[2].z = 0.0;pointList[3].color = (D3DCOLOR_COLORVALUE( 1.0, 1.0, 0.0, 1.0 ));pointList[3].x = 0.0f; pointList[3].y = -0.5f; pointList[3].z = 0.0;pointList[4].color = (D3DCOLOR_COLORVALUE( 0.0, 1.0, 1.0, 1.0 ));pointList[4].x = 0.0f; pointList[4].y = 0.5f; pointList[4].z = 0.0;// pointList[5]?//Now the buffer://Here I dont know how to size the buffer corectly acording to the vector size.LPDIRECT3DVERTEXBUFFER9 g_pPointList_VB     = NULL;Vertex *pVertices = NULL;MyBuffer.Device->CreateVertexBuffer( pointList.size() * sizeof(pointList::value_type), 0, D3DFVF_MY_VERTEX, D3DPOOL_DEFAULT, &g_pPointList_VB, NULL );pVertices = NULL;g_pPointList_VB->Lock( 0, sizeof(pointList::value_type), (void**)&pVertices, 0 );// c++ provides better (i.e. typesafe) functions for copyingstd::copy(pointList.begin(), pointList.end(), pVertices);g_pPointList_VB->Unlock();
You should probably also consider giving your Vertex struct a constructor and using the push_back member function of vector. Oh, and please read this.

Σnigma
I dont understand this:
pointList::value_type


Sorry
for a declaration of:
vector<Vertex> pointList

pointList::value_type would return Vertex

it makes life easier, since there is less code to change if you ever change the type in the vector
declaration, you dont have to go changing it everywhere in the code.
Quote:Original post by KulSeran
for a declaration of:
vector<Vertex> pointList

pointList::value_type would return Vertex

it makes life easier, since there is less code to change if you ever change the type in the vector
declaration, you dont have to go changing it everywhere in the code.



''unknown-type'': illegal sizeof operand. I cant use pointList::value_type

'value_type' : undeclared identifier

ITS anything im missing ? :(
Sorry, my mistake. I somehow got confused and thought pointList was a typedef as well as a variable. If you had, for example:
typedef std::vector< Vertex > PointList;PointList pointList;
Then you could use PointList::value_type to get the type of the objects contained within the vector. This means that if you change the contained type in the typedef the changes propogate through to the rest of your code automatically, which is a good thing™.

It would probably be a good idea for you to introduce such a typedef, but in it's absence you can use it's equivalent std::vector< Vertex >::value_type, or simply Vertex where I'd erroneously put pointList::value_type.

Σnigma
MyBuffer.Device->CreateVertexBuffer( pointList.size() * sizeof(vector<Vertex >::value_type), 0, D3DFVF_MY_VERTEX,D3DPOOL_DEFAULT, &g_pPointList_VB,NULL);pVertices = NULL;g_pPointList_VB->Lock( 0, sizeof(vector<Vertex>::value_type), (void**)&pVertices, 0 );memcpy( pVertices, &pointList, sizeof(vector<Vertex>::value_type));g_pPointList_VB->Unlock();I still see only one point !! :(
try multiplying all the sizeof(...) by vector.size() for whatever you call your vector

- Do this in the Lock and in the memcpy

[EDIT]
sizeof(vector<Vertex>::value_type) is worthless (you'd better use sizeof(Vertex)) or alternatively typedef vector<Vertex> to something you're comfortable with and use vertexVector::value_type for example.
[ my blog ]
Quote:Original post by arithma
try multiplying all the sizeof(...) by vector.size() for whatever you call your vector

- Do this in the Lock and in the memcpy


I did now i dont see anything (not even that litle point it was before)

And btw , thats a strange behavior with VS2005 C++ Pro. If i compile the project I see a dot, then i make some modifications and i dont see anything, when i undo, instead of dot again I dont see anything or i see 3 points instead of one (even with the same code). Is this random bulding ? Sometimes the IDE forgets to build and just runs the last exe ? How can I stop that ?

MyBuffer.Device->CreateVertexBuffer(pointList.size() * sizeof(Vertex), 0, D3DFVF_MY_VERTEX,D3DPOOL_DEFAULT, &g_pPointList_VB,NULL);pVertices = NULL;g_pPointList_VB->Lock( 0,pointList.size() * sizeof(Vertex), (void**)&pVertices, 0 );memcpy( pVertices, &pointList,pointList.size() * sizeof(Vertex) );g_pPointList_VB->Unlock();


[Edited by - Azzazelus on January 26, 2007 3:47:55 PM]
Should I go back to the simple array ?
(sorry fot up)

This topic is closed to new replies.

Advertisement