Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Texture: Added resolutionType.

  • Loading branch information...
commit 138f6ee492cd2a9350ae913697bd180810d90af4 1 parent 9a4f78e
@ricardoquesada ricardoquesada authored
View
18 cocos2d/CCTexture2D.h
@@ -123,7 +123,10 @@ typedef enum {
maxT_;
BOOL hasPremultipliedAlpha_;
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
ccResolutionType resolutionType_;
+#endif
+
}
/** Intializes with a texture2d with data */
- (id) initWithData:(const void*)data pixelFormat:(CCTexture2DPixelFormat)pixelFormat pixelsWide:(NSUInteger)width pixelsHigh:(NSUInteger)height contentSize:(CGSize)size;
@@ -152,14 +155,18 @@ typedef enum {
/** whether or not the texture has their Alpha premultiplied */
@property(nonatomic,readonly) BOOL hasPremultipliedAlpha;
-/** resolution type. Is it a RetinaDisplay texture, an iPad texture ?.
+/** returns the content size of the texture in points */
+-(CGSize) contentSize;
+
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+/** Returns the resolution type of the texture.
+ Is it a RetinaDisplay texture, an iPad texture or an standard texture ?
Only valid on iOS. Not valid on OS X.
@since v1.1
*/
-@property(nonatomic, readwrite) ccResolutionType resolutionType;
+-(ccResolutionType) resolutionType;
+#endif
-/** returns the content size of the texture in points */
--(CGSize) contentSize;
@end
/**
@@ -180,7 +187,8 @@ Note that RGBA type textures will have their alpha premultiplied - use the blend
@interface CCTexture2D (Image)
/** Initializes a texture from a UIImage object */
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
-- (id) initWithImage:(UIImage *)uiImage;
+- (id) initWithImage:(UIImage *)uiImage resolutionType:(ccResolutionType)resolution;
+- (id) initWithImage:(UIImage *)uiImage DEPRECATED_ATTRIBUTE;
#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
- (id) initWithImage:(CGImageRef)cgImage;
#endif
View
47 cocos2d/CCTexture2D.m
@@ -79,8 +79,10 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
#import "ccConfig.h"
#import "ccMacros.h"
#import "CCConfiguration.h"
-#import "Support/ccUtils.h"
#import "CCTexturePVR.h"
+#import "Support/ccUtils.h"
+#import "Support/CCFileUtils.h"
+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && CC_FONT_LABEL_SUPPORT
// FontLabel support
@@ -111,7 +113,6 @@ @implementation CCTexture2D
@synthesize contentSizeInPixels = size_, pixelFormat = format_, pixelsWide = width_, pixelsHigh = height_, name = name_, maxS = maxS_, maxT = maxT_;
@synthesize hasPremultipliedAlpha = hasPremultipliedAlpha_;
-@synthesize resolutionType = resolutionType_;
- (id) initWithData:(const void*)data pixelFormat:(CCTexture2DPixelFormat)pixelFormat pixelsWide:(NSUInteger)width pixelsHigh:(NSUInteger)height contentSize:(CGSize)size
{
@@ -157,8 +158,10 @@ - (id) initWithData:(const void*)data pixelFormat:(CCTexture2DPixelFormat)pixelF
maxT_ = size.height / (float)height;
hasPremultipliedAlpha_ = NO;
-
+
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
resolutionType_ = kCCResolutionUnknown;
+#endif
}
return self;
}
@@ -197,6 +200,14 @@ -(CGSize) contentSize
return ret;
}
+
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+-(ccResolutionType) resolutionType
+{
+ return resolutionType_;
+}
+#endif
+
@end
#pragma mark -
@@ -204,7 +215,16 @@ -(CGSize) contentSize
@implementation CCTexture2D (Image)
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+// XXX deprecated. To be removed in 2.0
- (id) initWithImage:(UIImage *)uiImage
+{
+ return [self initWithImage:uiImage resolutionType:kCCResolutionUnknown];
+}
+#endif
+
+
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+- (id) initWithImage:(UIImage *)uiImage resolutionType:(ccResolutionType)resolution
#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
- (id) initWithImage:(CGImageRef)CGImage
#endif
@@ -364,6 +384,10 @@ - (id) initWithImage:(CGImageRef)CGImage
CGContextRelease(context);
[self releaseData:data];
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+ resolutionType_ = resolution;
+#endif
+
return self;
}
@end
@@ -637,10 +661,18 @@ -(id) initWithPVRTCData: (const void*)data level:(int)level bpp:(int)bpp hasAlph
}
#endif // __IPHONE_OS_VERSION_MAX_ALLOWED
--(id) initWithPVRFile: (NSString*) file
+-(id) initWithPVRFile: (NSString*) relPath
{
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+ ccResolutionType resolution;
+ NSString *fullpath = [CCFileUtils fullPathFromRelativePath:relPath resolutionType:&resolution];
+
+#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
+ NSString *fullpath = [CCFileUtils fullPathFromRelativePath:relPath];
+#endif
+
if( (self = [super init]) ) {
- CCTexturePVR *pvr = [[CCTexturePVR alloc] initWithContentsOfFile:file];
+ CCTexturePVR *pvr = [[CCTexturePVR alloc] initWithContentsOfFile:fullpath];
if( pvr ) {
pvr.retainName = YES; // don't dealloc texture on release
@@ -658,10 +690,13 @@ -(id) initWithPVRFile: (NSString*) file
[self setAntiAliasTexParameters];
} else {
- CCLOG(@"cocos2d: Couldn't load PVR image: %@", file);
+ CCLOG(@"cocos2d: Couldn't load PVR image: %@", relPath);
[self release];
return nil;
}
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+ resolutionType_ = resolution;
+#endif
}
return self;
}
View
2  cocos2d/CCTextureCache.h
@@ -123,6 +123,8 @@
* hasAlpha: whether or not the image contains alpha channel
*
* IMPORTANT: This method is only defined on iOS. It is not supported on the Mac version.
+ *
+ * @deprecated Will be removed in 2.0. Use addPVRImage instead.
*/
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
-(CCTexture2D*) addPVRTCImage:(NSString*)fileimage bpp:(int)bpp hasAlpha:(BOOL)alpha width:(int)w;
View
26 cocos2d/CCTextureCache.m
@@ -35,6 +35,7 @@
#import "Support/CCFileUtils.h"
#import "CCDirector.h"
#import "ccConfig.h"
+#import "ccTypes.h"
// needed for CCCallFuncO in Mac-display_link version
#import "CCActionManager.h"
@@ -218,7 +219,9 @@ -(void) addImageAsync: (NSString*)path target:(id)target selector:(SEL)selector
CCTexture2D * tex;
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
path = [CCFileUtils removeSuffixFromFile:path];
+#endif
if( (tex=[textures_ objectForKey: path] ) ) {
[target performSelector:selector withObject:tex];
@@ -247,7 +250,9 @@ -(CCTexture2D*) addImage: (NSString*) path
[dictLock_ lock];
// remove possible -HD suffix to prevent caching the same image twice (issue #1040)
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
path = [CCFileUtils removeSuffixFromFile: path];
+#endif
tex=[textures_ objectForKey: path];
@@ -268,11 +273,12 @@ -(CCTexture2D*) addImage: (NSString*) path
) {
// convert jpg to png before loading the texture
- NSString *fullpath = [CCFileUtils fullPathFromRelativePath: path ];
+ ccResolutionType resolution;
+ NSString *fullpath = [CCFileUtils fullPathFromRelativePath: path resolutionType:&resolution];
UIImage *jpg = [[UIImage alloc] initWithContentsOfFile:fullpath];
UIImage *png = [[UIImage alloc] initWithData:UIImagePNGRepresentation(jpg)];
- tex = [ [CCTexture2D alloc] initWithImage: png ];
+ tex = [ [CCTexture2D alloc] initWithImage:png resolutionType:resolution];
[png release];
[jpg release];
@@ -287,11 +293,11 @@ -(CCTexture2D*) addImage: (NSString*) path
else {
- // prevents overloading the autorelease pool
- NSString *fullpath = [CCFileUtils fullPathFromRelativePath: path ];
+ ccResolutionType resolution;
+ NSString *fullpath = [CCFileUtils fullPathFromRelativePath:path resolutionType:&resolution];
UIImage *image = [ [UIImage alloc] initWithContentsOfFile: fullpath ];
- tex = [ [CCTexture2D alloc] initWithImage: image ];
+ tex = [ [CCTexture2D alloc] initWithImage:image resolutionType:resolution];
[image release];
if( tex )
@@ -347,7 +353,7 @@ -(CCTexture2D*) addCGImage: (CGImageRef) imageref forKey: (NSString *)key
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
// prevents overloading the autorelease pool
UIImage *image = [[UIImage alloc] initWithCGImage:imageref];
- tex = [[CCTexture2D alloc] initWithImage: image];
+ tex = [[CCTexture2D alloc] initWithImage:image resolutionType:kCCResolutionUnknown];
[image release];
#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
@@ -412,6 +418,7 @@ - (CCTexture2D *)textureForKey:(NSString *)key
@implementation CCTextureCache (PVRSupport)
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+// XXX deprecated
-(CCTexture2D*) addPVRTCImage:(NSString*)path bpp:(int)bpp hasAlpha:(BOOL)alpha width:(int)w
{
NSAssert(path != nil, @"TextureCache: fileimage MUST not be nill");
@@ -449,16 +456,15 @@ -(CCTexture2D*) addPVRImage:(NSString*)path
CCTexture2D * tex;
// remove possible -HD suffix to prevent caching the same image twice (issue #1040)
+#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
path = [CCFileUtils removeSuffixFromFile: path];
+#endif
if( (tex=[textures_ objectForKey: path] ) ) {
return tex;
}
- // Split up directory and filename
- NSString *fullpath = [CCFileUtils fullPathFromRelativePath:path];
-
- tex = [[CCTexture2D alloc] initWithPVRFile: fullpath];
+ tex = [[CCTexture2D alloc] initWithPVRFile: path];
if( tex )
[textures_ setObject: tex forKey:path];
else
View
28 cocos2d/Support/CCFileUtils.h
@@ -26,7 +26,7 @@
#import <Foundation/Foundation.h>
-
+#import "../ccTypes.h"
/** Helper class to handle file operations */
@interface CCFileUtils : NSObject
@@ -35,10 +35,13 @@
/** Returns the fullpath of an filename.
- If this method is when Retina Display is enabled, then the
- Retina Display suffix will be appended to the file (See ccConfig.h).
+ If in RetinaDisplay mode, and a RetinaDisplay file is found, it will return that path.
+ If in iPad mode, and an iPad file is found, it will return that path.
- If the Retina Display image doesn't exist, then it will return the "non-Retina Display" image
+ Examples:
+
+ * In iPad mode: "image.png" -> "/full/path/image-ipad.png" (in case the -ipad file exists)
+ * In RetinaDisplay mode: "image.png" -> "/full/path/image-hd.png" (in case the -hd file exists)
*/
+(NSString*) fullPathFromRelativePath:(NSString*) relPath;
@@ -46,6 +49,23 @@
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+/** Returns the fullpath of an filename including the resolution of the image.
+
+ If in RetinaDisplay mode, and a RetinaDisplay file is found, it will return that path.
+ If in iPad mode, and an iPad file is found, it will return that path.
+
+ Examples:
+
+ * In iPad mode: "image.png" -> "/full/path/image-ipad.png" (in case the -ipad file exists)
+ * In RetinaDisplay mode: "image.png" -> "/full/path/image-hd.png" (in case the -hd file exists)
+
+ If an iPad file is found, it will set resolution type to kCCResolutioniPad
+ If a RetinaDisplay file is found, it will set resolution type to kCCResolutionRetinaDisplay
+
+ */
++(NSString*) fullPathFromRelativePath:(NSString*)relPath resolutionType:(ccResolutionType*)resolutionType;
+
+
/** removes the suffix from a path
* On RetinaDisplay it will remove the -hd suffix
* On iPad it will remove the -ipad suffix
View
39 cocos2d/Support/CCFileUtils.m
@@ -30,6 +30,7 @@
#import "../CCConfiguration.h"
#import "../ccMacros.h"
#import "../ccConfig.h"
+#import "../ccTypes.h"
static NSFileManager *__localFileManager=nil;
@@ -128,10 +129,10 @@ +(NSString*) getPath:(NSString*)path forSuffix:(NSString*)suffix
return nil;
}
-+(NSString*) fullPathFromRelativePath:(NSString*) relPath
++(NSString*) fullPathFromRelativePath:(NSString*)relPath resolutionType:(ccResolutionType*)resolutionType
{
NSAssert(relPath != nil, @"CCFileUtils: Invalid path");
-
+
NSString *fullpath = nil;
// only if it is not an absolute path
@@ -140,36 +141,50 @@ +(NSString*) fullPathFromRelativePath:(NSString*) relPath
if (fullpath == nil)
fullpath = relPath;
-
+
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
NSString *ret = nil;
-
+
// Retina Display ?
- if( CC_CONTENT_SCALE_FACTOR() == 2 )
+ if( CC_CONTENT_SCALE_FACTOR() == 2 ) {
ret = [self getPath:fullpath forSuffix:__suffixRetinaDisplay];
-
+ *resolutionType = kCCResolutionRetinaDisplay;
+ }
+
// iPad ?
- else if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
+ else if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
ret = [self getPath:fullpath forSuffix:__suffixiPad];
-
+ *resolutionType = kCCResolutioniPad;
+ }
+
// It should be an iPhone in non Retina Display mode. So, do nothing
else
- ;
+ *resolutionType = kCCResolutionStandard;
- if( ! ret )
+ if( ! ret ) {
+ *resolutionType = kCCResolutionStandard;
ret = fullpath;
+ }
return ret;
-
+
#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
+ *resolutionType = kCCResolutionStandard;
+
return fullpath;
-
+
#endif // __MAC_OS_X_VERSION_MAX_ALLOWED
}
++(NSString*) fullPathFromRelativePath:(NSString*) relPath
+{
+ ccResolutionType ignore;
+ return [self fullPathFromRelativePath:relPath resolutionType:&ignore];
+}
+
#pragma mark CCFileUtils - Suffix (iOS only)
View
4 cocos2d/ccTypes.h
@@ -284,9 +284,13 @@ typedef struct _ccBlendFunc
//! ccResolutionType
typedef enum
{
+ //! Unknonw resolution type
kCCResolutionUnknown,
+ //! standard (iphone) resolution type
kCCResolutionStandard,
+ //! RetinaDisplay resolution type
kCCResolutionRetinaDisplay,
+ //! iPad resolution type
kCCResolutioniPad,
} ccResolutionType;
Please sign in to comment.
Something went wrong with that request. Please try again.