Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I 'break away' from Cocoa and develop Mac OpenGL applications in C/C++?

I am looking to get started with some 3D programming in C or C++. The problem I have is that it seems like the only tutorials I can find for Mac OS use objective C and Cocoa frameworks. I want to obtain the same environment as Windows users, more or less.

If I try to use a text editor and g++ compiler, I am missing headers, but, if I try to use Xcode, I am forced to grapple with Cocoa, which is frustrating to me. I don't really see any reason why the OpenGL/GLUT that comes pre-installed on Mac should force me to use Xcode, but it seems I can't get the header files without it.

How can I get through all of the Apple 'developer friendly' interfaces to write some old-fashioned code with full cross-platform portability?

like image 543
Eric Thoma Avatar asked May 22 '12 01:05

Eric Thoma


2 Answers

Some portion of Objective-C is inevitable if you want to use the latest benefits of the OSX/Cocoa. The easiest way to port an existing application to MacOS would be the following:

  1. Write the "bare bones" nibless application in Objective-C. It would only be a single AppDelegate class and a little setup in the main() function
  2. Add the custom NSGLView descendant in your window which you create in the AppDelegate's didFinishLaunching event handler
  3. Setup the CVDisplayLink and rendering callback in the NSGLView initialization
  4. Use your existing OpenGL rendering code in the CVDisplayLink's callback

Now for the interesting part: where to get all of this ?

Surprisingly, a good nibless application sample is the UI for OSX's port of QEMU (yes, the emulator). Also the Apple's official GLEssenstialPractices demo shows all the information you need to set up OpenGL rendering pipeline. All the rest is up to you.

The detailed and modern introduction to system-level OSX programming can be found in the "Advanced Mac OS X Programming" book by Mark Dalrymple. It explains many things and after reading all of this I've understood most of the design decisions in the OS (it really makes you accept all the "non-standard" things if you think from the performance viewpoint).

To get through the "nibless" programming I would recommend you to read the blog posts like this one http://blog.kleymeyer.com/2008/05/creating-cocoa-applications-programatically-ie-nib-less/ The google search helps a lot.

The same tricks apply to the CocoaTouch/iOS and there are a lot of questions answered on SO, like this one Cocoa touch/Xcode - generating NIB-less graphics context

like image 180
Viktor Latypov Avatar answered Nov 01 '22 12:11

Viktor Latypov


If you want to create cross-platform applications you could create a project with the Command Line Tool template. Next, import the OpenGL and GLUT framework. This will get you a "blank" C++ project with the required OpenGL and GLUT headers.

Lighthouse 3D gives you some tips about portability and how to initiate your first project. http://www.lighthouse3d.com/tutorials/glut-tutorial/initialization/

like image 29
marlonp33 Avatar answered Nov 01 '22 10:11

marlonp33