iDevGames Forums
Using Cocoa/objective-c in C++ - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Using Cocoa/objective-c in C++ (/thread-3007.html)

Using Cocoa/objective-c in C++ - nightition - Oct 1, 2007 01:46 PM


although an experienced programmer, I'm completely new to the Mac world, so please bear with me Smile

We're currently developing an engine and a game, which we'd like to run on most modern platforms.

As a beginner, I thought Carbon would be my most natural choice, since I can call it from C++. However, since Apple discontinued future Carbon support, it seems pointless to start learning an already dead technology.

What I am wondering is how Cocoa with Objective-C would fit into this whole story? In other words, is it possible to have an otherwise C++ application, but with just small parts using Cocoa for window initialization/message processing, etc.? I've seen examples where objc code calls c++ code, but I don't find it convenient since it doesn't fit with the code on other platforms - I would need to 'wrap' the whole objective-c part around some c++ code in order to make the whole thing portable.

Is it realistic to expect to achieve something like this? What are other developers who need cross-platform compatibility doing? Is there a universal solution to all this?

A few months ago, I got really excited about Macs, and really looked forward to switching to OS X... However, after finding out that Carbon is officially going out, and that practically the only way to program for OS X in the future will be Cocoa/objective-c, I'm getting more and more frustrated. Now I'm near the point to just dump the whole Mac thing and get back to Windows...
*end rant*


Using Cocoa/objective-c in C++ - OneSadCookie - Oct 1, 2007 01:55 PM

ObjC and C++ play perfectly well together. Putting ObjC objects in C++ classes is slightly easier than putting C++ objects in ObjC classes. .mm files can include both ObjC and C++ code.

I don't understand the basis for the rant. Carbon was nasty, it's much better dead. In its place, you get a modern OO application framework in a nice OO language. What's the issue?

Using Cocoa/objective-c in C++ - TomorrowPlusX - Oct 2, 2007 08:46 AM

ObjCis a sweet language, so don't fret too much.

Anyway, like you, I'm a C++ guy. My 3d stuff is 100% C++. But, there's nothing stopping you from encapsulating OS X "toolkit" stuff written in ObjC/Cocoa with a C++ interface.

My own project has a pretty decent cocoa backend, but apps using it are 100% C++, and never need to touch ObjC.

That being said, if you're writing a nice GUI app, you'll completely and utterly fall in love with Cocoa. You just have to let go. Use C++ where it makes sense, use ObjC where it makes sense. I imagine you have some routines written in highly optimized C, right? Well, this is just one more C-like language to deal with. Each have their strengths.

Seriously, I learned ObjC in a weekend.

Using Cocoa/objective-c in C++ - MattDiamond - Oct 3, 2007 11:39 AM

nightition Wrote:Hello,
is it possible to have an otherwise C++ application, but with just small parts using Cocoa for window initialization/message processing, etc.?

Yes. I did this as an experiment once. My C++ game core had it's own event class that represented events like key-down, mouse-down. Then I created a Cocoa application shell, and tied them together. Whenever the cocoa framework told me there was a mousedown, I created one of my game-event objects and told my game engine to process it.

Months later I replaced the Cocoa framework with SDL, again converting SDL events into my internal game-events. Thus most of my game could be written in C++ and was oblivious to the change in the wrapper.

That worked fine as far as it went. It turns out I should have abstracted screen coordinates as well, because the SDL coordinate system differed. In addition, I should mention my game was only theoretically portable- I never tried to port it to Windows for instance.

But the point is that most of your game can be C++, and you can use Cocoa and Obj-C just for the window, menus, and events, and it's not particularly difficult.