Thanks for the suggestion. Using fastDirector increased the framerate by about 10fps, but its still slow (running at about 40-50fps on average. I've narrowed down the problem code to the following loop that is plotting stars for my starfield. When I have a total of 256 stars in the array, there's slowdown. If I reduce that number, its fine, running at a consistent 60fps: -
update function (called from schedule/tick):
// set rotation values (sinA, cosA, etc)
...
...
...
// update each star
NSEnumerator *enumerator = [stars objectEnumerator];
Star *star;
while ( star = [enumerator nextObject] ){
// move star closer
star.universeZ -= star.velocityZ * starVelocity;
if( star.universeZ < STARFIELD_FIELD_MINZ ){
[star resetRandom:NO];
}
// rotate Y
star.rotationX = star.universeX * cosA + star.universeZ * sinA;
star.rotationZ = star.universeZ * cosA - star.universeX * sinA;
// rotate X
star.rotationY = star.universeY * cosB + star.rotationZ * sinB;
star.rotationZ = star.rotationZ * cosB - star.universeY * sinB;
tempRotationX = star.rotationX * cosC + star.rotationY * sinC;
star.rotationY = star.rotationY * cosC - star.rotationX * sinC;
star.rotationX = tempRotationX;
// calculate screen coordinates
star.screenX = (CGFloat)screenCentreX + star.rotationX / star.rotationZ * screenZoom;
star.screenY = (CGFloat)screenCentreY + star.rotationY / star.rotationZ * screenZoom;
star.brightness = 180 - (180 * (star.universeZ / STARFIELD_FIELD_MAXZ));
}
draw function:
// draw the starfield
NSMutableArray *stars = starfield.stars;
NSEnumerator *enumerator = [stars objectEnumerator];
Star *star;
while ( star = [enumerator nextObject] ){
if( star.rotationZ > 1.0f && star.rotationZ < 640.0f ){
if( (star.screenX > 2 && star.screenX < starfield.screenWidth + self.position.x) &&
(star.screenY > 2 && star.screenY < (starfield.screenHeight + self.position.y)) ){
glPointSize(1);
glColor4ub(star.brightness,star.brightness,star.brightness,255);
drawPoint( ccp(star.screenX, star.screenY) );
}
}
}
Surely running this code on 256 stars is not gonna cause such a slowdown... or can anyone see any obvious mistakes in this code? I'm new to objective C so its possible I'm doing something incorrectly.
Thanks again