Code for Concinnity

cocos2d-x with Xcode 4 from scratch (without template)

This is mainly some quick notes for myself. Getting cocos2d-x to work on iOS is surprisingly involved. Here we go:

Building cocos2d-x

First grab a copy of cocos2d-x

$ git clone

Now we need to build cocos2d-x. After some pondering about it, I conclude that the project is reasonably disorganized. The easiest way to grab a lib out of the mess is to compile its HelloWorld program:

    $ cd cocos2d-x/HelloWorld/ios
    $ xcodebuild -sdk iphonesimulator-4.3 -configuration Debug
    # ...
    # Now we have the .a file ready, let's find out where it is
    $ find . -iname '*.a'

Configuring the Xcode project

Now we need to configure our Xcode project to refer to the libcocos2d.a and the headers.

  1. Create a new Window-based Application
  2. Add these frameworks: OpenGLES.framework, QuartzCore.framework, libxml2.dylib
  3. In Build Settings, define the Preprocessor Macro TARGET_IPHONE_SIMULATOR
  4. Add Header Search Paths: cocos2d-x/cocos2dx/include cocos2d-x/cocos2dx/platform/ios cocos2d-x/cocos2dx/platform cocos2d-x/cocos2dx
  5. Finally Linker Flag -lcocos2d

Then we need to change our default project’s structure a bit. Since we’re creating our own window we will:

  1. Delete MainWindow.xib
  2. In *-Info.plist, remove the “main nib file” entry
  3. Change *AppDelegate.m to *

Let’s write some code!

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        static MainApplication sMainAppplication;
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        EAGLView * glView = [EAGLView viewWithFrame: [self.window bounds]pixelFormat:kEAGLColorFormatRGBA8 depthFormat:GL_DEPTH_COMPONENT16_OES preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0];
        self.window.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
        self.window.rootViewController.wantsFullScreenLayout = YES;
        self.window.rootViewController.view = glView;
        [self.window makeKeyAndVisible];
        // ...
// MainApplication.cpp
    bool MainApplication::initInstance()
        return true;
    bool MainApplication::applicationDidFinishLaunching()
        cocos2d::CCDirector * director = cocos2d::CCDirector::sharedDirector();
        director->setAnimationInterval(1.0 / 60.0);
        return true;
    void MainApplication::applicationDidEnterBackground()
    void MainApplication::applicationWillEnterForeground()

MainScene is your typical cocos2d::CCScene.

There we have it!

Published by kizzx2, on June 18th, 2011 at 1:51 am. Filled under: UncategorizedNo Comments

No comments yet.

Leave a Reply