jacius / ruby-sdl-ffi

Ruby-FFI binding to SDL multimedia libraries. (Still in alpha)
http://jacius.github.com/ruby-sdl-ffi/
16 stars 6 forks source link

Some magic to get this working with vanilla Ruby on OS X #2

Closed erisdev closed 13 years ago

erisdev commented 14 years ago

If you don't mind a little bit of magic and a little bit of platform-specific code, NSApplicationLoad is the officially sanctioned way to enable Cocoa in non-Cocoa applications.

module MacMagic
  extend NiceFFI::Library
  load_library '/System/Library/Frameworks/Cocoa.framework/Cocoa'
  func :NSApplicationLoad, [], :char
  func :NSPushAutoreleasePool, [], :void
  func :NSPopAutoreleasePool, [], :void
  NSApplicationLoad()
end

This results in memory leaks for some functions (particularly SetVideoMode and other functions that interact with the window server) because SDL uses Cocoa behind the scenes but doesn't allocate NSAutoreleasePools to clean up after itself. Ideally, SDL would do the right thing itself, but I suppose calls that leak can be wrapped with the other two functions bound above until then.

If this doesn't get integrated somehow into the official ruby-sdl-ffi, well, it's here for everybody else who wants to use it on Mac and can't get the damned rsdl binary to build. :)

jacius commented 14 years ago

Thank you!

Words cannot express how grateful I am for this information. I had pretty much given up hope for getting SDL 1.2 to work on Mac without a special interpreter. I still have some work to do to deal with the memory leaks you mentioned, but this has given me some very substantial hope that it can be done.

I've pushed ef1d643a8324e22cafe0014a7611596948f9a65e, which works fine (memory leaks aside) on standard Ruby, as well as on JRuby -- which is particularly exciting, as there is no rsdl equivalent for JRuby.

So again, thank you!

I've got some prototype code for wrapping the functions, tomorrow I'll take a crack at identifying the ones that need wrapped and getting it to work. Also, it appears that I'll need to find a way to set up the top menu for the application so that OSX will let the window gain focus. A tricky puzzle, but I'm feeling encouraged to tackle it. :D

erisdev commented 14 years ago

Glad I could help! It was a problem that bothered me for a while and I decided to get brave and look for a solution myself.

Do you know if SDL is still actively maintained? One of us should probably submit a patch to fix this in the library itself since it is kind of their fault that it doesn't work automagically like everything else does. NSApplicationLoad belongs in SDL_Init I think, and the autorelease pool management should be done in the OS X driver.

jacius commented 14 years ago

SDL is still actively maintained, but I'm not sure it's worthwhile to submit a patch. Most of the development effort there is going into SDL 1.3, which handles window management on Mac differently, and does not appear to have this problem. I don't think they are planning on doing any more 1.2-series releases.

I'm just content to use this magic to make SDL 1.2 work on Mac until SDL 1.3 is released.

jacius commented 13 years ago

Closing this, it has been several months since things have been working in vanilla Ruby. Thanks again for the tips, Eris. :)