If you're drawing lines, I recommend using Quartz and Core Graphics instead of Carbon. Not many people on programming forums use Carbon so you are going to have a difficult time getting anyone to answer Carbon questions.
Apple's Quartz 2D Programming Guide is a good source for learning Quartz. The Quartz 2D Programming Guide is part of Apple's developer documentation, which you can read online or in Xcode.
Cocoa is a series of frameworks that consists of APIs and the runtime layer that make up the bulk of OS X. Whenever you invoke a stock window, save a file or get user input you're using Cocoa. Cocoa "speaks" Obj-C natively.
Carbon was a technology designed to bridge the transition from the older OS 9 technologies to OS X. Carbon is no longer necessary should be avoided at this point.
Quartz is the graphics specific engine framework within Cocoa and comes in 2D and 3D flavors.
If you understand OOP then it will take you an afternoon to learn Obj-C (since it's just C with classes and messaging) and this where you should start. Even if you want to reuse the bulk of your C++ libraries don't resist learning Obj-C because it's easy and will facilitate your understanding of the interface to Cocoa.
Cocoa and Carbon are frameworks for writing GUI applications. Cocoa is an Objective-C framework. Carbon is a C framework. Quartz is an engine for drawing 2D graphics. Reading the Quartz 2D Programming Guide provides more information on what Quartz does. The Cocoa Fundamentals Guide, which is part of Apple's developer documentation, provides more information on what Cocoa does.
Quartz is newer than Carbon. Carbon is going to be obsolete first. The GUI parts of Carbon were not updated for 64-bit Macs, which means you can't write 64-bit applications using Carbon only. That's part of the reason I advised you not to use Carbon.
This has been extremely helpful, and I thank you. But after some hours of beating my head against the documentation, I still do not know how to do some basic things.
For starters, when I start a new application project in XCode, it gives me a few options:
- various AppleScript apps
- various Carbon apps
- various Cocoa apps
- various Core Data apps
Quartz is nowhere on the list. It looks like the right option may be to tell it I am writing a Carbon C++ app and then include the Quartz libraries explicitly. Does this sound correct?
I understand that I need to use the functions CGContextBeginPath(), CGContextMoveToPoint(), CGContextAddLineToPoint(), and CGContextStrokePath(). But I do not know how to establish the CGContextRef in the first place. The documentation says I have to either use Cocoa for this, or use UIGraphicsGetCurrentContext(). Is this correct, or am I missing something?
When I try to use UIGraphicsGetCurrentContext() it claims that this function is not declared. I assume I need to include the Quartz library, but I cannot find where it is. (The Carbon and Cocoa libraries came with the XCode download. Do I need to get Quartz separately?)
Thanks again for all your help with this.
You want to start with a Cocoa Application. Basic Frameworks will be included in the generic project and additonal frameworks - like Quartz - required by a program are added to the project as needed.
I personally would start with a basic app and get some experience with Xcode/Interface Builder and Cocoa and leave out any legacy code at this point. You are starting a big project taking on a new dev environment, run time and OS so make it easy on yourself.
A decent (if dated) beginner dev site is cocoadev and they have some specific Quartz graphics examples I'd check out: http://cocoadevcentral.com/d/intro_to_quartz/
If you have access to Apple developer site defnitely start with some of their example code since this can give you a jump started project with the frameworks included and drawing already happening.
Once you feel comfortable with builing an app and seeing how the Quartz graphics system works and what it requires for input then I would work on getting that data from your old C++ code. You'll need to decide where to draw the line between your old C++ code and Obj-C code. It strikes me that using your C++ to define the data and Obj-C to call Quartz to actually draw it - as opposed to trying to draw in Quartz with C++ - is probably where you want to be.
There is a good discussion thread about using/wrapping C++ inside Cocoa apps here:
I think I am beginning to understand that it simply is not possible to do graphics programming in C++ on a Mac any more. Is that true?
I cannot find a single web page which contains sample C++ code for so simple an operation as creating a graphics context and drawing a line. It is all in Objective-C.
I think I may end up porting to Windows.
If you don't need much of a Mac GUI, you could try using a cross-platform game library like SDL, SFML, or Allegro. These libraries allow you to create a window and draw in them. Your old C++ code probably won't run out of the box, but by using these libraries you can do graphics programming in C++ and avoid using Objective-C. These libraries would also allow you to port to Windows and Linux as well as Mac OS X.
Seconding what Mark said. If you want to actually draw using your C++ code itself you'll probably need a third-party graphics/display engine like OGL or something platform agnostic. If you can separate the calculation of what you want to draw from the actual drawing commands then do the calculations in your original code and then passing the information to be drawn with Cocoa/Quartz commands then you'll probably be happy with that.
That's what I was alluding to earlier:
You'll need to decide where to draw the line between your old C++ code and Obj-C code. It strikes me that using your C++ to define the data and Obj-C to call Quartz to actually draw it - as opposed to trying to draw in Quartz with C++ - is probably where you want to be.
All this depends on what you really want to do. If all you want is to recycle your old C++ code for personal use then Cocoa/Quartz is probably a poor choice. If you want use it going forward and want it to run it on OS X and/or iOS then it may worth the effort for you to convert it and learn some Cocoa. Maybe a third party library will give you all a wider range of places to run it for the same trouble was learning Cocoa so if that's desriable then look into that.
Only you can decide what you want and how much effort is worth that for you,