PDA

View Full Version : Basic Carbon Queston




ba747heavy
Apr 6, 2006, 11:26 AM
At least, I think what I'm interested in is carbon.

My situation is that I am writing an OpenGL application in C. My intent is cross platform compatability, so the code I am writing is going to be as much as possible "safe" code. However, in the area of the UI I need to be able to set the menu bar, and open up sub windows so the user can alter some settings. I've done some googling and basically it seems that OGL gives me the ability to create context menus on mouse clicks, but that isn't what I need.

So what I am looking for is any tutorial or reference that will show how to include and use Carbon, preferably without creating an XCode project and using the interface builder, etc.

Thanks!

Fred Clausen



Soulstorm
Apr 6, 2006, 11:39 AM
At least, I think what I'm interested in is carbon.

My situation is that I am writing an OpenGL application in C. My intent is cross platform compatability, so the code I am writing is going to be as much as possible "safe" code. However, in the area of the UI I need to be able to set the menu bar, and open up sub windows so the user can alter some settings. I've done some googling and basically it seems that OGL gives me the ability to create context menus on mouse clicks, but that isn't what I need.

So what I am looking for is any tutorial or reference that will show how to include and use Carbon, preferably without creating an XCode project and using the interface builder, etc.

Thanks!

Fred Clausen
There are books written by Apple and third party writers that can help you. Every book involves the use of Xcode. And with Carbon and Interface builder, it's easy to include OpenGL to your projects. Why don't you want to use Xcode? Integration with C and the OpenGL API and Carbon API is very good in Xcode.

With XCode, comes the Reference Library which contains a very good reference for Carbon. Alternatively, you can look here (http://developer.apple.com/carbon/), or buy a book.

iSee
Apr 6, 2006, 12:22 PM
I agree with Soulstorm's post--nothing to add there. But here's an idea, if you don't want to jump into Xcode:

Just use Xcode to setup a skeletal Carbon & OpenGL project--it doesn't have to do anything but include Carbon and OpenGL and compile and link. Make a copy of the build log and then quit Xcode. Look at the build log and the source files and use them as a guide to setting up your #includes and make file.
- - - -
Also a few notes about cross-platform development:

Keep the platform specific code separate from the cross-platform code!!!

Define your interface from the cross-platform code into the platform specific code. Keep it simple. For your situation, if I read it correctly, you might use something like this for the settings interface:

MySettings.h
...
struct MySettings {
Int32 setting1;
Int32 setting2;
...
};

bool doSettingsDlg(MySettings* pSettings);
...
MySettings.h is a cross-platform file--this file can be included in your cross-platform code.

Then you would have multiple implementations of MySettings.c, one for each platform you want to support. I usually make a subfolder in my source code folder for each platform. So, you'd have osx/MySettings.c, win/MySettings.c, and linix/MySettings.c (or whatever). You compile and link the one that matches whatever platform you are building for.

Also, (this is important), you ONLY #include carbon.h, windows.h, or whatever in the platform specific .c files. Never #include the platform specific api headers in the cross-platform code. This keeps you honest. If you accidentally try to use a platform specific function, it's a compile error! Nice and easy. I forces you to keep your code clean and separate.

So in win/MySettings.c you have:
...
#include "windows.h"
...
// windows implementation of doSettingsDlg(MySettings* pSettings)
bool doSettingsDlg(MySettings* pSettings)
{
// TODO: implement code to load settings from pSettings into a dialog box
// and then invoke the dialog box
}


And in osx/MySettings.c you have:
...
#include "carbon.h"
...
// osx carbon implementation of doSettingsDlg(MySettings* pSettings)
bool doSettingsDlg(MySettings* pSettings)
{
// TODO: implement code to load settings from pSettings into a dialog box
// and then invoke the dialog box
}

Soulstorm
Apr 6, 2006, 05:15 PM
I must also add that cross-platform code does not exist when programming for Carbon. Carbon API routines don't have any similarities with the ones at windows. So, a few guidelines would be these:

In order to be as cross-platform as I can, I make my application as follows:

I build the main application routines separately from the main.cpp file (yes, I program in C++). In the main.cpp file I only add the interface (command line-like interface). So what I have is the spine of my program with only the necessary routines.

After that, I only have to change the interface file (which in this case is the main.cpp file) with the one thhat suits my platform better. When I program in carbon, I delete the main.cpp file and I rebuild it to include the command event handlers, and then I call the functions that are defined in the separate files.

I would follow the same method in windows, too.

Finally, if you want cross-platform compatibility, have you considered, Qt (http://www.trolltech.com/products/qt/index.html)?

ba747heavy
Apr 7, 2006, 12:56 AM
Thanks for the helpful posts! I've pretty much laid out my program as you recommend; the common routines in a common set of files, and the UI/OS Specific calls go into a separate area.

I'm very interested in the Qt code. That may be the perfect solution. Thanks again.