Skip to content

Instantly share code, notes, and snippets.

@PaulSolt
Created January 4, 2014 16:20
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 PaulSolt/8256995 to your computer and use it in GitHub Desktop.
Save PaulSolt/8256995 to your computer and use it in GitHub Desktop.
A wrapper around Cocos2d so that you can use it with UIKit as a view.
//
// AECocos2D.h
// CocosUIKitTest
//
// Created by Paul Solt on 7/4/13.
// Copyright (c) 2014 Paul Solt. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface AECocos2D : NSObject
+ (AECocos2D *)shared;
- (void)setupDirector;
- (CCGLView *)currentGLView;
@end
//
// AECocos2D.m
// CocosUIKitTest
//
// Created by Paul Solt on 7/4/13.
// Copyright (c) 2014 Paul Solt. All rights reserved.
//
#import "AECocos2D.h"
@implementation AECocos2D {
CCGLView *_glView;
}
static AECocos2D *sharedInstance;
+ (AECocos2D *)shared;
{
if(!sharedInstance) {
sharedInstance = [[AECocos2D alloc] init];
sharedInstance->_glView = [CCGLView viewWithFrame:[[UIScreen mainScreen] bounds]
pixelFormat:kEAGLColorFormatRGBA8 //kEAGLColorFormatRGB565
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
[sharedInstance->_glView setMultipleTouchEnabled:YES];
[sharedInstance setupDirector];
[sharedInstance->_glView setFrame:[[UIScreen mainScreen] bounds]];
}
return sharedInstance;
}
- (void)setupDirector
{
CCDirectorIOS *director = (CCDirectorIOS *) [CCDirector sharedDirector];
[director setView:_glView];
[director enableRetinaDisplay:YES];
// director.wantsFullScreenLayout = YES; // Not on iOS 7
[director setDisplayStats:YES];
[director setAnimationInterval:1.0/60];
}
- (CCGLView *)currentGLView {
return _glView;
}
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Call this right here, before anything else. Need to set up the director ASAP
[AECocos2D shared];
// Other setup code
}
- (void)viewDidLoad
{
[super viewDidLoad];
// The 2 below statements made the game window appear in landscape orientation correctly
[[[AECocos2D shared] currentGLView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[[[AECocos2D shared] currentGLView] setFrame:self.view.frame];
[self.view insertSubview:[[AECocos2D shared] currentGLView] atIndex:0];
CCScene *scene = [GameWorldLayer scene];
self.gameWorldLayer = (GameWorldLayer *)[scene.children objectAtIndex:0];
[[CCDirector sharedDirector] runWithScene:scene];
self.gameWorldLayer.parentViewController = self;
[[CCDirector sharedDirector] startAnimation];
}
- (void)transitionToViewController {
//...
// Disable or enable interaction for your menus on top
[[AECocos2D shared] currentGLView].userInteractionEnabled = NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment