Skip to main content

dealing with textureAtlas and spritekit all about preloading texture atlas and remove them when you not need them anymore

spritekit is a cool 2d framework designed by apple with support fast 2d graphics rendering one of the killer feature of sprite kit is texture atlas a

what is a texture atlas

https://developer.apple.com/library/ios/recipes/xcode_help-texture_atlas/_index.html

working with texture atlas in sprite kit is very easy.



example



#import "GameScene.h"

@implementation GameScene
{
    UIImage *defaultImg;
    int MCrow;
    int MCcoloum;
    float imgWidth;
    float imgHeight;
    NSArray *atlasArray;
    NSMutableArray *fetchedAtlas;
    NSDictionary *dict1;
    NSDictionary *dict2;
    NSDictionary *dict3;
    NSDictionary *dict4;
    NSDictionary *dict5;
    NSDictionary *dict6;
    NSDictionary *dict7;
    SKSpriteNode *node;

}
-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */

    MCrow=3;
    MCcoloum=3;


 /*
loading all texture atlas in memory after ten second


*/   
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self drawAllTextures];
    });
}
-(void)drawAllTextures
{
    
/*
all my texture atlas 

*/
atlasArray=@[@"yellowseahorse",@"stingray",@"tiger",@"shark",@"mathlab",@"zipline",@"hurdies"];

//and empty array thats holds a group of textureAtlas

    
fetchedAtlas=[[NSMutableArray alloc] init];
    
    
    for(int i=0;i<[atlasArray count];i++)
    {
        //add texture atlas to a NSMutableArray 
        [fetchedAtlas addObject:[self getAtlas:atlasArray[i]]];
    }
//preload texture atlas 
    [SKTextureAtlas preloadTextureAtlases:fetchedAtlas withCompletionHandler:^{
        
        //fetch all texture from textureAtlas and add them their respective dictionary 
        dict1=[self fetchTexturesFromAtlas:fetchedAtlas[0]];
        dict2=[self fetchTexturesFromAtlas:fetchedAtlas[1]];
        dict3=[self fetchTexturesFromAtlas:fetchedAtlas[2]];
        dict4=[self fetchTexturesFromAtlas:fetchedAtlas[3]];
        dict5=[self fetchTexturesFromAtlas:fetchedAtlas[4]];
        dict6=[self fetchTexturesFromAtlas:fetchedAtlas[5]];
        dict7=[self fetchTexturesFromAtlas:fetchedAtlas[6]];
        
    }];
}
-(NSMutableDictionary*)fetchTexturesFromAtlas:(SKTextureAtlas*)atlas
{
    NSMutableDictionary *SKTextureDictionary=[[NSMutableDictionary allocinit];
    for(int i=0;i<[atlas.textureNames count];i++)
    {
        SKTexture *texture=[self createTextureFromAtlas:atlas texture:atlas.textureNames[i]];
        NSArray *fullname=[atlas.textureNames[i] componentsSeparatedByString:@"."];
        [SKTextureDictionary setObject:texture forKey:fullname[0]];
    }
    return SKTextureDictionary;
}
-(SKTexture*)createTextureFromAtlas:(SKTextureAtlas *)atlas texture:(NSString*)textureName
{
    SKTexture *spriteTexture=[atlas textureNamed:textureName];
    return spriteTexture;
}

//fetch texture atlas from memory

-(SKTextureAtlas*)getAtlas:(NSString*)name
{
    return [SKTextureAtlas atlasNamed:name];
}

//
-(void)drawObjects
{
    for (NSString* key in dict1) {
        node=[SKSpriteNode spriteNodeWithTexture:[dict1 objectForKey:key]];
        node.position=CGPointMake(400, 400);
        [self addChild:node];
       
    }
    for (NSString* key in dict2) {
        node=[SKSpriteNode spriteNodeWithTexture:[dict2 objectForKey:key]];
        node.position=CGPointMake(300, 300);
        [self addChild:node];
        
    }
    for (NSString* key in dict3) {
        node=[SKSpriteNode spriteNodeWithTexture:[dict3 objectForKey:key]];
        node.position=CGPointMake(100, 100);
        [self addChild:node];
        
    }
}





-(void)removeAllAtlas
{
//remove all reference of texture atlas and all textures 
    dict1=nil;
    dict2=nil;
    dict3=nil;
    dict4=nil;
    dict5=nil;
    dict6=nil;
    dict7=nil;
    [fetchedAtlas removeAllObjects];
    [self removeAllChildren];
     fetchedAtlas=nil;
   
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
     [self drawObjects];
  }
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self removeAllAtlas];
    });
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    
}


@end




Comments

Popular posts from this blog

Creating a word Scramble game where you can drag and drop the word and create a complete word. (drag and drop word Scramble game).

Creating a word game using  phaser game engine which is a javascript based gaming engine  where u can drag and drop world like Word Scramble Game and complete a given word. your index.html file look like below < html > < head > < script src = "src/phaser.min.js" ></ script > < script src = "src/wordGame.js" ></ script > < script src = "src/main.js" ></ script > </ head > < body > </ body > </ html > 1.    <script src="src/phaser.min.js"></script> required to run the  phaser game engine. 2. rest of the two files deals with game logics      <script src="src/wordGame.js"></script>       <script src="src/main.js"></script> you can download whole project from https://github.com/manishchauhan/wordgame/tree/master just copy and paste the project in your local server and run ...

starting with three.js and react with a very simple example

 1. three.js is undoubtedly the best library to create interactive content for the web in 3d. link https://threejs.org/ a working sample of three with react can be found below https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js why choose threejs 1. lightweight  2.fast 3.big community  https://discourse.threejs.org/ 4. even you can use unity or unreal to publish html5 content but i don't think that would be acceptable in many cases. 5. open-source project. i created a running sample with React  https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js

A simple binary search tree with generic approach tree can store any type of data. (DFS and BFS)

 1. a binary search tree in c++ using a generic approach with both BFS and DFS approach    working tree    https://www.onlinegdb.com/edit/HkuQLdA-w code=> some properties of binary search tree-> 1. binary search tree is not always a balanced tree.  2. Inorder, traversing gives you sorted data.  3.  best is o(log n) and worst is O(n).  4. left node data is always less than root data and right node data always be greater than root data.  5. red-black tree STL map is a balanced binary search tree. *******************************************************************************/ #include <stdio.h> #include<iostream> #include<queue> using namespace std; template < typename T > class Node {   //can store any type of data private:   //genric data or store any type of data   T data;   //reference of left pointer   Node *left = nullptr;   //reference of right pointer   Node *rig...