Sunday, January 24, 2010

Building pygame on OSX Snow Leopard

The version of pygame available on the pygame site works for the python.org version of Python. I want one for the system Python (2.6.1 64-bit). I decided to revisit this after giving up (very easily) the first time.
  • First, get pygame source. I'm trying with 1.9.1 release. This sounds like recipe now.
  • Untar the source and python config.py reveals some dependencies are necessary. Config will show you which ones you have available.
  • The pygame dependencies are as follows. So we'll need collect these. Notes follow.
    • SDL.
    • SDL_ttf  (fonts).
    • SDL_image.
    • SDL_mixer.
    • smpeg.
    • PNG.
    • JPEG.
    • SCRAP  (cross-platform clipboard).
    • PortMIDI.
SDL
  • There is a Mac SDL 1.2 package on the downloads page.
  • I got SDL-1.2.14.dmg.
  • Version 1.2.14 has Snow Leopard support according to the ReadMe.txt and is the minimum requirement. See notes below from UniversalBinaryNotes file:
64-bit Universal Binary Notes:
SDL 1.2.14 is our first release with Snow Leopard on the market. In order to make SDL compile and run in 64-bit, we had to remove code that depended on deprecated Mac APIs and move over to more modern Mac APIs.
In addition, Apple has stopped shipping gcc 3.3 and the 10.3 SDK.
Because of all these combined factors, we have made the decision to make Mac OS X 10.4 the new minimum requirement for SDL.
Our official SDL.framework is compiled as a 3-way Universal Binary (64-bit Intel, 32-bit Intel, 32-bit PowerPC.)
Certain APIs that SDL relies on were not made 64-bit ready by Apple until 10.6. This means even though 10.5 had preliminary 64-bit support, SDL will not compile or run correctly in 64-bit mode on 10.5. So there are two fallout items from this.
First, you can only compile 64-bit code on Snow Leopard or greater (which removes the possibility of 64-bit PowerPC). 
Second, this presents a corner-case where if you have a 64-bit Intel executable in your Universal binary and try to run on 10.5 on an 64-bit Intel Mac, it will launch and crash. To force 10.5 to use the 32-bit version instead of the 64-bit, you should set the LaunchServices key, LSMinimumSystemVersionByArchitecture, in your application's Info.plist. Our SDL/Xcode templates for Snow Leopard already set this up for you.
One additional fallout item is we had to remove the SDL Custom Cocoa Xcode template project. It depended on NSQuickTimeView which was deprecated and removed from the SDL codebase. It may still be possible to recreate the behavior that this template demonstrated, but we would need a volunteer to investigate this.
In addition, the SDL satellite projects were affected by the 64-bit transition.
- SDL_mixer depended on legacy Quicktime for midi playback support. We had to disable midi. (Recall that we also disabled MP3 support awhile back because we never got SMPEG working during the Tiger/Intel transition.) To fix this, we would need a native Core Audio backend for SDL_mixer.
- Since we have changed the baseline to 10.4, we took this opportunity to switch SDL_image over to a new native ImageIO based backend. This makes the binary about 10x smaller, greatly simplifies our maintenance requirements and build process as we no longer have to maintain build systems for 3rd party dependencies, and gives us access to more image formats.
- The static library target for SDL_ttf no longer works because we no longer have access to a libfreetype.a. We have been relying on Apple's supplied libfreetype.a, but they stopped shipping a static version starting in 10.5 which means we have no static 64-bit version. But since 10.4 is our new baseline, all these systems should have libfreetype.dylib installed, so it shouldn't be much of a problem to use SDL_ttf as a dynamic library which dynamically links to libfreetype.
-Eric Wing 2009-09-23
  • To install SDL 1.2.14 we copy the SDL.framework to /Library/Frameworks:
    • sudo cp -r /Volumes/SDL/SDL.framework/ /Library/Frameworks/SDL.framework
SDL_ttf
  • There is a Mac OSX package on ttf project page.
  • I got version 2.0.9.
  • sudo cp -r /Volumes/SDL_ttf/SDL_ttf.framework/ /Library/Frameworks/SDL_ttf.framework
SDL_image
  • Get package from image project page.
  • I got version 1.2.10.
  • sudo cp -r /Volumes/SDL_image/SDL_image.framework/ /Library/Frameworks/SDL_image.framework
SDL_mixer
  • Get package from mixer project page.
  • I got version 1.2.11.
  • sudo cp -r /Volumes/SDL_mixer/SDL_mixer.framework/ /Library/Frameworks/SDL_mixer.framework
smpeg
  • From icculus page.
  • svn co svn://svn.icculus.org/smpeg/trunk smpeg
  • We'll skip this for, it's optional.
PNG
  • Get libPNG from the Sourceforge project download page. I got 1.2.42.
  • Don't run configure. Instead: cp scripts/makefile.darwin Makefile
  • make
  • sudo make install
JPEG
PortMIDI

  • Source is hosted on Sourceforge. 
  • I got version 2.00. Fairly new. 
  • Note: PortMIDI uses cmake to generate makefiles. So you'll need to have that installed. I have version 2.8.0. 
  • make -f pm_mac/Makefile.osx 
  • cd Release 
  • sudo make install 
SCRAP

I think this is support for the clipboard for pygame. We are missing sdl-config from our SDL framework install so we can't configure SCRAP to build it. We'll skip this.

Building pygame

So after all that palaver, you can now go to the pygame source and run the config.py.

One problem: Portmidi seems to have changed the name of its static library. It now has "_s" appended. config_darwin.py needs editing:
config_darwin.py:    Dependency('PORTMIDI', 'portmidi.h', 'libportmidi_s', ['portmidi']),
    Now:
    • python config.py
    • python setup.py build
    • sudo python setup.py install

    After testing... some of the examples work, but fonts don't. :(  This is something to do with SDL linking with a new dynamic library version of freetype which I think is in the /usr/X11/lib direcotory, but this isn't in the dlopen path... Grrr.


    4 comments:

    Chadrik said...

    don't leave me hanging! what's the fix for fonts?

    Chin Billy said...

    Ha! That's a good question. I'd hoped that someone would have posted an answer, but they haven't. I've avoided using pygame and been using pyglet in my own gamelab library. Haven't had a lot of spare time the last few months but will try and get an answer soon. Cheers. (If you find the answer please let me know).

    Samuel Messing said...

    Thanks for the work through! I've been hunting around for a solution all day.

    I'd also be interested in fixing fonts. Unfortunately I myself don't know how to figure something like that out, but I'll try to post here if I find a solution out there...

    echodot said...

    Just posted an answer to the font issue over at stackoverflow. Hope it helps.

    http://stackoverflow.com/questions/3470377/my-py2app-app-will-not-open-whats-the-problem/5201031#5201031