http://listas.apesol.org/pipermail/sdl-libsdl.org/2003-July/036886.html
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/src-main-macosx-exports.patch http://homepage.mac.com/walisser/downloads/src-video-quartz.patch Screenshot of the sample program: 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. 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.
@interface MyCustomView : NSQuickDrawView
{
}
@end
/
#import <AppKit/AppKit.h>
// Be a subclass of SDL_QuartzWindow so SDL will
// handle the redraw problems when minimizing the window
// This class is defined in SDL.framework
@interface SDL_QuartzWindow : NSWindow
@end
// Also assign SDL_QuartzWindowDelegate to the window
// to perform other tasks. You can subclass this delegate
// if you want to add your own delegation methods
// This class is defined in SDL.framework
@interface SDL_QuartzWindowDelegate : NSObject
@end
// Declare our custom class
@interface MyCustomWindow : SDL_QuartzWindow
@end
// Export cocoa objects to environment
// SDL will use these when you call SDL_SetVideoMode
// The window must be visible when you call SDL_SetVideoMode,
// and the view must lie completely within the window.
//
// The width and height passed to SDL_SetVideoMode should match
// the width/height of the view (the window can be any size)
//
// For resizing to work, you must set the appropriate
// attributes on the window and view. Then the SDL_RESIZABLE
// flag will be set automatically
//
// SDL will retain a reference to the window, and
// will release it when unsetting the video mode
//
// The view is not retained (the window object manages this).
//
char buffer[256];
printf ("NSWindow=%p\n", _window);
sprintf (buffer, "%d", (int)_window);
setenv ("SDL_NSWindowPointer", buffer, 1);
printf ("NSQuickDrawView=%p\n", _view);
sprintf (buffer, "%d", (int)_view);
setenv ("SDL_NSQuickDrawViewPointer", buffer, 1);