给 Cocos2d 增加对 4 英寸屏幕的支持 (iPhone 5, iPod touch 5 代)

【更新】Cocos2d V2.1 Beta4 更新了CCFileUtils,增加了对 iPhone 5的支持;iPhone 5的资源可以采用”-widehd”后缀,还可以预设目录的方式来组织资源(目前默认还是后缀)。我们终于不用再自己做一些 hack 的方法了,Release Note:https://github.com/cocos2d/cocos2d-iphone/wiki/cocos2d-v2.1-release-notes

概述

Cocos2d 通过给文件增加 -hd 后缀来支持 Retina 屏幕。目前 V2.1 Beta2 支持通过给 launch image 增加 -568h 后缀来支持 640×1136 分辨率的 4-Inch 屏幕,但是对于游戏中用到的普通图片还不支持自动加载 -568h 后缀的图片。

我们可以通过修改项目 libs 目录下的 ccType.h, Support/CCFileUtils.h, Support/CCFileUtils.m 几个文件使 Cocos2d 可以自动为 4-Inch 屏幕自动加载 -568h 后缀的图片。

修改过的这几个文件压缩包下载:Cocos2d-fourinch

也可以按照下面的步骤逐步修改一下:

修改 ccType.h 文件

打开ccType.h, 在 ccResolutionType 中增加 kCCResolutioniPhoneFourInchDisplay 。

[sourcecode language=”objc”]
//! ccResolutionType
typedef enum
{
//! Unknown resolution type
kCCResolutionUnknown,
#ifdef __CC_PLATFORM_IOS
//! iPhone resolution type
kCCResolutioniPhone,
//! RetinaDisplay resolution type
kCCResolutioniPhoneRetinaDisplay,
kCCResolutioniPhoneFourInchDisplay,
//! iPad resolution type
kCCResolutioniPad,
//! iPad Retina Display resolution type
kCCResolutioniPadRetinaDisplay,

#elif defined(__CC_PLATFORM_MAC)
//! Mac resolution type
kCCResolutionMac,

//! Mac RetinaDisplay resolution type
kCCResolutionMacRetinaDisplay,
#endif // platform

} ccResolutionType;
[/sourcecode]

修改 CCFileUtils.h 文件

  • 在 CCFileUtil.h 中,在 {} 变量声明中增加 NSString *iPhoneFourInchDisplaySuffix_; 作为 4-inch 的后缀

[sourcecode language=”objc”]
#ifdef __CC_PLATFORM_IOS
NSString *iPhoneRetinaDisplaySuffix_;
NSString *iPadSuffix_;
NSString *iPadRetinaDisplaySuffix_;

NSString *iPhoneFourInchDisplaySuffix_;
#elif defined(__CC_PLATFORM_MAC)
NSString *macSuffix_;
NSString *macRetinaDisplaySuffix_;
#endif // __CC_PLATFORM_MAC
[/sourcecode]

  • 同时在 CCFileUtils.h 中增加 NSString *iPhoneFourInchDisplaySuffix 的 property 声明:

[sourcecode language=”objc”]
@property (nonatomic,readwrite, copy, setter = setiPhoneFourInchDisplaySuffix:) NSString *iPhoneFourInchDisplaySuffix;
[/sourcecode]

  • 在 CCFileUtils.h 中增加一个判断给定 4inch 文件是否在方法:

[sourcecode language=”objc”]
-(BOOL) iPhoneFourInchFileExistsAtPath:(NSString*)filename;
[/sourcecode]

修改 CCFileUtils.m 文件

下面开始修改 CCFileUtils.m 文件:

  • 在第一个 enum 中增加对 4-inch 的支持:

[sourcecode language=”objc”]
#ifdef __CC_PLATFORM_IOS
enum {
kCCiPhone,
kCCiPhoneRetinaDisplay,
kCCiPad,
kCCiPadRetinaDisplay,
// 4-Inch 支持
kCCiPhoneFourInchDisplay,
};
#elif __CC_PLATFORM_MAC
enum {
kCCMac,
kCCMacRetinaDisplay,
};
#endif
[/sourcecode]

  • 增加对 iPhoneFourInchDisplaySuffix 的 synthesize:

[sourcecode language=”objc”]
#ifdef __CC_PLATFORM_IOS
@synthesize iPhoneRetinaDisplaySuffix = iPhoneRetinaDisplaySuffix_;
// 对 iPhoneFourInchDisplaySuffix 的 synthesize
@synthesize iPhoneFourInchDisplaySuffix = iPhoneFourInchDisplaySuffix_;
@synthesize iPadSuffix = iPadSuffix_;
@synthesize iPadRetinaDisplaySuffix = iPadRetinaDisplaySuffix_;
#elif defined(__CC_PLATFORM_MAC)
@synthesize macSuffix = macSuffix_;
@synthesize macRetinaDisplaySuffix = macRetinaDisplaySuffix_;
#endif // __CC_PLATFORM_IOS
[/sourcecode]

  • 修改 init 方法,增加对 iPhoneFourInchDisplaySuffix_ 的赋值:

[sourcecode language=”objc”]
#ifdef __CC_PLATFORM_IOS
iPhoneRetinaDisplaySuffix_ = @"-hd";
iPhoneFourInchDisplaySuffix_ = @"-568h";
iPadSuffix_ = @"-ipad";
iPadRetinaDisplaySuffix_ = @"-ipadhd";
#elif defined(__CC_PLATFORM_MAC)
macRetinaDisplaySuffix_ = @"-machd";
macSuffix_ = @"-mac";
#endif // __CC_PLATFORM_IOS
[/sourcecode]

  • 在 dealloc 中 release iPhoneFourInchDisplaySuffix_:

[sourcecode language=”objc”]
#ifdef __CC_PLATFORM_IOS
[iPhoneRetinaDisplaySuffix_ release];

[iPhoneFourInchDisplaySuffix_ release];

[iPadSuffix_ release];
[iPadRetinaDisplaySuffix_ release];

#elif defined(__CC_PLATFORM_MAC)
[macRetinaDisplaySuffix_ release];
[macSuffix_ release];

#endif // __CC_PLATFORM_MAC
[/sourcecode]

  • 修改 -(NSString*) fullPathFromRelativePath:(NSString*)relPath resolutionType:(ccResolutionType*)resolutionType 方法增加对 4-inch 的支持:

[sourcecode language=”objc”]

// iPhone HD ?
if( device == kCCiPhoneRetinaDisplay || (enableFallbackSuffixes_ && !ret) ) {
ret = [self getPath:relPath forSuffix:iPhoneRetinaDisplaySuffix_];
*resolutionType = kCCResolutioniPhoneRetinaDisplay;
}

// 4 Inch iPhone HD ?
if( device == kCCiPhoneFourInchDisplay || (enableFallbackSuffixes_ && !ret) ) {
ret = [self getPath:relPath forSuffix:iPhoneFourInchDisplaySuffix_];
*resolutionType = kCCResolutioniPhoneFourInchDisplay;
}

// If it is not Phone HD, or if the previous "getPath" failed, then use iPhone images.
if( device == kCCiPhone || !ret )
{
ret = [self getPath:relPath forSuffix:@""];
*resolutionType = kCCResolutioniPhone;
}

[/sourcecode]

  • 修改 -(NSInteger) runningDevice 方法,增加对 4-inch iPhone 的判断:如果高度为568则认为是 4-inch iPhone 或 iPod Touch 5 代:

[sourcecode language=”objc”]

else
{
if ([[UIScreen mainScreen] bounds].size.height == 568) {
ret = kCCiPhoneFourInchDisplay;
}
else if( CC_CONTENT_SCALE_FACTOR() == 2 )
ret = kCCiPhoneRetinaDisplay;
else
ret = kCCiPhone;
}

[/sourcecode]

  • 修改 -(NSString*) removeSuffixFromFile:(NSString*) path 方法,增加对 kCCiPhoneFourInchDisplay 的支持:

[sourcecode language=”objc”]

if( device == kCCiPhoneRetinaDisplay || (enableFallbackSuffixes_ && !ret) )
ret = [self removeSuffix:iPhoneRetinaDisplaySuffix_ fromPath:path];

if( device == kCCiPhoneFourInchDisplay || (enableFallbackSuffixes_ && !ret) )
ret = [self removeSuffix:iPhoneFourInchDisplaySuffix_ fromPath:path];

if( device == kCCiPhone || !ret )
ret = path;

[/sourcecode]

  • 最后,实现 -(BOOL) iPhoneFourInchFileExistsAtPath:(NSString*)path 方法

[sourcecode language=”objc”]
-(BOOL) iPhoneFourInchFileExistsAtPath:(NSString*)path
{
return [self fileExistsAtPath:path withSuffix:iPhoneFourInchDisplaySuffix_];
}
[/sourcecode]

修改过的这几个文件压缩包下载:Cocos2d-fourinch

回复