I’m not exactly sure how to ask this, or if it’s been asked before…
So here we go:
I’m trying to write a game in Objective-C for iOS. One main feature of this game is the ability to morph the 2D landscape (technically 3D, however it starts out as a flat, 2D plain with points scattered throughout it to make a perfect mesh of 20 1×1 squares) to the user’s liking; however, in Objective-C, apparently you cannot pass C-Typed arrays through methods.
e.g:
-(VertexS *) getVertexArrayData {
VertexS myArray[definedAmount]; //VertexS being a structure
for (int i = 0; i != definedAmount; i++) {
myArray[i] = vertexData;
}
return *myArray;
}
//In the other file (the one with all my rendering), under a different class, inside a method
//Lets pretend the aforementioned method was under a class called "Terrain"
-(id) initWithFrame:(CGRect)frame
{
terrain = [[[Terrain alloc] init] autorelease];
[terrain constructLand];
VertexS *_vertexData = [terrain getVertexArrayData];
... etc.
}
Apparently, it doesn’t work. Not saying that that is exactly what I’m doing, but very similar. What happens in my exact code is I use an ivar to hold the vertex/index data after it gets info from the other method.
Considering OpenGL (ES) requires either a vector container (well, in C++), or a C type Array (which is technically the same thing) in order to correctly create an IBO/VBO, I’m at a loss. I’ve been fiddling with it for almost a day and a half now, unable to get around the problem, and too (mentally) fat to learn NSMutableArrays–unless suggested by you, the wonderful community, in which I will pseudo-miraculously lose enough weight to learn NSMutableArrays (probably better said as “I will stop avoiding the unfamiliar solution,”).
I can’t actually remember my other question… Mreh. It probably isn’t relavent but if I remember I will find a way to ask it.
Anyways, any help is requested. I don’t know what else I can possibly do to get around this road block in my project.
Objective-C is a superset of the C language, so anything you can do in plain C will also work in Objective-C. The
myArrayarray is declared on the stack in-getVertexArrayData. It gets abandoned when the method returns and that memory is available for reuse by the next function call. To fix this, allocate the array on the heap usingmalloc()(or one of its relatives).C-style memory management doesn’t use reference counting, so don’t forget to free the array when you’re done with it: