Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-08-2010, 02:20 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 15
SamBaylus is on a distinguished road
Default Calloc help, maybe? For my game engine!

The objects in my game are put inside of an NSMutableArray called "gameObjectArray." Each time it renders, it cycles through each object in the array, pulls out each vertex information, and inserts it into an array called vertexArray of type HOSpriteVertex (which is just a structure holding vertex and color structures).

Code:
-(void)draw{
	int vertexCount = 0;
	
	HOSpriteVertex *vertexArray = calloc([gameObjectArray count]*6, sizeof(HOSpriteVertex));
	
	
	for (int i = 0; i < [gameObjectArray count]; i ++) 
	{
		HOGameObject *temp = [gameObjectArray objectAtIndex:i];
		vertexArray[vertexCount++] = HOSpriteVertexMake(HOVertexMake(temp->x, temp->y), HOColorMake(temp->red, temp->green, temp->blue, temp->alpha));
		vertexArray[vertexCount++] = HOSpriteVertexMake(HOVertexMake(temp->x + 32, temp->y), HOColorMake(temp->red, temp->green, temp->blue, temp->alpha));
		vertexArray[vertexCount++] = HOSpriteVertexMake(HOVertexMake(temp->x, temp->y + 32), HOColorMake(temp->red, temp->green, temp->blue, temp->alpha));
		
		vertexArray[vertexCount++] = HOSpriteVertexMake(HOVertexMake(temp->x, temp->y + 32), HOColorMake(temp->red, temp->green, temp->blue, temp->alpha));
		vertexArray[vertexCount++] = HOSpriteVertexMake(HOVertexMake(temp->x + 32, temp->y + 32), HOColorMake(temp->red, temp->green, temp->blue, temp->alpha));
		vertexArray[vertexCount++] = HOSpriteVertexMake(HOVertexMake(temp->x +32, temp->y), HOColorMake(temp->red, temp->green, temp->blue, temp->alpha));
	}
	
	glVertexPointer(2, GL_FLOAT, sizeof(HOSpriteVertex), &vertexArray[0].vertex);
    glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(HOSpriteVertex), &vertexArray[0].color);
	
    glDrawArrays(GL_TRIANGLES, 0, [gameObjectArray count]*6);
	
	vertexCount = 0;
	free(vertexArray);
}
So to add to the array, simply add objects into the mutable array and this will insert their vertex information to OpenGL ES to render.

Problem is, I'm not getting the performance I was hoping for, and I believe it has to do with how I'm using calloc() and free() each rendering method. Is this correct at all? How SHOULD it look? Any thing you see here I should avoid?
SamBaylus is offline   Reply With Quote
Old 06-08-2010, 11:53 AM   #2 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Mr Jack is on a distinguished road
Default

Yeah, calloc zero initialises the memory (ick), no need to be using that. You shouldn't be re-allocated memory like that every frame, either. Much better to alloc all you need and be done, but I doubt that's your time sink either way.

What's all that HOSpriteVertexMake stuff doing? Why are you calling that colour making function so often?
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 06-09-2010, 12:43 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 15
SamBaylus is on a distinguished road
Default Hey!

Oh, alright. I'll try to malloc a large array at the beginning and just feed into that one. Butttttttt the HOSpriteVertex is a structure containing vertex and color structures. These are assigned to each vertex of the HOGameObject, and take on the properties of the HOGameObject.

So each object has 4 vertexes, that's why I'm creating them each run-through and feeding them into the array. I'm making the color each time and creating a structure to put into the interleaved array. Would it be less expensive to create the structures in the UPDATE method and reference to them during the DRAW method? Is creating structures too taxing for that?
SamBaylus is offline   Reply With Quote
Old 06-09-2010, 03:41 AM   #4 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Mr Jack is on a distinguished road
Default

Quote:
Originally Posted by SamBaylus View Post
So each object has 4 vertexes, that's why I'm creating them each run-through and feeding them into the array. I'm making the color each time and creating a structure to put into the interleaved array. Would it be less expensive to create the structures in the UPDATE method and reference to them during the DRAW method? Is creating structures too taxing for that?
Could you post the code for HOSpriteVertexMake, HOVertexMake and HOColorMake, please? And the definition for any structures they're using?

