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?
alter array size in struct?
alter array size in struct?
My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
yes... if you use C++ it is really easy:
And If you need to pass the array to some function, which prefers a pointer to Vertex, it goes like this:
If you prefer C, you use malloc:
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:
C++
Code: Select all
#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();
Code: Select all
void DrawVertexArrayThing(const Vertex* verts, int n);
...
DrawVertexArrayThink(&m->verts[0], m->verts.size());
Code: Select all
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();
C:
Code: Select all
free(m->verts);
free(m);
Code: Select all
delete m
Thanks that is really helpfull :) thanks again
My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php