![logo](https://private-user-images.githubusercontent.com/493908/313143088-f22a1ade-a730-48e4-8f71-681e5cc01aa6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ5MDE3NDIsIm5iZiI6MTcyNDkwMTQ0MiwicGF0aCI6Ii80OTM5MDgvMzEzMTQzMDg4LWYyMmExYWRlLWE3MzAtNDhlNC04ZjcxLTY4MWU1Y2MwMWFhNi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODI5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgyOVQwMzE3MjJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zNmZhNTM1M2FjM2JlMTAxNGJiZTlmNWJlNzNiZGY3ZDMzNDQ3NDk1ODVlODhkN2VlNTlmY2JiNTQ4ODZjZDQwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.8dGa-36t8n6B-GptFtNtqlXgcRf08o3JUnkM8zW8aXw)
🎮 modified Gamepad4J.
- use jna instead of jni
- use event listener instead of polling
- jinput spi
- linux, windows are not tested
jinput spi
- keep the context object for callback not to be garbage collected
- callback issue: it's important also callback is static or not, why ???
A simple, elegant and powerful API for using gamepads like the Xbox 360 or DualShock 3 pad in Java-based games.
Release 1.0 planned for Nov/Dec 2014
- The core API provides simple, object-oriented access to the buttons, analog sticks and triggers of a common gamepad.
- Convenience methods for getting the direction (degrees) and distance to center of an analog stick
- Constants for querying specific buttons, like "lower face button" or "upper left shoulder button"
- Constants for handling common "accept" and "deny" buttons
- Predefined text keys and labels for all the buttons, so that with a DualShock 3, a message can be displayed like "Press X to accept", while with an Xbox 360 pad, it would be "Press A to accept".
- Low-level methods for accessing all analog axes and digital buttons of a controller
- Low footprint, minimal object allocation at runtime
- Single Uber-jar contains all wrappers for all supported platforms
- The executable Uber-jar also contains a mapper tool that can be used to create new mappings for desktop controllers.
- Provides clean Gradle and Maven build for the Java part
- Apache 2.0 license
Initialize the API, query the connected controllers, and check the state of some buttons and an analog stick. Then shut down the API again when exiting the game:
// Initialize the API
Controllers.initialize();
...
// Poll the state of the controllers
Controllers.checkControllers();
IController[] gamepads = Controllers.getControllers();
...
// Use the lower gamepad face button ("X" on DualShock, or "A" on Xbox pad)
IButton jumpButton = gamepads[0].getButton(ButtonID.FACE_DOWN);
if(jumpButton.isPressed()) {
...jump...
}
// Use the d-pad
if(gamepads[0].getDpadDirection() == DpadDirection.DOWN) {
...move down...
}
// Use an analog stick
IStick leftStick = gamepad[0].getStick(StickID.LEFT);
StickPosition pos = leftStick.getPosition();
// Now check stick position
if(!pos.isStickCentered() && pos.getDirection() == 45f) {
...move character up right, 45 degrees...
}
// Game ended / shut down the API
Controllers.shutdown();
- OUYA (uses OUYA ODK controller API)
- Desktop (Windows, Linux, MacOS, uses JNI wrapper)
Support for more platforms is planned for the future, specifically generic Android 2/3/4, Sony Xperia Play and Gamestick.
The API provides mappings for some popular controllers out of the box:
- Microsoft Xbox 360 wired (USB) and wireless controller
- Sony DualShock 3
- Sony DualShock 4
- Logitech F310
Support for more controllers will probably be included in future releases. Sending in your own, custom created mappings (using the Mapper tool contained in the executable Uber-jar) will help a lot.
It is not (yet) designed to deal specifically with joysticks / flightsticks, steering wheels etc. However, since today probably 80% of game developers really just want to support a gamepad for their game, this is what this API is focusing on. Still, all the analog axes and digital buttons of a controller can be queried, so the API can be used to handle any kind of controller, it just provides no mappings or convenience methods to do so. In the future, specific support for joysticks will probably be added (and maybe for steering wheels, if possible).
- On desktop systems, the mappings may not always be correct, depending on what driver is being used.
- The Xbox 360 pad analog triggers are not properly supported on Windows, due to the Microsoft driver putting them both on the same analog axis (one using the positive, the other using the negative value range; press both, and they negate each other). Ironically, there seems to be no such issue on Linux and MacOS.
- Project Site: http://www.gamepad4j.org
- Github Site: https://github.com/marcelschoen/gamepad4j
- Javadoc: http://www.gamepad4j.org/javadoc
- Downloads: https://sourceforge.net/projects/gamepad4j/files/
- Native Gamepad API: http://forums.tigsource.com/deviceId.php?topic=10675
- Java API by Marcel Schoen, Switzerland
- Desktop native API by Alex Diener: http://forums.tigsource.com/deviceId.php?topic=10675.msg328738#msg328738
- Native API multiplatform build support and testing by Thomas Frauenknecht, Switzerland
- Website Design by Jason Long: https://github.com/jasonlong/architect-theme
- Jekyll Theme by Pietro Menna: https://github.com/pietromenna/jekyll-architect-theme
- GIT (sourcecode is on Github)
- Java 1.6
- Maven 3.2 or Gradle 1.2
- OUYA SDK (ODK) installed, environment variable "ODK" set for Gradle, or Maven property "ouya.sdk.path" set accordingly
- GCC 4.7.x on Linux (only if native re-compile is required)
- MinGW on Windows (dito)
- XCode on MacOS (dito)
Currently there are only two branches, "master" and "gh-pages". Only "master" is relevant for the code. "gh-pages" is a special branch used by GitHub for publishing the project site. Separate, new branches will probably be used in the future for developing new experimental stuff.
The project was developed using Gradle 1.2 and/or Maven 3.2.3. It can be built with either Gradle or Maven, just to make it accessible to more developers. Just check out the project using GIT and build it with either one of these commands:
$ gradle build
or
$ mvn install
The native libraries should not need any more changes. If they do, see below.
For the desktop support, this API uses native libraries, wrapped with JNI. In order to re-compile the native code, the proper environment is required for each platform. The first releases were built with:
- Build scripts are included in "gamepad4j-desktop/src/build/"
- Compile / link with GCC on Linux and Windows. Version 4.7.3 was used on Linux
- Use MinGW on Windows for the GCC build (only minimal installation required)
- On MacOS, the Apple XCode SDK is required.
You will have to adapt some environment variables in the corresponding build scripts, according to your environment, before you can compile the code.
Once compiled, the native libraries have been copied into the resources folder. Just rebuild the Jar file, and bob's your uncle.
image by pngwing.com