Sunday 17 June 2012

Joystick test application (Qt + SFML) [Windows/Linux]

Previously, a demonstration for joystick control was published. It was a demo that made use of Windows DirectInput COM-library with Qt to retrieve the data from joystick - buttons' state and axes positions. Since DirectInput is not a cross-platform library, a new cross-platform demo, based on SFML library is prepared (library has a very liberate license).

How demo works.
The main class of this demo is XInput (include header xinputGamepad.h). It is instantiated and then initialization method initInput(joystick_id) is called, where joystick_id is the index of a joystick in the system starting from zero. Then a timer is set up to generate timeout event every 15 milliseconds (smaller values increases refresh rate and responsiveness), where update() method is called, and joystick state is retrieved using getHorizontal(), getVertical(), isKeyPressed(key_id)getRawAxis(axis_id) and other class functions. 



  Download link:
       application demo (zip): Public_QtJoystick-SFML-Win32-386 - version 0.3a [Win32/i386] - 5.91 MiB
       source code (zip): Source_QtJoystick-SFML - version 0.3a - 233 KiB

    It is written with Qt 4.8.1 for Desktop. Source code is available for download and use under the LGPLv2 or MIT license.

  Usage:
1) Download Public_QtJoystick-SFML.zip archive
2) Unpack it with zip extractor
3) Plug in the joystick (if it isn't plugged in yet)
4) Run QtJoystick-SFML

If you don't have a joystick yet, but want to test the application, there is a joystick emulator for Windows PPJoy (download link, git repository). If you know keyboard/mouse-to-joystick emulator for Linux, please post it in the comments.






8 comments:

  1. Hi! There are no "ui_mainwindow.h" file in archive. Can you reload the archive with this file?

    ReplyDelete
    Replies
    1. Hi Sergey,

      Thanks for trying that!

      In general, there shouldn't be any "ui_mainwindow.h" coming with source files, still it is included. And this is because this file shall be generated by Qt .ui file generator/compiler based on the current mainwindow.ui file, which is supplied with source file in this archive. This header file will differ from Qt version to Qt version, and there's a chance, that if I provide you this file: 1) it will be overwritten by compiler during even first compilation (it should) 2) it has different compilation version. Please take a quick look at

      http://stackoverflow.com/questions/1948185/generate-h-and-cpp-from-ui-file

      and also try searching google "qt generating h file from .ui" this might clear further. By the way, I'm using Qt version 4.7.3 and know it will compile on all the Qt versions since Qt 4 till Qt 4.8.2. But I'm unsure about compatibility of this code to Qt 5+. Which version are you using? Shall I check the code and project compiling/working with newest Qt 5 versions?

      The short answer is that - just try opening the .pro in Qt creator, setup the correct build environment when Qt asks you while opening the project (select MinGW or VS2008 or other compiler), and ensure correct building directories.

      Qt will generate two directories for files made for debug, including intermediate ui_someuifilename.h files for building. QtJoystick-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug. You can not build this code without Qt toolkit or compiler without proper environment variables configured.

      Please let me know if there are any further questions.

      Delete
  2. Hello, Thank you for sharing this with us. But I have a problem to download it, would it be possible to reupload it again? Thank you.

    ReplyDelete
    Replies
    1. Hi,
      sorry - hosting was down..
      Please retry it now:)
      Thanks for the feedback!

      Delete
  3. Hi, the links for the source and demo are no longer valid. Is it possible to have them updated ?

    ReplyDelete
    Replies
    1. Hi Massimiliano, links updated and should work now. Thanks a lot for letting me know!:)

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This example was written in Qt4 but you can run it in Qt5. You need to and "widgets" here: QT += core gui widgets. And replace this line #include QtGui/QApplication to this #include QtWidgets/QApplication in main.cpp

    I built the demo (7.39 MB): https://dl.dropboxusercontent.com/s/jsetztdtx9fsism/QtJoystick-SFML-exe.zip
    Source Code for Qt5 (235 KB): https://dl.dropboxusercontent.com/s/zc885u0facawvlr/Source_QtJoystick-SFML.zip

    I tested this example. It works with PS2 controller and USB converter.

    ReplyDelete

Locations of visitors to this page