# Mac OS X Cocoa Integration Patch and Sample Code

**URL:** <https://discourse.libsdl.org/t/mac-os-x-cocoa-integration-patch-and-sample-code/9071>\
**Category:** SDL Development\
**Created:** [July 21, 2003, 3:59pm UTC](https://discourse.libsdl.org/t/mac-os-x-cocoa-integration-patch-and-sample-code/9071 "2003-07-21T15:59:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darrell\_Walisser](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/darrell_walisser/32/588_2.png) [@Darrell\_Walisser](https://discourse.libsdl.org/u/Darrell_Walisser)\
**Post date:** [July 21, 2003, 3:59pm UTC](https://discourse.libsdl.org/t/mac-os-x-cocoa-integration-patch-and-sample-code/9071/1 "2003-07-21T15:59:02Z")

</div>

Hi,

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  
files.

[http://homepage.mac.com/walisser/downloads/PBProjects.tar.gz](http://homepage.mac.com/walisser/downloads/PBProjects.tar.gz)  
[http://homepage.mac.com/walisser/downloads/src-main-macosx-exports.patch](http://homepage.mac.com/walisser/downloads/src-main-macosx-exports.patch)  
[http://homepage.mac.com/walisser/downloads/src-video-quartz.patch](http://homepage.mac.com/walisser/downloads/src-video-quartz.patch)

## Screenshot of the sample program: [http://homepage.mac.com/walisser/downloads/sdlcocoa.png-----------------------------](http://homepage.mac.com/walisser/downloads/sdlcocoa.png-----------------------------) 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  
[http://www.apple.com/developer](http://www.apple.com/developer). For clarification of the following, you  
should consult the example code.

SDL will handle three new environment variables: SDL\_NSWindowPointer,  
SDL\_NSQuickDrawViewPointer, and SDL\_ENABLEAPPEVENTS.

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  
SDL\_VIDEORESIZE.

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.

-Darrell
