Coder Social home page Coder Social logo

sdl2-gamecontroller's Introduction

CII Best Practices

sdl2-gamecontroller

Game controller support for nodejs EventEmitter backed by SDL2.

Description

Do you want to use a game controller with your node app? You should first ask yourself โ€œwhyโ€ and you should examine your life choices that have brought you to this point.

If you do find you need to use a game controller with a node app, you are in the right place. I used SDL (Simple DirectMedia Layer) to power the controller because, in my experience, SDL just works. The most recent version has support for the DualSense, but it also has support for every other controller I have tried so far. The node addon is a thin wrapper around the SDL event loop and the SDL functions for rumble and lights.

Article on LinkedIn

Installation

Install sdl2 and cmake. See prerequisites

yarn add sdl2-gamecontroller
npm install sdl2-gamecontroller

Feedback

Found a bug or just what to let me know what you think. Open an issue on the issues tab or leave a comment

Using the library

Hello world

const gamecontroller = require("sdl2-gamecontroller");

gamecontroller.on("error", (data) => console.log("error", data));
gamecontroller.on("warning", (data) => console.log("warning", data));
gamecontroller.on("sdl-init", () => console.log("SDL2 Initialized"));
gamecontroller.on("a:down", (data) => console.log("Hello A button world"));

Hello world with custom options

const gamecontroller = require('sdl2-gamecontroller/custom')({sdl_joystick_rog_chakram: true, fps: 25})

gamecontroller.on('error', (data) => console.log('error', data));
gamecontroller.on('warning', (data) => console.log('warning', data));
gamecontroller.on('sdl-init', (data) => console.log('SDL2 Initialized', data));
gamecontroller.on('a:down', () => console.log('Hello A button world'));
gamecontroller.on('controller-device-added', (data) =>
  console.log('controller connected', data.name),
);
gamecontroller.on('x:down', () => process.exit(0));

Lengthy example

const gamecontroller = require("sdl2-gamecontroller");

gamecontroller.on("error", (data) => console.log("error", data));
gamecontroller.on("warning", (data) => console.log("warning", data));
gamecontroller.on("sdl-init", () => console.log("SDL2 Initialized"));

// controller connected
gamecontroller.on("controller-device-added", (data) =>
  console.log("controller connected", data)
);

// Rumble (if supported) when A button is pressed
gamecontroller.on("a:down", (data) => {
  console.log(`player ${data.player} pressed A`);
  gamecontroller.rumble(60000, 40000, 100, data.player);
});

// Rumble triggers (if supported) when B button is pressed
gamecontroller.on("b:down", (data) => {
  console.log(`player ${data.player} pressed B`);
  gamecontroller.rumbleTriggers(40000, 40000, 100, data.player);
});

// Enable Gyroscope (if supported) when X button is pressed
gamecontroller.on("x:down", (data) => {
  console.log(`player ${data.player} pressed X`);
  gamecontroller.enableGyroscope(true);
});

// Disable Gyroscope (if supported) when X button is released
gamecontroller.on("x:up", (data) => {
  console.log(`player ${data.player} released X`);
  gamecontroller.enableGyroscope(false);
});

// Enable Accelerometer (if supported) when Y button is pressed
gamecontroller.on("y:down", (data) => {
  console.log(`player ${data.player} pressed Y`);
  gamecontroller.enableAccelerometer(true, data.player);
});

// Disable Accelerometer (if supported) when Y button is released
gamecontroller.on("y:up", (data) => {
  console.log(`player ${data.player} released Y`);
  gamecontroller.enableAccelerometer(false, data.player);
});

// Set LED colors (if supported) when leftstick button is pressed
var count = 0;
gamecontroller.on("leftstick:down", (data) => {
  console.log(`player ${data.player} pressed leftstick`);
  switch (count % 3) {
    case 0:
      gamecontroller.setLeds(0x0f, 0x62, 0xfe, data.player);
      break;
    case 1:
      gamecontroller.setLeds(0x19, 0x80, 0x38, data.player);
      break;
    case 2:
      gamecontroller.setLeds(0x8a, 0x3f, 0xfc, data.player);
      break;
  }
  count += 1;
});

