How can I “tear myself away” from Cocoa and develop Mac OpenGL applications in C / C ++?

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

If I try to use a text editor and g ++ compiler, I skip the headers, but if I try to use Xcode, I have to get into Cocoa, which is not pleasant for me. I really don’t see the reasons why OpenGL / GLUT, pre-installed on Mac, should force me to use Xcode, but it seems like I cannot get the header files without it.

How can I go through all of Apple’s developer interfaces to write old-fashioned code with full cross-platform portability?

+6
source share
3 answers

Some of Objective-C is inevitable if you want to take advantage of the latest OSX / Cocoa benefits. The easiest way to port an existing application on MacOS is as follows:

  • Write a boneless headless application in Objective-C. This will be only one AppDelegate class and a little tweak in the main () function
  • Add a custom NSGLView downstream to your window, which you create in the AppDelegate doneFinishLaunching event handler
  • Configuring CVDisplayLink and render callback in NSGLView initialization
  • Use existing OpenGL rendering code in CVDisplayLink callback

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

Surprisingly, a good example of a nibless application is the interface for the QEMU OSX port (yes, an emulator). Also, Apple's official GLEssenstialPractices demo shows you all the information you need to set up your OpenGL rendering pipeline. Everything else is up to you.

A detailed and up-to-date introduction to OSX system programming can be found in Mark Dalrimple's book, Advanced Mac OS X Programming. This explains a lot of things, and after reading all this I understood most of the design decisions in the OS (it really makes you accept all the “non-standard” things if you think in terms of performance).

To get through the "nibless" programming, I would recommend you read blog entries like this http://blog.kleymeyer.com/2008/05/creating-cocoa-applications-programatically-ie-nib-less/ google search helps many.

The same tricks apply to CocoaTouch / iOS, and there are many questions on SO like this Cocoa touch / Xcode - creating a graphical context without NIB

+5
source

If you want to create cross-platform applications, you can create a project using the command line tool template. Then import the OpenGL and GLUT framework. This will give you an “empty” C ++ project with the necessary OpenGL and GLUT headers.

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

+2
source

I created a program layer (called cocoglut ) that allows you to translate basic or important GLUT calls to COCOA. This library allows you to create / destroy windows and register callbacks from a C / C ++ application, simply using GLUT calls, without the need for nib files or Xcode project files (and they can be compiled from the command line). This option uses the full screen resolution of the retina. The source is on GitHub .

-1
source

Source: https://habr.com/ru/post/916314/


All Articles