Showing posts with label usb. Show all posts
Showing posts with label usb. Show all posts

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);
     
     SDL_JoystickClose(m_joystick);
     m_joystick = SDL_JoystickOpen(js);
}
is.  Go ahead and comment out the line that says
SDL_JoystickClose(m_joystick);
.  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.

Friday, February 17, 2012

Qt + SDL on OSX

First things first:  I spend way more time in Windows 7 on my MacBook Pro than I do in OSX SL.  So when I finally learned a little about OSX's frameworks and *.dylibs I thought I'd give Qt and SDL on OSX another try.  After about 30 minutes of research, I have successfully used SDL on OSX to read joysticks using a Qt application!  Now, I can configure Monterey to compile on OSX and rov-suite will be another step closer to it's full cross-platform distribution that I set out to create when I first started it.

Here's the photographic proof:



I got the original Joypick source code from http://code.google.com/p/joypick/.  By the way, the developer of Joypick is the same guy that I got the QJoystick class from... which I use in Monterey.  It's the best class that I've found out there for reading USB joysticks.  If you are looking to use USB joysticks in Qt, check it out!

I'm pretty busy right now with things other than programming, but I'll post a tutorial about how to use Qt and SDL on OSX here soon (within the next week).  

Thanks for stopping by!

Chris K