Hi
I would like to share with you solution that I came up with, to actually have a Hi-res App that is also working smoothly on iPhone 3G in lower resolution. It's rather tutorial for beginners.
Standard screen dimension iPhone has is 320x480 (480x320 landscape). New iPhone 4 has 640x960 (960x640).
To turn on Hi-Res graphics in Cocos2D you have to add following line in AppDelegate
[director setContentScaleFactor:2];
This actually change screen dimension to hi-res, and is also working on older devices simply scaling down the graphics. It would be ok, but performance loss is just unacceptable on iPhone 3G. In my application I got only 15-20 fps for just drawing 480 x 320 background and 2 buttons.
All you have to do, to actually use new hi-res retina display and also retain good performance on older devices is to do few things:
1. Set director in right mode(AppDelegate):
//set "float _scale" global variable using singleton, or some other way
if([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(@"scale")])
{
if ([[UIScreen mainScreen] scale] < 1.1){
NSLog(@"Standard Resolution Device"]);
[director setContentScaleFactor:1];
_scale=1;
}
if ([[UIScreen mainScreen] scale] > 1.9){
NSLog(@"High Resolution Device:");
[director setContentScaleFactor:2];
_scale=2;
}
} else{
//this means we run on iOS<4.0 and only low-res is valid
_scale = 1;
}
Now depending on device you have screen 320x480 (480x320) or 640x960 (960x480). To unify code scale width and height:
_size = [[CCDirector sharedDirector] winSize];
_size = CGSizeMake(_size.width/_scale, _size.height/_scale);
Now _size should return 320x480 (480x320) and we should work with these dimensions. All we have to do now is to scale every object we draw on screen and multiply position with _scale. Let's draw a sprite on position (55,103) for example:
CGpoint position = CGPointMake(55,103);
CCSprite *sprite = [CCSprite spriteWithFile:@"sprite.png"];
sprite.position = (55*_scale,103*_scale);
sprite.scale = _scale/2; //sprite should be prepared for hi-res
[self addChild: sprite z:10];
Using this method we should have displayed hires sprite on retina display and low res on older phones. This work 2-3 times faster than just inserting [director setContentScaleFactor:2] in AppDelegate.
More speed could be achieved if we had graphics in lowres and hires version and use proper one instead of scaling hires object.
Hope this helps:)
PS. This is actually a wrap up from other threads on this forum.