Author Topic: Improve gamepad support  (Read 51 times)

duffy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Improve gamepad support
« on: December 06, 2017, 04:40:40 pm »
Gamepad can be better handled by using SDL_gamecontroller instead of directly SDL_joystick API.

Pros :
  • Mapped buttons with "real" name
  • No need to remap buttons while switching controller
  • A default mapping can be hard coded (and why not overrideable with an engine settings file)
Cons :

  • If the controller is not known, it must be added "manually" before it can be used (which is not the case with SDL_joystick)
What I propose to do :

  • Replace SDL_joystick by SDL_gamecontroller
  • Add a hard coded default mapping

    A -> action
    B -> attack
    X -> item_1
    Y -> item_2
    start -> "pause"

    Map both left joystick (axis) and dpad (hat) to directions.

    Maybe deprecate Lua axis/hat functions (sol.input..._joypad_..., game:on_joypad_..., ...) and add something like ..._joypad_direction(direction) or use existing ...joypad_button_..  functions ?
  • Load game controller (SDL) mappings from a file E.g : Solarus_save_path/gamecontrollerdb.txt to let user add gamepad mappings if needed.
    // see SDL_GameControllerDB
    // Several gamepad are already mapped in SDL 2.0.7 (I don't know for previous version) : http://hg.libsdl.org/SDL/file/2088cd828335/src/joystick/SDL_gamecontrollerdb.h
  • Try to create a default (SDL) mapping if the controller is not supported
  • Handle joystick added/removed event.
    on added : If there is no controller or this controller was used before, use it.
    on removed : "do nothing" (I mean we don't pick the next one).
  • Convert "button X" from old save game with new mapping
  • Add haptic rumble in Lua API

    bool is_joypad_rumble_enabled()
    void set_joypad_rumble_enabled([bool enabled])
    void joypad_rumble_play(float strength, int duration) // strength [0-1], duration in ms
    void joypad_rumble_stop()

    A function for periodic effect  can be nice too for boss/cinematic/...
It should not break backward compatibility and be transparent from the user point of view except if the gamepad is not supported.


One future improvement which can be nice is to deport saved mapping from save game to an engine settings file + an engine menu and save mapping by controller.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Improve gamepad support
« Reply #1 on: December 06, 2017, 04:51:10 pm »
Looks great. It is annoying that we can't guess correct buttons automatically with the current solarus.
A pull request will be welcome :)

I think joystick added/removed events and a haptic rumble API are other subjects (new features) and should be developed in separate works later.
« Last Edit: December 06, 2017, 04:57:32 pm by Christopho »

darknior

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Improve gamepad support
« Reply #2 on: December 06, 2017, 04:54:22 pm »
I love the idea of GENERIC joystick mapping :)
And the RUMBLE with x360 gamepad on PC or PI is really excellent to use <3

duffy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Improve gamepad support
« Reply #3 on: December 06, 2017, 05:03:22 pm »
You are right about haptic rumble (I'm not sure about added/removed event but why not).
I will post here when it's done before a PR if there are some stuff that you want me to change and because it's maybe better to PR  this on a "joystick" branch (of course I can't create a branch).

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Improve gamepad support
« Reply #4 on: December 06, 2017, 05:22:34 pm »
Don't worry about that, you can make a PR to the dev branch and if I want to put it in a new branch I can still do it.