-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introducing game controller rumble in SDL builds #729
Conversation
There are a few points which probably will need some discussion:
|
Yeah, probably makes sense to put all the controller setup in the Also, SDL just got a HasRumble query... libsdl-org/SDL#4943 (Not that we can use it yet) ... and no PR would be complete without me pointing out that the indentation is looking a bit wonky 😄 |
32blit-sdl/Main.cpp
Outdated
|
||
SDL_GameController* gc = SDL_GameControllerOpen(n); | ||
|
||
if (gc != nullptr && SDL_GameControllerGetAttached(gc) == SDL_TRUE) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure open can be successful if the controller isn't connected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funnily, this is taken from some SDL Lib example. But I get your point, the second part of the if could probably be omited.
32blit-sdl/Input.hpp
Outdated
class System; | ||
|
||
struct game_controller |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be CamelCase I think. (Maybe even private to Input
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought about making it private as well. But since not all functions are within the Input class, it currently needs to be public.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And re CamelCase, nopes, see file.hpp (32blit main, not SDL part)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The struct in that file is CamelCase though? I know there's packed_image
, but I think that was missed back when everything was changed to CamelCase...
32blit-sdl/Main.cpp
Outdated
blit_input = new Input(blit_system); | ||
if(!blit_input->game_controllers.empty()) { | ||
|
||
SDL_GameControllerEventState(SDL_ENABLE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, enabled should be the default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taken from an example as well. Not sure it always is. Documentation is not 100% clear on that. Would leave this as it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure that there are only a few SDL events that are disabled by default, and they're not controller events... also it worked without that before.
32blit-sdl/Main.cpp
Outdated
SDL_GameControllerClose(gc.gc_id); | ||
} | ||
blit_input->game_controllers.clear(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say that should be in Input
's destructor... but it's currently not getting delete
d. Something else to fix I guess...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. Leaving there until fixed, or moving to the Input class and calling it from here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd just stick a delete with the other three below, fixing the (tiny) leak too. (Not that it matters much as we're exiting anyway...)
32blit-sdl/System.cpp
Outdated
@@ -269,6 +272,16 @@ void System::loop() | |||
blit::joystick.y = shadow_joystick[1]; | |||
SDL_UnlockMutex(m_input); | |||
blit::tick(::now()); | |||
|
|||
if(blit::vibration > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this mean you can't turn vibration off? Though the duration would make it auto-disable anyway... This block could be an update_rumble
method on Input
or something similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nopes, no need to. The code only rumbles for 20ms max anyway. So it only rumbles until the next cycle. And then either is restarted for another 20ms or silenced automatically.
re Input class. OK, I am moving the code into the Input class. re duration. Not sure I get what you mean. But that is what is happening now. Rumble is set for 20ms which is one cycle. And is then restarted for another 20ms if still asked for by the game. No need to stop, you won't have any chance to cancel those 20ms anyway.
Thanks for the hint. The documentation regarding controllers is a bit difficult btw.
Must be the problem with the diff between me writing and me moving the changes into another repository... Looking into it, but I hope you can life with it anyway. |
For the duration I was mostly pointing out that setting the rumble again before the duration is finished is okay, because SDL itself does that to stop after the duration is finished. (It might be a good idea to set a duration slightly higher than the update interval, since there's no guarantee that the 20ms timer is actually 20ms.) For the indentation, it's apparently supposed to be 2 spaces (https://github.com/32blit/32blit-sdk/blob/master/.editorconfig#L11-L12). But the SDL code is mostly tabs... Though the new code here is neither of those. |
What should I take as duration? 50ms? Not noticeable by the user, but long enough for the cycle duration? |
Something like that yeah, enough to cover any timing variations but doesn't need to be too big. |
Hopefully I addressed all points:
|
Well, it works... when you use a controller that actually has rumble 🤦 (turns out most of the controllers I have lying around don't...) Don't see anything non-trivial to complain about so I'll just ignore any little formatting things and leave the rest to someone who can press the merge button 😄 |
Thanks @Daft-Freak for your support! |
|
||
void Input::_add_controller(SDL_GameController* gc) { | ||
// welcome rumble to test if it can rumble | ||
auto can_rumble = SDL_GameControllerRumble(gc, 0xFFFF, 0xFFFF, 200); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't actually want the welcome rumble, you can probe for rumble functionality using short low-intensity rumble like SDL_GameControllerRumble(gc, 1, 1, 1)
for versions of SDL that lack SDL_GameControllerHasRumble()
. It's so short that the rumble motors won't even have time to spin up.
What's the status on this? I'd love to merge it for the next release. |
You should be good to go. Just added the changes to the 32blit dev version yesterday. Although: There is a "welcome rumble" which shortly rumbles the controller when being initialised. You should reduce the duration to 1 if you do not want that to be noticed by the users. I find it quite useful since you know things work because of the welcome greeting. But YMMV. |
It does seem like a helpful debugging thing. I'd probably add a command-line switch to enable/disable it in future. |
And... merged! Thank you all involved. |
This is a first shot at having the game controllers which can rumble react to blit::vibration in the SDL port.