Skip to content

Instantly share code, notes, and snippets.

@mkhl
mkhl / DebugLog.h
Created December 8, 2008 18:14
Objective-C Logging
// Source: http://blog.mbcharbonneau.com/post/56581688/better-logging-in-objective-c
#define DebugLog(format, ...) NSLog(@"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(format), ##__VA_ARGS__])
@mkhl
mkhl / autoscoped.h
Created December 8, 2008 18:27
Objective-C macros
// GCC Attribute for autoscoped Obj-C objects
// Source: http://www.cocoabuilder.com/archive/message/cocoa/2009/3/13/232287
#define autoscoped __attribute__((cleanup(releaseObject)))
static inline void releaseObject(id *object)
{
[*object release];
}
//
// NSObject+BlockObservation.h
// Version 1.0
//
// Andy Matuschak
// andy@andymatuschak.org
// Public domain because I love you. Let me know how you use it.
//
#import <Cocoa/Cocoa.h>
@solidline
solidline / detect_cocos2d.m
Created September 10, 2009 22:44
Simple touch delegation method to find if a object has been hit
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
CGRect myRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.contentSize.width, sprite.contentSize.height);
if(CGRectContainsPoint(myRect, location)) {
// particularSprite touched
return kEventHandled;
glEnable(GL_SCISSOR_TEST);
// Some region to clip to
CGRect rect = CGRect(100, 100, 200, 200);
CGRect frame = [[[CCDirector sharedDirector] openGLView] frame];
// Adjust for position
CGPoint worldPos = [self convertToWorldSpaceAR:position_];
- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{
CGFloat angleInRadians = angle * (M_PI / 180);
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect imgRect = CGRectMake(0, 0, width, height);
CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
@kernel1983
kernel1983 / gist:646532
Created October 26, 2010 08:24
create looping background in cocos2D
-(id) init
{
if( (self=[super init] )) {
CGSize size = [[CCDirector sharedDirector] winSize];
CCTexture2D *bg = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
CCSprite *staticBackground0 = [CCSprite spriteWithTexture:bg];
staticBackground0.position = ccp(-size.width/2,size.height/2);
CCSprite *staticBackground1 = [CCSprite spriteWithTexture:bg];
staticBackground1.position = ccp(size.width/2,size.height/2);
@ecpplus
ecpplus / gist:713131
Created November 24, 2010 04:51
Cocos2d(0.99.4) create animation by CCSpriteSheet
CCSpriteSheet *animationSheet = [CCSpriteSheet spriteSheetWithFile:@"animation_sheet.png"];
[self addChild:animationSheet];
CCSprite *animationSprite = [CCSprite spriteWithTexture:animationSheet.texture
rect:CGRectMake(0, 0, 256, 290)];
animationSprite.position = ccp(self.contentSize.width / 2, self.contentSize.height / 2);
[animationSheet addChild:animationSprite];
CCAnimation *animation = [CCAnimation animationWithName:@"an_animation" delay:0.04f];
@rummelonp
rummelonp / NSObject+Swizzle.h
Created January 21, 2011 02:48
Objective-Cでメソッド定義を差し替えるメソッド
@interface NSObject(Swizzle)
+ (void)swizzleMethod:(SEL)orig_sel withMethod:(SEL)alt_sel;
@end
@rummelonp
rummelonp / SomeController.m
Created January 23, 2011 04:12
Objective-Cのクロージャ
// Make button block.
UIBarButtonItem* (^buttonWithTitle)(NSString*, SEL) = ^(NSString* title, SEL action)
{
UIBarButtonItem* button = [UIBarButtonItem alloc];
[button initWithTitle:title
style:UIBarButtonItemStyleBordered
target:self
action:action];
[button setWidth:65.0f];
[button autorelease];