It looks like you're needlessly recreating an HOColor structure for each vertex when they're all the same then copying that structure into an HOSpriteVertex, and copying an HOVertex structure into the same. Then copying all that into the HOSpriteVertex afterwards. That's a load of unneeded copying.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 06-09-2010, 02:57 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 15
SamBaylus is on a distinguished road
Default Sure!

I'll show ya what I got, but I changed it first. I think it's better now. Basically the demo is this: You touch the screen, and a colored box appears. You can do this 100 times.

This is the init for the game object being created.
Code:
-(id)init{
	if (self = [super init])
	{
		x = touchX;
		y = touchY;
		width = 32;
		height = 32;
		red = arc4random() %255;
		green = arc4random() %255;
		blue = arc4random() %255;
		v1 = HOSpriteVertexMake(HOVertexMake(x, y), HOColorMake(red, green, blue, 255));
		v2 = HOSpriteVertexMake(HOVertexMake(x + width, y), HOColorMake(red, green, blue, 255));
		v3 = HOSpriteVertexMake(HOVertexMake(x, y + height), HOColorMake(red, green, blue, 255));
		v4 = HOSpriteVertexMake(HOVertexMake(x + width, y + height), HOColorMake(red, green, blue, 255));
	}
It is then passed into an NSMutableArray, which cycles through each object's draw method.
Code:
-(void)draw{
	
	int vertexCount = 0;
	
	for (int i = 0; i < [gameObjectArray count]; i ++) 
	{
		HOGameObject *temp = [gameObjectArray objectAtIndex:i];
		vertexArray[vertexCount++] = temp->v1;
		vertexArray[vertexCount++] = temp->v2;
		vertexArray[vertexCount++] = temp->v3;
		
		vertexArray[vertexCount++] = temp->v3;
		vertexArray[vertexCount++] = temp->v4;
		vertexArray[vertexCount++] = temp->v2;
	}
	
	glVertexPointer(2, GL_FLOAT, sizeof(HOSpriteVertex), &vertexArray[0].vertex);
    glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(HOSpriteVertex), &vertexArray[0].color);
	
    glDrawArrays(GL_TRIANGLES, 0, [gameObjectArray count]*6);
}
So this now just takes the HOSpriteVertex from each object and plugs it into the large array.

...and since you asked....
Code:
typedef struct _HOVertex
{
	GLfloat x;
	GLfloat y;

} HOVertex;

static inline HOVertex HOVertexMake(GLfloat aX, GLfloat aY)
{
	HOVertex temp;
	temp.x = aX;
	temp.y = aY;
	return temp;
}

typedef struct _HOColor
{
	GLubyte red;
	GLubyte green;
	GLubyte blue;
	GLubyte alpha;

} HOColor;

static inline HOColor HOColorMake(GLubyte aRed, GLubyte aGreen, GLubyte aBlue, GLubyte aAlpha)
{
	HOColor temp;
	temp.red = aRed;
	temp.green = aGreen;
	temp.blue = aBlue;
	temp.alpha = aAlpha;
	return temp;
}

typedef struct _HOSpriteVertex
{
	HOVertex vertex;
	HOColor color;
} HOSpriteVertex;

static inline HOSpriteVertex HOSpriteVertexMake(HOVertex aVertex, HOColor aColor)
{
	HOSpriteVertex temp;
	temp.vertex = aVertex;
	temp.color = aColor;
	return temp;
}
For the record, I know HOVertex is just a CGPoint. I just did it this way to be consistent.

Thanks!
BTW, Mr.Jack, check your PMs!
SamBaylus is offline   Reply With Quote
Old 06-09-2010, 03:21 PM   #6 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Mr Jack is on a distinguished road
Default

Your new solution looks fine, you're no longer needlessly copying all over the place
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Reply

Bookmarks

Tags
calloc game engine opengl

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 404
11 members and 393 guests
7twenty7, ChrisYates, djohnson, Duncan C, gmarro, hussain1982, Kirkout, Retouchable, SLIC, walex, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:05 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0