Buggieboy
Buggieboy

Reputation: 4696

Use Objective-C game engine in C++ iPhone game?

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.

Upvotes: 5

Views: 1981

Answers (2)

Whatever
Whatever

Reputation:

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.

Upvotes: 3

Amok
Amok

Reputation: 1269

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.

Upvotes: 7

Related Questions