Sunday, February 26, 2012

Qt + SDL on OSX Tutorial

I know that I promised a tutorial on using SDL with Qt on OSX, and here it is (complete with screenshots)!

To download the example application that I used (Joypick), go here.  You will obviously want to get the source code, not the executable.  It is under the "Source" tab.

Step 1:  Download the SDL Library

Go to the SDL download page to download the OSX runtime library.  It'll download as a *.dmg file.

Step 2: Copy Files
Open up the *.dmg and select the "devel-lite" folder.  Next, copy the "SDLMain.h" and "SDLMain.m" files to a place of your choosing.  I like to copy this kind of files to ...QtSDK/ExternalLibs/ on my hard drive.  It's just a nice place to keep them organized, although you can put them wherever you want.

Step 3: Install Framework
Next, copy the "SDL.framework" folder from the *.dmg to /System/Library/Frameworks/.  That is all there is to installing the framework.

Step 4:  Modify the *.pro File

Here is how my *.pro file looks.  I've modified it slightly from the original to get rid of the Windows related information.  Then, right-click (control-click) on Joypick (under Projects) and click on "Add Existing Files."
Next, locate the "SDLMain.h" and "SDLMain.m" files on your hard drive and select those.  Then click on "Open."  Qt Creator will automatically modify your *.pro file to include these files.

Step 5:  Link to the Framework
Now it is time to link to the SDL.framework that you installed earlier.  Go ahead and add the two lines that you see in the picture above (lines 17 and 18) exactly as they are shown (working on Snow Leopard).  I'll show them below.

QMAKE_LFLAGS += -F/Library/Frameworks/
LIBS += -framework SDL

Step 6: Undefine main
Because of SDL, you'll need to undefine main in your "main.cpp" file.  See picture above for more information.

Step 7: Fix the Crash
Open up "qjoystick.cpp" and go to where
void QJoystick::setJoystick(int js)
     Q_ASSERT(js < availableJoysticks());
     Q_ASSERT(js >= 0);
     m_joystick = SDL_JoystickOpen(js);
is.  Go ahead and comment out the line that says
.  This line causes the application to crash almost each time it is run when not using an XBox 360 wired joystick (bug confirmed on Windows but not tested on OSX).

Step 8: Build and Run!
Go ahead and run Joypick.  It should work like a charm!  I tested this out using a Logitech Extreme 3D Pro joystick on Snow Leopard.  Enjoy being able to run SDL applications on OSX!

I hope this helps out!

Chris K

P.S.  If you have any questions, feel free to ask them in the comments.

1 comment:

  1. Thank you! I've been searching for a while for a good explanation on how to setup SDL with Qt. Much appreciated!