Code for Concinnity

beautiful and elegant solutions


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

1
    $ git clone https://github.com/cocos2d/cocos2d-x.git

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:

1
2
3
4
5
6
    $ 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'
    ./build/Debug-iphonesimulator/libcocos2d.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 *AppDelegate.mm

Let’s write some code!

*AppDelegate.mm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    - (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];
        cocos2d::CCApplication::sharedApplication().run();

        // ...
    }

MainApplication.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    MainApplication::MainApplication()
    {
    }

    MainApplication::~MainApplication()
    {
       
    }

    bool MainApplication::initInstance()
    {
        return true;
    }

    bool MainApplication::applicationDidFinishLaunching()
    {
        cocos2d::CCDirector * director = cocos2d::CCDirector::sharedDirector();
        director->setOpenGLView(&cocos2d::CCEGLView::sharedOpenGLView());
       
        director->setAnimationInterval(1.0 / 60.0);
        director->runWithScene(MainScene::scene());
       
        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