18
\$\begingroup\$

I'm pretty confident programming in Objective-C and C++, but I find Objective-C to be somewhat easier to use and more flexible and dynamic in nature.

What would be the pros and cons when using C++ instead of Obj-C for writing games in iOS? Or rather, are there any known problems with using Obj-C as compared to C++?

For instance, I suspect there might be performance issues with Obj-C compared to code written in C/C++.

\$\endgroup\$
1

5 Answers 5

21
\$\begingroup\$

One big upside to C++: If you decide to do so, it'll be relatively easy to port to Android/PC/DS/PSP/(insert platform of choice here). Objective-C will lock you into iOS until you decide to rewrite the entire game.

\$\endgroup\$
5
  • 10
    \$\begingroup\$ I disagree with the word "easy." "Possible" may be a better choice. \$\endgroup\$
    – 3Dave
    Commented Apr 13, 2011 at 21:11
  • 1
    \$\begingroup\$ Yeah, for many smaller games (eg. most iPhone games) it's better to simply re-write everything from scratch for the new platform rather than attempting to adapt the code from one platform to work on another. A solid MVC architecture would minimize this pain, but who would bother with that for a match-3 game? \$\endgroup\$
    – jhocking
    Commented Apr 13, 2011 at 22:01
  • 3
    \$\begingroup\$ Relatively easy! If the alternative is impossible, then "extremely difficult" is relatively easy ;) \$\endgroup\$
    – ZorbaTHut
    Commented Apr 22, 2011 at 6:57
  • \$\begingroup\$ blog.vucica.net/2011/06/… he talks about building Objective C apps for android. It's just Linux and Linux can run Objc as easily as an iPhone. \$\endgroup\$ Commented Aug 26, 2012 at 0:49
  • \$\begingroup\$ Using objective c won't lock you in. Using functions specific to the XNU kernel will. Gclang is there for a reason. \$\endgroup\$
    – user20618
    Commented Dec 12, 2012 at 18:49
16
\$\begingroup\$

There are no real pros or cons here, at least none that should force a programmer comfortable in one language to have to use the other.

Performance shouldn't be an issue. It's unlikely that you'd write any heavy lifting with lots of messaging in the inner loops if you're a good Obj-C programmer, which means you'll really be writing those inner loops in C. If you're not a low level programmer, chances are the heavy lifting will be done by whatever libraries you chose to defer that too, and you'll be at the mercy of whatever they've chosen to do.

Portability is a real issue, if you care about it. If not, then meh. Being cross platform isn't the end all be all. Being successful on one first would be nice :) You can always port later. If you're not carefully planning to be cross platform from day 1 anyway, just being in the same language won't get you very far: there are plenty of further real differences between platforms that need addressing.

Finishing the project is more important that fretting about the tech, and if you're more productive in Obj-C then stay in Obj-C.

My preference? I'm a C++ guy. I like Obj-C just fine, but I'm happy in C++, and I do write cross platform code.

\$\endgroup\$
1
  • 5
    \$\begingroup\$ I very much concur with the advice to focus on finishing the project with whatever tools you are most productive with. Avoiding premature optimization doesn't only apply to writing code, it's a good way to approach life. \$\endgroup\$
    – jhocking
    Commented Apr 13, 2011 at 22:04
4
\$\begingroup\$

There are no performance issues with Obj-C over C++. Both are compiled code, and recall that Obj-C is a superset of C, and Obj-C++ is a superset of C++, which is great when you want to mix Obj-C and C++ in the same program (easy to do and XCode has nice support for the C++ side). All language combinations support OpenGL, so any serious graphics work is fine and the library support is ok with Apple regardless of language.

That said, what is the purpose of your game?

  • If you are solely writing for IOS, then don't worry and write in whatever language or combination you feel more comfortable in. For me, this happens to be Obj-C++ with STL since that's my favorite part of C++. Since I prefer Obj-C slightly over C++, I tend to use this option.

  • If you are writing with the goal of porting to another device, write in C++ with a translation layer to Obj-C to deal with any device-specific stuff (graphics, input, camera, ...), where the calls are made to entry-points in your application.

  • If you are a purest and want just one language, write in Obj-C.

Others have talked about other libraries (physics and such). There are absolutely no problems with accessing any of that from any of the language choices you are talking about (Obj-C, C++, or Obj-C++). You will also not have any performance issues with those libraries in any language.

A caveat: There is a pointer indirection call with message passing. I'm not sure how much you can interpret that as overhead, since I'm pretty sure that's exactly the same as an inherited C++ method call. Can anyone clarify this?

Hope that helps,

Lee.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Good points, but would you really want to write your Vector3D class in Obj-C? My gut feeling tells me that could lead to bad performance. \$\endgroup\$ Commented Apr 16, 2011 at 9:52
  • 1
    \$\begingroup\$ That's just it. Obj-C is C. Not a performance issue. If you go to the standard header files with XCode, you will find the underlying types are just C structs. \$\endgroup\$
    – Lee
    Commented Apr 16, 2011 at 20:46
  • 2
    \$\begingroup\$ The message overhead with Obj-C is quite a bit higher than C++ because it's more dynamic. It's certainly NOT "exactly the same". eg: see developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/… \$\endgroup\$
    – Andy Dent
    Commented Mar 23, 2012 at 5:10
1
\$\begingroup\$

It depends on exactly what you are programming. If it's mainly game logic using existing physics and graphics engines, i can safely say i don't see any reason to use c or c++ if you're more comfortable with obj-c. If you are going to be rolling out rendering or physics classes i would probably write those using c or c++. Cocos2d - which is a wonderful and one of the most popular iOS game libraries is written in obj-c.

Check out this read - the relevant part is "division of labor", might help you to decide when to code in a higher level language and when to use lower level: Managed code in games

While objective-c overhead should be no where near C# and isn't even managed (on iOS), i believe the logic still applies, and devices running iOS are also no where near as strong as the PC that would be interpreting MSIL :)

\$\endgroup\$
9
  • 2
    \$\begingroup\$ Obj-C isn't managed code on iOS. For example (and in particular) Obj-C has a garbage collector when programming for OSX, but it doesn't have a garbage collector on iOS. \$\endgroup\$
    – jhocking
    Commented Apr 13, 2011 at 20:08
  • \$\begingroup\$ @jhocking i never said it was managed, isn't there still overhead when using obj-c as opposed to c++? I did mention c# vs c++ it was no where near obj-c vs c++ \$\endgroup\$ Commented Apr 13, 2011 at 20:11
  • \$\begingroup\$ When you post a link called "Managed Code in Games" while talking about Obj-C then I take that to mean you think Obj-C is managed code. Which it is on OSX, just not on iOS. Kind of goofy that, as if Java compiled to machine code on some platforms but ran in JVM on others. \$\endgroup\$
    – jhocking
    Commented Apr 13, 2011 at 20:14
  • \$\begingroup\$ oh and I don't know if Obj-C has any overhead compared to C++. That's why I'm not posting an answer myself. \$\endgroup\$
    – jhocking
    Commented Apr 13, 2011 at 20:14
  • 1
    \$\begingroup\$ @jhocking no sir :) "The thing is, the retain/release model in Objective-C is roughly a middle ground from no memory management to a basic GC so you are already ahead of the curve." interfacelab.com/objective-c-memory-management-for-lazy-people \$\endgroup\$ Commented Apr 13, 2011 at 21:57
1
\$\begingroup\$

This study says to really get the performance in a CPU intensive game, you have to use C. The linked article is complete with a XCode project that you can run.

I believe the bottom line is: Use Objective-C where you must interact with the iPhone's functions (after all, putting trampolines everywhere can't be good for anyone), but when it comes to loops, things like vector object classes, or intensive array access, stick with C++ STL or C arrays to get good performance.

I mean it would be totally silly to see position = [[Vector3 alloc] init] ;. You're just asking for a performance hit if you use references counts on basic objects like a position vector.

See also these comparisons,

comparisons

\$\endgroup\$
1
  • \$\begingroup\$ Downvoter? Please explain. \$\endgroup\$
    – bobobobo
    Commented Sep 17, 2012 at 21:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .