low-level graphic programming in C++

Requirements :

  • I need to draw 2D lines, circles, boxes. Fast. That's it.
  • I don't want to learn objC, Swift, Xcode.
  • I'm using C++ and Jetbrains' CLion.
  • OpenGL is deprecated (or soon to be) on Mac
  • I don't even care about Apple (as you probably guessed), but the M1 processor is just too good. (and fanless <3)
  • I'll be happy as long as it runs on my MacBook Air
  • Text would be a useful feature but totally optional
  • I won't even use sprites, or any kind of assets, sound, keyboard, mouse, IO, GUI. I need pure non-interactive visualisation of science-ish stuff.
  • fast FPS, refreshing the visualization as fast as possible.
  • Even if it's a pure framebuffer (an array of pixels), I'll manage.
  • If it's a full fledged 3D accelerated graphic library (eg : openGL, Vulkan, Metal), it also works for me.
  • "as long as it works"

What kind of options are available to me with the aforementioned requirements ?

  • use openGL anyway and cry when it will no longer works ? (meh)
  • hack my way to Metal in C++ ?
  • I'm fine if a suggested library isn't Object Oriented (eg : openGL)
  • Vulkan ?
  • I still need to open a graphic window context somehow
  • SDL2 appears to have a some kind of Metal backend/port/whatever but all the exemples I find are in ObjC.
  • honestly, my post feel like I'm crying for a documented c++ metal wrapper of some kind I suppose ?

EDIT :

  • I read mixed message and confusion on the Future of OpenGL on Mac. Mostly about "no longer supported" vs "Deprecated". Will OpenGL be removed and no longer works. Or it will works but won't be updated anymore ? (considering my requirements, an outdated version of OpenGL is perfectly fine to me)

Thank you <3

Answered by Graphics and Games Engineer in 689936022

Hi ker2x!

Glad you are enjoying the Apple M1 processor!

To answer your questions:

  1. To render 2D primitives, you can give Quartz a try. It has a C API, so it meets your requirement of not having to use Swift/Objective-C. You can use CGBitmapContextCreate to create a context for rendering to an offscreen buffer.

  2. This leaves you with the problem of displaying this offscreen buffer on screen. This can be accomplished by using any 3rd party app framework.

  3. Generally speaking to do high performance rendering using the GPU, your best option is to use Metal. Unfortunately as of today no C++ interface/wrapper for Metal exists. This means that if you want to take this route, there is no way of completely avoiding objective C. By using the objective C++ language (.mm files), you can effectively mix C++ with objective C code. This means you can limit the amount of objective C++ code to just the (metal) API calls, while the rest of you application can be written completely in C++. I hope that would take away some of the burden of having to completely learn a new programming language. An example of using Metal this way can be found here.

Good luck!

PS : If I really "must" use Xcode (for some obscure mandatory reasons), I guess I will. However, forget about telling me to code my software in ObjC or Swift. I already tried.

You can download C++. Search for C++ in appstore and have a look at this:

h t t p s : / / w w w.wikihow.com/Set-Up-C%2B%2B-and-Write-Code-on-Xcode

And yes, you should use Xcode. If you plan to publish your app, you have to. Good luck.

Metal runtime is strongly and tightly coupled with the Apple's UI frameworks -- intended to be used with ObjC or Swift. So, you should better give up the option -- hack my way to Metal in C++. Meaning use openGL anyway and cry when it will no longer works ? (meh) would be the only options if you insist on denying to learn ObjC or Swift. And Deprecated in Apple's frameworks means that it is not recommended to use it any more and will be removed at any time in the future. So, if your M1 Mac can run OpenGL apps currently, it would work for a while -- an unpredictable period. It may be months or less, or years.

Accepted Answer

Hi ker2x!

Glad you are enjoying the Apple M1 processor!

To answer your questions:

  1. To render 2D primitives, you can give Quartz a try. It has a C API, so it meets your requirement of not having to use Swift/Objective-C. You can use CGBitmapContextCreate to create a context for rendering to an offscreen buffer.

  2. This leaves you with the problem of displaying this offscreen buffer on screen. This can be accomplished by using any 3rd party app framework.

  3. Generally speaking to do high performance rendering using the GPU, your best option is to use Metal. Unfortunately as of today no C++ interface/wrapper for Metal exists. This means that if you want to take this route, there is no way of completely avoiding objective C. By using the objective C++ language (.mm files), you can effectively mix C++ with objective C code. This means you can limit the amount of objective C++ code to just the (metal) API calls, while the rest of you application can be written completely in C++. I hope that would take away some of the burden of having to completely learn a new programming language. An example of using Metal this way can be found here.

Good luck!

low-level graphic programming in C++
 
 
Q