Beginner problem with dynamic arrays

Started by
0 comments, last by Space_Panda 21 years ago
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.
Advertisement
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.

This topic is closed to new replies.

Advertisement