 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Fri Sep 01, 2006 6:30 pm Post subject: alter array size in struct? |
|
|
Hi folks,
Since i only know C and i do not know C++, i'm trying to make an .obj loader in c using a struct as some form of class. The problem however is that i do not know how to fill up an array of vertices inside a struct (change the number of the array), is it possible to initialize the array when you know the number of vertices you want to store? _________________ My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Fri Sep 01, 2006 10:20 pm Post subject: |
|
|
yes... if you use C++ it is really easy:
| Code: | #include <vector>
struct Vertex { float x, y, z; }
...
struct Mesh
{
std::vector<Vertex> verts;
};
...
Mesh* m = new Mesh;
int n = ReadNumVertices();
m->verts.resize(n);
for(int i = 0; i < n; ++i)
m->verts[i] = ReadVertex();
|
And If you need to pass the array to some function, which prefers a pointer to Vertex, it goes like this:
| Code: | void DrawVertexArrayThing(const Vertex* verts, int n);
...
DrawVertexArrayThink(&m->verts[0], m->verts.size());
|
If you prefer C, you use malloc:
| Code: | struct Mesh
{
Vertex* verts;
in nverts;
};
Mesh* m = new Mesh;
int n = ReadNumVertices();
m->verts = (Vertex*)malloc(n * sizeof(Vertex));
m->nverts = n;
for(int i = 0; i < n; ++i)
m->verts[i] = ReadVertex();
|
The std::vector will free the memory for you when you destroy the mesh, but with the C method, you will need to free the mesh yourself:
C:
| Code: | free(m->verts);
free(m);
|
C++
|
|
| Back to top |
|
 |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|