Skip to content

Instantly share code, notes, and snippets.

@pontusarmini
Last active November 10, 2015 04:39
Show Gist options
  • Save pontusarmini/358306596cffc49174de to your computer and use it in GitHub Desktop.
Save pontusarmini/358306596cffc49174de to your computer and use it in GitHub Desktop.
Cocos2d Objective-C to Swift transition snippets
/*
Setting up custom texture parameters:
Firstly, add this to Bridging-Header.h-file: #import "libs/cocos2d-iphone/cocos2d/CCTexture_Private.h"
The ccTexParams struct is set up with GLuint's. The texParams needs to be a 'var' and not a 'let' constant.
*/
var texture = ...
var texParams = ccTexParams(minFilter: GLuint(GL_LINEAR), magFilter: GLuint(GL_LINEAR), wrapS: GLuint(GL_REPEAT), wrapT: GLuint(GL_CLAMP_TO_EDGE))
texture.setTexParameters(&texParams)
/*
Setting up a custom CCBlendMode:
The [CCBlendMode blendModeWithOptions:] constructor takes a NSDictionary as it's argument. In Swift that'll be a [NSObject:AnyObject].
The GL constants (GL_DST_ALPHA etc) are Int32's which will not automatically bridge to NSNumber. Wrapping them with an Int()
(as Int's are automatically bridged) or NSNumber(int:) does the trick
*/
sprite.blendMode = CCBlendMode(options: [CCBlendFuncSrcColor: Int(GL_DST_ALPHA), CCBlendFuncDstColor:Int(GL_ONE_MINUS_SRC_ALPHA)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment