user1585646
user1585646

Reputation: 421

Converting a typdef struct into NSMutableArray

I am trying to figure out how to take a typdef struct such as the following:

typedef struct {
    float Position[3];
    float Color[4];
    float TexCoord[2];
} const Vertex;

and turn it into seperate arrays.

I have the following as my conversion array:

+ (...)arrayConverter: (Vertex *) v
{
    // Turn typeDef struct into seperate arrays
    NSMutableArray *position          = [[NSMutableArray alloc] init];
    NSMutableArray *color             = [[NSMutableArray alloc] init];
    NSMutableArray *texcord           = [[NSMutableArray alloc] init];
    const NSMutableArray *vertexdata  = [[NSMutableArray alloc] init];

    for (int p=0; p<(sizeof(v->Position)/sizeof(v)); p++) {
        [position addObject:[NSNumber numberWithFloat:v->Position[p]]];
    }

    for (int c=0; c<(sizeof(v->Color)/sizeof(v)); c++) {
        [color addObject:[NSNumber numberWithFloat:v->Color[c]]];
    }

    for (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 some reason I only get the first line of data for each position, color and texcoord. How can I get the rest of the data I am 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}},
    // Back
    {{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
)

Upvotes: 1

Views: 199

Answers (3)

Shekhar Gupta
Shekhar Gupta

Reputation: 6218

Edit (According to your question):

 NSMutableArray *array = [NSMutableArray array];
 Vertex data;
 int i;
  for (i = 1;  i <= yourCount;  i++) 
  {
     NSValue *value = [NSValue valueWithBytes:&data objCType:@encode(Vertex)];
     [array addObject:value];
  }

To retrieve these structs as 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];

Upvotes: 7

Alexander Tkachenko
Alexander Tkachenko

Reputation: 3281

Sorry, I had misunderstood your question. Now I've understand that you need to output your 1 vertice position,color,texcoords into array. So quick answer is

+ (...)arrayConverter: (Vertex *) v
{
    // Turn typeDef struct into seperate arrays
    NSMutableArray *position          = [[NSMutableArray alloc] init];
    NSMutableArray *color             = [[NSMutableArray alloc] init];
    NSMutableArray *texcord           = [[NSMutableArray alloc] init];
    const NSMutableArray *vertexdata  = [[NSMutableArray alloc] init];

    for (int p=0; p<3; p++) {
        [position addObject:[NSNumber numberWithFloat:v->Position[p]]];
    }

    for (int c=0; c<4; c++) {
        [color addObject:[NSNumber numberWithFloat:v->Color[c]]];
    }

    for (int t=0; t<2; t++) {
        [texcord addObject:[NSNumber numberWithFloat:v->TexCoord[t]]];
    }

    [vertexdata addObjectsFromArray:position];
    [vertexdata addObjectsFromArray:color];
    [vertexdata addObjectsFromArray:texcord];

    return vertexdata;
}

Upvotes: 0

Alexander Tkachenko
Alexander Tkachenko

Reputation: 3281

You may try convert you Vertex struct to NSData before adding to array. It is much simpler approach to store structs in NSArray.

// make a NSData object
NSData *myData = [NSData dataWithBytes:&myStruct length:sizeof(myStruct)];

// make a new PacketJoin
MyStruct myStruct;
[myData getBytes:&myStruct length:sizeof(myStruct)];

Upvotes: 2

Related Questions