Permalink
Browse files

Added support for @2x textures

  • Loading branch information...
1 parent da3f26e commit 567e77c8707c45c056b12d4606ba0cc5f458829d @phoboslab committed Nov 12, 2012
@@ -282,6 +282,7 @@ - (EJTexture *)texture {
NSObject<EJDrawable> * drawable = (NSObject<EJDrawable> *)JSObjectGetPrivate((JSObjectRef)argv[0]);
EJTexture * image = drawable.texture;
+ float scale = image.contentScale;
short sx = 0, sy = 0, sw = 0, sh = 0;
float dx = 0, dy = 0, dw = sw, dh = sh;
@@ -290,8 +291,10 @@ - (EJTexture *)texture {
// drawImage(image, dx, dy)
dx = JSValueToNumberFast(ctx, argv[1]);
dy = JSValueToNumberFast(ctx, argv[2]);
- dw = sw = image.width;
- dh = sh = image.height;
+ sw = image.width;
+ sh = image.height;
+ dw = sw / scale;
+ dh = sh / scale;
}
else if( argc == 5 ) {
// drawImage(image, dx, dy, dw, dh)
@@ -304,10 +307,10 @@ - (EJTexture *)texture {
}
else if( argc >= 9 ) {
// drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)
- sx = JSValueToNumberFast(ctx, argv[1]);
- sy = JSValueToNumberFast(ctx, argv[2]);
- sw = JSValueToNumberFast(ctx, argv[3]);
- sh = JSValueToNumberFast(ctx, argv[4]);
+ sx = JSValueToNumberFast(ctx, argv[1]) * scale;
+ sy = JSValueToNumberFast(ctx, argv[2]) * scale;
+ sw = JSValueToNumberFast(ctx, argv[3]) * scale;
+ sh = JSValueToNumberFast(ctx, argv[4]) * scale;
dx = JSValueToNumberFast(ctx, argv[5]);
dy = JSValueToNumberFast(ctx, argv[6]);
@@ -79,11 +79,11 @@ - (void)dealloc {
}
EJ_BIND_GET(width, ctx ) {
- return JSValueMakeNumber( ctx, texture ? texture.width : 0);
+ return JSValueMakeNumber( ctx, texture ? (texture.width / texture.contentScale) : 0);
}
EJ_BIND_GET(height, ctx ) {
- return JSValueMakeNumber( ctx, texture ? texture.height : 0 );
+ return JSValueMakeNumber( ctx, texture ? (texture.height / texture.contentScale) : 0 );
}
EJ_BIND_GET(complete, ctx ) {
@@ -9,6 +9,7 @@
NSString * fullPath;
GLuint textureId;
GLenum format;
+ float contentScale;
}
- (id)initWithPath:(NSString *)path;
- (id)initWithPath:(NSString *)path context:(EAGLContext*)context;
@@ -29,6 +30,7 @@
+ (BOOL)smoothScaling;
+ (void)setSmoothScaling:(BOOL)smoothScaling;
+@property (readonly, nonatomic) float contentScale;
@property (readonly, nonatomic) GLuint textureId;
@property (readonly, nonatomic) short width, height, realWidth, realHeight;
@@ -17,13 +17,15 @@ + (void)setSmoothScaling:(BOOL)smoothScaling {
+@synthesize contentScale;
@synthesize textureId;
@synthesize width, height, realWidth, realHeight;
- (id)initWithPath:(NSString *)path {
// Load directly (blocking)
if( self = [super init] ) {
+ contentScale = 1;
fullPath = [path retain];
GLubyte * pixels = [self loadPixelsFromPath:path];
[self createTextureWithPixels:pixels format:GL_RGBA];
@@ -37,6 +39,7 @@ - (id)initWithPath:(NSString *)path context:(EAGLContext*)context {
// Load in a low-priority thread (non-blocking)
if( self = [super init] ) {
+ contentScale = 1;
fullPath = [path retain];
GLubyte * pixels = [self loadPixelsFromPath:path];
@@ -62,6 +65,7 @@ - (id)initWithWidth:(int)widthp height:(int)heightp format:(GLenum)formatp {
// Create an empty texture
if( self = [super init] ) {
+ contentScale = 1;
fullPath = [@"[Empty]" retain];
[self setWidth:widthp height:heightp];
[self createTextureWithPixels:NULL format:formatp];
@@ -78,6 +82,7 @@ - (id)initWithWidth:(int)widthp height:(int)heightp pixels:(GLubyte *)pixels {
// Creates a texture with the given pixels
if( self = [super init] ) {
+ contentScale = 1;
fullPath = [@"[From Pixels]" retain];
[self setWidth:widthp height:heightp];
@@ -160,6 +165,18 @@ - (void)updateTextureWithPixels:(GLubyte *)pixels atX:(int)x y:(int)y width:(int
}
- (GLubyte *)loadPixelsFromPath:(NSString *)path {
+ // Try @2x texture?
+ if( [UIScreen mainScreen].scale == 2 ) {
+ NSString * path2x = [[[path stringByDeletingPathExtension]
+ stringByAppendingString:@"@2x"]
+ stringByAppendingPathExtension:[path pathExtension]];
+
+ if( [[NSFileManager defaultManager] fileExistsAtPath:path2x] ) {
+ contentScale = 2;
+ path = path2x;
+ }
+ }
+
// All CGImage functions return pixels with premultiplied alpha and there's no
// way to opt-out - thanks Apple, awesome idea.
// So, for PNG images we use the lodepng library instead.

0 comments on commit 567e77c

Please sign in to comment.