Last active
December 31, 2015 21:49
-
-
Save thecodemaiden/8049650 to your computer and use it in GitHub Desktop.
How to use a texture atlas from Zwoptex in your SpriteKit game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BPTextureCache.h | |
// BallPuzzle | |
// | |
// Created by Adeola Bannis on 11/2/13. | |
// Copyright (c) 2013 Adeola Bannis. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <SpriteKit/SpriteKit.h> | |
@interface BPTextureCache : NSObject | |
- (void)addTextureFromPlist:(NSString *)filename; | |
- (SKSpriteNode *)spriteWithCacheName:(NSString *)cacheName; | |
+ (id)sharedInstance; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BPTextureCache.m | |
// BallPuzzle | |
// | |
// Created by Adeola Bannis on 11/2/13. | |
// Copyright (c) 2013 Adeola Bannis. All rights reserved. | |
// | |
#import "BPTextureCache.h" | |
@interface BPTextureCache () | |
{ | |
NSMutableArray *_allTextures; | |
} | |
@end | |
static BPTextureCache *__shared; | |
@implementation BPTextureCache | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
_allTextures = [[NSMutableArray alloc] init]; | |
} | |
return self; | |
} | |
+ (id)sharedInstance | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
__shared = [[BPTextureCache alloc] init]; | |
}); | |
return __shared; | |
} | |
- (NSString *)plistPathForBaseName:(NSString *)baseName | |
{ | |
NSString *ipadSuffix = @""; | |
NSString *hdSuffix = @""; | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { | |
ipadSuffix = @"-ipad"; | |
// ipadSuffix = @"~ipad"; // use this if you named your plists following Apple's device suffixes | |
} | |
if ([[UIScreen mainScreen] scale] > 1.0) | |
hdSuffix = @"-hd"; | |
//hdSuffix = @"@2x"; | |
NSString *fileName = [NSString stringWithFormat:@"%@%@%@", baseName, ipadSuffix, hdSuffix]; | |
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]; | |
if (!path && [ipadSuffix length]) { | |
fileName = [NSString stringWithFormat:@"%@%@", baseName, hdSuffix]; | |
path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]; | |
} | |
if (!path && [hdSuffix length]) { | |
fileName = [NSString stringWithFormat:@"%@%@", baseName, ipadSuffix]; | |
path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]; | |
} | |
if (!path) { | |
path = [[NSBundle mainBundle] pathForResource:baseName ofType:@"plist"]; | |
} | |
return path; | |
} | |
- (void)addTextureFromPlist:(NSString *)filename | |
{ | |
NSString *fileName = [filename componentsSeparatedByString:@"."][0]; | |
NSString *plistPath = [self plistPathForBaseName:fileName]; | |
NSDictionary *textureInfo = [NSDictionary dictionaryWithContentsOfFile:plistPath]; | |
if (textureInfo) { | |
NSString *imageName = [textureInfo[@"metadata"][@"textureFileName"] stringByDeletingPathExtension]; | |
NSData *imageData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]]; | |
UIImage *texImage = [UIImage imageWithData:imageData scale:[[UIScreen mainScreen] scale]]; | |
SKTexture *bulkTexture = [SKTexture textureWithImage:texImage]; | |
NSDictionary *info = @{@"texture":bulkTexture, @"frames":textureInfo[@"frames"]}; | |
[_allTextures addObject:info]; | |
} else { | |
NSLog(@"Could not load resource %@", fileName); | |
} | |
} | |
- (SKSpriteNode *)spriteWithCacheName:(NSString *)cacheName | |
{ | |
// can be faster but w/e | |
SKTexture *foundTexture = nil; | |
NSString *frameRectString = nil; | |
for (NSDictionary *i in _allTextures) { | |
NSDictionary *frameNames = i[@"frames"]; | |
if ([[frameNames allKeys] containsObject:cacheName]) { | |
foundTexture = i[@"texture"]; | |
frameRectString = frameNames[cacheName][@"textureRect"]; | |
break; | |
} | |
} | |
SKSpriteNode *sprite = nil; | |
if (foundTexture && frameRectString) { | |
CGRect sampleRect = CGRectFromString(frameRectString); | |
CGRect textureRect = sampleRect; | |
// adjust the rect to SpriteKit's [0.0 - 1.0] range, and flip Y | |
// now you can reuse your Cocos2D Zwoptex textures! | |
textureRect.size.width = textureRect.size.width/foundTexture.size.width; | |
textureRect.size.height = textureRect.size.height/foundTexture.size.height; | |
textureRect.origin.x = (textureRect.origin.x/foundTexture.size.width); | |
textureRect.origin.y = 1 - (textureRect.origin.y/foundTexture.size.height) - textureRect.size.height; | |
// scale this down.... dunno why I need to do this but... | |
CGFloat factor = 1.0/[[UIScreen mainScreen] scale]; | |
sampleRect = CGRectApplyAffineTransform(sampleRect, CGAffineTransformMakeScale(factor, factor)); | |
SKTexture *subTex = [SKTexture textureWithRect:textureRect inTexture:foundTexture]; | |
sprite = [SKSpriteNode spriteNodeWithTexture:subTex size:sampleRect.size]; | |
} | |
return sprite; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment