Mac OS X Cocoa Integration Patch and Sample Code


As requested, I’ve put together Cocoa integration support. I’d like to
get several people to apply the patches and run tests before including
this in 1.2.6.

Included are two patches to CVS version and new Project Builder project

Screenshot of the sample program:
Installing the new stuff

cd SDL12/src/video/quartz
patch -p0 < ~/Desktop/src-video-quarz.patch

cd SDL12/src/main/macosx/exports
patch -p0 < ~/Desktop/src-main-macosx-exports.patch

Then drop PBProjects.tar.gz in SDL12/ and unpack.

Compile SDL.framework, then you can open the example program in
SDL12/PBProjects/Project Stationary/SDL Custom Cocoa Application.
There’s also new stationary for OpenGL and Nib-based SDL apps. Beware
the stationary hasn’t been converted into PB’s stationary format yet
and the developer install script needs to be fixed, so these projects
aren’t ready to be put into CVS.

How it works

This will probably make little if no sense to anyone who isn’t familiar
with Cocoa. You can always read up on Cocoa at For clarification of the following, you
should consult the example code.

SDL will handle three new environment variables: SDL_NSWindowPointer,

SDL_NSWindowPointer contains the address of an instance of NSWindow.
However, you probably want to use a subclass or instance of
SDL_QuartzWindow because it handles window resizing and other things.
You set it by passing the integer value of the NSWindow object/pointer:

sprintf (buffer, “%d”, (int)nsWindowObj);
setenv (“SDL_NSWindowPointer”, buffer, 1);

SDL_NSQuickDrawViewPointer points to an instance of NSQuickDrawView.
This view controls where SDL will do its drawing, send mouse events,
etc. The view must lie within the window you passed with
SDL_NSWindowPointer. Note that YUV overlays are currently untested, but
should work. Using NSOpenGLView will probably not work.

SDL_ENABLEAPPEVENTS tells SDL to pass keyboard events to Cocoa. With
this environment variable set, Cocoa keyboard shortcuts, textboxes,
etc, will work as expected. The Cocoa objects will get the keyboard
events before the SDL event functions return them.

Before you call SDL_SetVideoMode(), you should make sure that you’ve
set both SDL_NSWindowPointer and SDL_NSQuickDrawViewPointer
(SDL_ENABLEAPPEVENTS is optional). Also, the window should be visible
and key. For example, call [ nsWindowObject makeKeyAndOrderFront:nil
]. There are some other optional initializations you may want, see [
MyController setupCocoaWindow ] for details.

If you want to use a resizable window, make sure you’ve set the
resizable properties of the window instance you pass. You can have a
fix-sized view and resizable window if you want - just ignore

When you call SDL_SetVideoMode(), the environment is checked for
SDL_NSWindowPointer and SDL_NSQuickDrawViewPointer. If these are both
found, the window pointer and view pointer are stored and and the
window pointer is retained (the view pointer is not retained).

When the video mode is destroyed, the window pointer is released. The
window is also closed, so make sure the “release when closed” attribute
is not set on the window (unless you want to recreate it). If the
window closes, you’ll have to make it visible and key again before
SDL_SetVideoMode() is called. You can override the window’s close
method to keep it from closing until you want it to.

The sample code should be very helpful when you begin constructing your
Cocoa GUI. Let me know if you have any problems or questions.