Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Space_Panda

Beginner problem with dynamic arrays

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

Hi. I''m trying to make classes for my Direct3D programming. I have a class that handles DirectX initialization where I can easily retrieve the device. This works correctly. However my first class I''m trying to make is a simple Object2D class where you fill in the position and color. You can also specify how many vertices this object is to have. My private variables in my header contain this. LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer; LPDIRECT3DDEVICE8 g_pD3DDevice; int totalVertices; struct CUSTOMVERTEX { FLOAT x, y, z, rhw; // The transformed position for the vertex. DWORD colour; // The vertex colour. }; Object2D::CUSTOMVERTEX *vertices; As you''ll see I have a pointer to the Custom Vertex structure called vertices. When I make a call to Object2D I pass in an integer. So this occurs. Object2D::Object2D(int num) { Object2D::totalVertices = num; Object2D::g_pVertexBuffer = NULL; vertices = new CUSTOMVERTEX[num]; } And I have a function to build up my vertices. bool Object2D::buildVertex(int vertexNum,FLOAT x, FLOAT y, FLOAT z, FLOAT rhw, DWORD colour) { if(vertexNum < Object2D::totalVertices) { Object2D::vertices[vertexNum].colour = colour; Object2D::vertices[vertexNum].rhw = rhw; Object2D::vertices[vertexNum].x = x; Object2D::vertices[vertexNum].y = y; Object2D::vertices[vertexNum].z = z; return true; } else return false; } Now when I ran the program I was just getting a blank screen. If I put sizeof(vertices) in a message box I''d get 4 outputted. But if I say put this before I created the vertex buffer: CUSTOMVERTEX vertices[] = { //Vertex 1 - Red (250, 100) {250.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 2 - Green (400, 350) {400.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 3 - Blue (100, 350) {100.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),}, }; and then did a sizeof(vertices) I''d get 60 and the triangle would display. Does anyone know what I''m doing wrong? I''m pretty sure it has to do with my arrary declaration. And yes I''m filling up the vertex array with points and color.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I have never used DirectX before, but I would assume you are passing something incorrectly to DX or you are calling your own buildVertex with wrong values? The array allocation looks fine. The first time you called sizeof() and got 4 this was the size of the pointer, *vertices, that you had declared...sizeof returned the size of a pointer not the size of what you were pointing to. The second time you called it and got 60 you had filled in the structure with stuff that actually added up to 60.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!