forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

alter array size in struct?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Fri Sep 01, 2006 6:30 pm    Post subject: alter array size in struct? Reply with quote

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
View user's profile Send private message
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Fri Sep 01, 2006 10:20 pm    Post subject: Reply with quote

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++

Code:
delete m
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Mon Sep 04, 2006 12:39 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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