// Axis motion
gamecontroller.on("controller-axis-motion", (data) =>
  console.log("axis motion", data)
);

// Sensor updated
gamecontroller.on("controller-sensor-update", (data) =>
  console.log("sensor update", data)
);

// Respond to both up & down events
gamecontroller.on("leftshoulder", (data) =>
  console.log(`player ${data.player} pressed:${data.pressed} leftshoulder`)
);

// Print information about a pressed button
gamecontroller.on("controller-button-down", (data) =>
  console.log("button pressed", data)
);

API Docs

Full docs for the events and the functions calls API doc

Prerequisites

NOTE: If you need rumble, LED, touchpad, gyroscope, or accelerometer support, you need SDL 2.0.16+ which seems not to be available form the Ubuntu/Red Hat repos.

Linux Red Hat

sudo dnf install -y gcc-c++ cmake make
# You may need to enable the powertool repo
# dnf -y install dnf-plugins-core
# dnf config-manager --set-enabled powertools
sudo dnf install -y SDL2-devel

Linux Ubuntu

sudo apt install -y build-essential cmake libsdl2-dev

OSX

install Homebrew

xcode-select --install
brew install cmake sdl2

Linux - Latest SDL on Linux from Homebrew

install Homebrew install git dnf install -y git or apt install git

brew install sdl2

Lastest SDL from source (OPTIONAL)

The version available from Homebrew is generally very recent but if you are dissatisfied with the version, you can install from source with the instructions on the SDL2 wiki.

Windows

I do not know if this works on Windows. If you get this working, please open a PR for changes to this doc and the code.

sdl2-gamecontroller's People

Contributors

davidnixon avatar dependabot[bot] avatar maaasyn avatar stevemar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sdl2-gamecontroller's Issues

function rumble and rumbleTriggers is not always executed on Xbox controller

Steps to reproduce:
Run the following lines with connected Xbox Series X Wireless controller on macOS:

Gamepad.on('a:down', data => {
  Gamepad.rumbleTriggers(40000, 40000, 100, data.player);
});
Gamepad.on('b:down', data => {
  Gamepad.rumble(60000, 40000, 100, data.player);
});

Expected result:
When pressing button a: Xbox controller shoulder triggers rumble every time button is pressed
When pressing button b: Whole Xbox controller rumble every time button is pressed

Actual result:
When pressing button a: Xbox controller shoulder triggers rumble only 3 out of 10 times button is pressed
When pressing button b: Whole Xbox controller rumble only 3 out of 10 times button is pressed

Dualshock3 (sixaxis) A B X Y errors

I'll test your node with tour lenghty example on linux with a dualshock3 sixaxis controller:
All works except A,B, X and Y buttons throwing error ๐Ÿ‘๐Ÿฟ

player undefined pressed A
/home/dewi/Documents/GitHub/sdl2/node_modules/sdl2-gamecontroller/index.js:12
  gamecontroller.pollEvents();
                 ^

Error: Wrong arguments
    at SdlGameController.<anonymous> (/home/dewi/Documents/GitHub/sdl2/index.js:15:18)
    at SdlGameController.emit (events.js:315:20)
    at Timeout._onTimeout (/home/dewi/Documents/GitHub/sdl2/node_modules/sdl2-gamecontroller/index.js:12:18)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

windows support

Definitely not working on windows. I think there's something to do in CMakeLists.txt file to link SDL2 library (maybe put SDL2 library in a specific folder) during build process but I don't know exactly what?:

Could not find a package configuration file provided by "SDL2" with any of
  the following names:

    SDL2Config.cmake
    sdl2-config.cmake

  Add the installation prefix of "SDL2" to CMAKE_PREFIX_PATH or set
  "SDL2_DIR" to a directory containing one of the above files.  If "SDL2"
  provides a separate development package or SDK, be sure it has been
  installed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.