views:

919

answers:

2

You often hear that C++ is preferable to Objective-C for games, especially in a resource-constrained environment like the iPhone. (I know you still need some Objective-C to initially talk to iPhone services.) Yet, the 2D game engine of choice these days seems to be Cocos2d, which is Objective-C.

I understand that what Apple calls "Objective-C++" allows you to mix C++ and Objective-C classes in a single file, but you can't mix and match the languages' constructs within the same class or function.

So, is it sensible/possible to use Cocos2d for a C++ game? Do you have to write a lot of "glue" code? I'd like to avoid some of the heavy lifting that a direct OpenGL-ES approach would require.

+5  A: 

You shouldn't have to write any glue code since Objective-C++ lets you mix C++ and Objective-C code pretty freely. However, I would recommend writing it in Objective-C and then profiling if you need to optimize it and rewrite those parts in C or C++ rather than starting out with C++. Objective-C is not a slow language and there is no reason to put in all the extra effort to use C++ when it might be fast enough.

Amuck
+1  A: 

I'm currently prototyping a game with Cocos2. I'm writing the game logic in C++ with Chipmunk and then using Cocos to implement the view layer. You can indeed mix C++ and Objective-C freely in the same class, function and line of code. I'm sure there are limits, like you probably can't mix Objective-C and C++ method definition syntax in a class interface (I actually hadn't thought to try), but for most practical purposes you can mix freely.

If you are only targeting iPhone then I wouldn't be too worried about writing everything in Objective-C. As others have mentioned, if anything is actually a performance bottleneck you can just profile and optimize it. I am writing my game core in C++ because I may want to deploy on other platforms and in that case Objective-C will become a liability.

Thanks. Performance considerations aside, I'd prefer to use C++ as a matter of personal taste. Sounds like I can do this and still use Cocos2D.
Buggieboy