Skip to content

Instantly share code, notes, and snippets.

@lephukhanhhuy
Created July 1, 2013 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lephukhanhhuy/5899977 to your computer and use it in GitHub Desktop.
Save lephukhanhhuy/5899977 to your computer and use it in GitHub Desktop.
//
// ViewController.m
// UIKitParticleDesigner
//
// Created by TGM on 6/28/13.
// Copyright (c) 2013 Silicon Straits Saigon. All rights reserved.
//
#import "ViewController.h"
#import "ParticleLayer.h"
#import "cocos2d.h"
#define kTimeLifeParticle 3.0 // Change for your particle
@interface ViewController ()
{
ParticleLayer* particleLayer;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Create cocos2d view and ready to show particle
[self setupCocos2d];
[self hideCocos2dView];
}
- (IBAction)fireLeftSelected:(id)sender
{
UIButton* btn = (UIButton*) sender;
[self addParticleWithPlist:@"smokeleft.plist" atPoint:CGPointMake(btn.frame.origin.x, btn.frame.origin.y +
btn.frame.size.height/2)];
}
- (IBAction)fireRightSelected:(id)sender
{
UIButton* btn = (UIButton*) sender;
[self addParticleWithPlist:@"smokeright.plist" atPoint:CGPointMake(btn.frame.origin.x + btn.frame.size.width,
btn.frame.origin.y + btn.frame.size.height/2)];
}
#pragma mark - Cocos2d helper
// Use to capture image of the view and set background of cocos2d layer
- (UIImage*)imageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (void) endParticle
{
[self hideCocos2dView];
[particleLayer endEffect];
}
- (void) addParticleWithPlist:(NSString*) fileName atPoint:(CGPoint) p
{
CGPoint cocos2dPoint = CGPointMake(p.x, self.view.frame.size.height - p.y);
if (particleLayer != nil)
{
// Capture self.view
UIImage* image = [self imageFromView:self.view];
if ([[CCTextureCache sharedTextureCache] textureForKey:kBackgroundNameCache] == nil)
{
NSLog(@"0");
[[CCTextureCache sharedTextureCache] addCGImage:image.CGImage forKey:kBackgroundNameCache];
}
else
{
NSLog(@"1");
}
[self showCocos2dView];
[particleLayer addParticleWithPlist:fileName atPoint:cocos2dPoint];
[self performSelector:@selector(endParticle) withObject:nil afterDelay:kTimeLifeParticle];
}
else
{
NSLog(@"Check your setup cocos2d");
}
}
- (void) setupCocos2d
{
CCGLView *glView = [CCGLView viewWithFrame:self.view.bounds
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0
];
// NSLog(@"self.view %@", NSStringFromCGRect(self.view.bounds));
// NSLog(@"glView %@", NSStringFromCGRect(glView.frame));
glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
glView.userInteractionEnabled = NO;
[self.view addSubview:glView];
CCDirector* director_ = [CCDirector sharedDirector];
[director_ setProjection:kCCDirectorProjection2D];
[director_ enableRetinaDisplay:YES];
[director_ setView:glView];
CCScene *scene = [CCScene node];
particleLayer = [[ParticleLayer alloc] init];;
[scene addChild:particleLayer];
[director_ runWithScene:scene];
}
- (void) hideCocos2dView
{
[self.view sendSubviewToBack:[CCDirector sharedDirector].view];
}
- (void) showCocos2dView
{
[self.view bringSubviewToFront:[CCDirector sharedDirector].view];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment