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 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:
$ 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.
- Create a new Window-based Application
- Add these frameworks: OpenGLES.framework, QuartzCore.framework, libxml2.dylib
- In Build Settings, define the Preprocessor Macro
TARGET_IPHONE_SIMULATOR
- Add Header Search Paths:
cocos2d-x/cocos2dx/include
cocos2d-x/cocos2dx/platform/ios
cocos2d-x/cocos2dx/platform
cocos2d-x/cocos2dx
- 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:
- Delete
MainWindow.xib
- In
*-Info.plist
, remove the “main nib file” entry - Change
*AppDelegate.m
to*AppDelegate.mm
Let’s write some code!
// AppDelegate.mm: - (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 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
.