iphone - Converting a typdef struct into NSMutableArray -


i trying figure out how take typdef struct such following:

typedef struct {     float position[3];     float color[4];     float texcoord[2]; } const vertex; 

and turn seperate arrays.

i have following conversion array:

+ (...)arrayconverter: (vertex *) v {     // turn typedef struct seperate arrays     nsmutablearray *position          = [[nsmutablearray alloc] init];     nsmutablearray *color             = [[nsmutablearray alloc] init];     nsmutablearray *texcord           = [[nsmutablearray alloc] init];     const nsmutablearray *vertexdata  = [[nsmutablearray alloc] init];      (int p=0; p<(sizeof(v->position)/sizeof(v)); p++) {         [position addobject:[nsnumber numberwithfloat:v->position[p]]];     }      (int c=0; c<(sizeof(v->color)/sizeof(v)); c++) {         [color addobject:[nsnumber numberwithfloat:v->color[c]]];     }      (int t=0; t<(sizeof(v->texcoord)/sizeof(v)); t++) {         [texcord addobject:[nsnumber numberwithfloat:v->texcoord[t]]];     }      [vertexdata addobjectsfromarray:position];     [vertexdata addobjectsfromarray:color];     [vertexdata addobjectsfromarray:texcord];  nslog(@"\n sizeof position: %lu\n sizeof color: %lu\n sizeof texcoord: %lu\n sizeof vertex: %lu\n", sizeof(v->position), sizeof(v->color), sizeof(v->texcoord), sizeof(v)); nslog(@"\n position array: %@\n color array: %@\n texcord array: %@\n",position, color, texcord); nslog(@"\n vertex data: %@\n",vertexdata);      return vertexdata; } 

for reason first line of data each position, color , texcoord. how can rest of data passing in.

see below of data being passed in.

const vertex square_vertices[] = {     // front     {{0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 0}},     {{0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 1}},     {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 1}},     {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 0}},     //     {{0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},     {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},     {{0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}},     {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},     // left     {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 0}},     {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 1}},     {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},     {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}},     // right     {{0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},     {{0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},     {{0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 1}},     {{0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 0}},     // top     {{0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 0}},     {{0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},     {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},     {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 0}},     // bottom     {{0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},     {{0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 1}},     {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 1}},     {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}} }; 

nslog information:

2013-07-31 01:42:45.346  sizeof position: 12  sizeof color: 16  sizeof texcoord: 8  sizeof vertex: 4 2013-07-31 01:42:45.347   position array: (     "0.5",     "-0.5",     1 )  color array: (     0,     0,     0,     1 )  texcord array: (     1,     0 ) 2013-07-31 01:42:45.348    vertex data: (     "0.5",     "-0.5",     1,     0,     0,     0,     1,     1,     0 ) 

edit (according question):

 nsmutablearray *array = [nsmutablearray array];  vertex data;  int i;   (i = 1;  <= yourcount;  i++)    {      nsvalue *value = [nsvalue valuewithbytes:&data objctype:@encode(vertex)];      [array addobject:value];   } 

to retrieve these structs follows:

nsvalue *structvalue = [array objectatindex:0]; vertex *mynode = (vertex *)[structvalue pointervalue]; 

for example:

float postionvalue = mynode->position[0]; float colorvalue = mynode->color[1]; float textcoordvalue = mynode->texcoord[1]; 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -