SDL2 gamepad input capture

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
User avatar
jordiperezarti
Halfling
Posts: 53
Joined: Sun Sep 01, 2024 7:50 pm

SDL2 gamepad input capture

Post by jordiperezarti »

Can someone tell me how i can capture the gamepad input using the SDL incorporated component in Ogre 14.4 ?

User avatar
jordiperezarti
Halfling
Posts: 53
Joined: Sun Sep 01, 2024 7:50 pm

Re: SDL2 gamepad input capture

Post by jordiperezarti »

i have implemented in

Code: Select all

OgreBites::InputListener

the

Code: Select all

axisMoved(evt)

I do

Code: Select all

if (evt.type == OgreBites::CONTROLLERAXISMOTION)

i capture the evt.value for the given evt.axis

But i have troubles when the gamepad is dragging or the gamepad has ended the move.
I loose the event when the gamepad is draging, and i never capture evt.value == 0.

any light here?

rpgplayerrobin
Bugbear
Posts: 803
Joined: Wed Mar 18, 2009 3:03 am
x 462

Re: SDL2 gamepad input capture

Post by rpgplayerrobin »

Try to poll it each frame instead of relying on callback events:
SDL_GameControllerGetAxis
https://wiki.libsdl.org/SDL2/SDL_GameControllerGetAxis

User avatar
jordiperezarti
Halfling
Posts: 53
Joined: Sun Sep 01, 2024 7:50 pm

Re: SDL2 gamepad input capture

Post by jordiperezarti »

Now runs very smooth and very well, thanks.

User avatar
jordiperezarti
Halfling
Posts: 53
Joined: Sun Sep 01, 2024 7:50 pm

Re: SDL2 gamepad input capture

Post by jordiperezarti »

The gamepad input capture works perfectly, but after a pair of minutes using it, the render stops and the program becomes bloqued.
i am doing:

Code: Select all

void CustomCameraMan::checkGamepadMotion()
{
    if (this->gamepad == nullptr)
        gamepad = SDL_GameControllerFromInstanceID(0);

const short leftAxisLx = SDL_GameControllerGetAxis(gamepad, SDL_CONTROLLER_AXIS_LEFTX);
const short leftAxisLy = SDL_GameControllerGetAxis(gamepad, SDL_CONTROLLER_AXIS_LEFTY);
const short leftAxisRx = SDL_GameControllerGetAxis(gamepad, SDL_CONTROLLER_AXIS_RIGHTX);
const short leftAxisRy = SDL_GameControllerGetAxis(gamepad, SDL_CONTROLLER_AXIS_RIGHTY);

Is needed some call to SD2 poll evends or some resource dispose function?

rpgplayerrobin
Bugbear
Posts: 803
Joined: Wed Mar 18, 2009 3:03 am
x 462

Re: SDL2 gamepad input capture

Post by rpgplayerrobin »

I have no idea why that happens, but first try some things yourself before we can help more in detail.

ChatGPT is a good friend, and it has some ideas: https://chatgpt.com/share/691c91b6-e2a8 ... b4b718a619

User avatar
jordiperezarti
Halfling
Posts: 53
Joined: Sun Sep 01, 2024 7:50 pm

Re: SDL2 gamepad input capture

Post by jordiperezarti »

Code: Select all

while (SDL_PollEvent(&e)){};

seems to clean the queue and keep the render loop floid.

rpgplayerrobin
Bugbear
Posts: 803
Joined: Wed Mar 18, 2009 3:03 am
x 462

Re: SDL2 gamepad input capture

Post by rpgplayerrobin »

There are a lot more things you need to handle if you have not done that loop previously.

That should already have happened if you used ApplicationContextSDL::pollEvents:

Code: Select all

void ApplicationContextSDL::pollEvents()
{
    if(mWindows.empty())
    {
        // SDL events not initialized
        return;
    }

SDL_Event event;
while (SDL_PollEvent(&event))
{
    switch (event.type)
    {
    case SDL_QUIT:
        mRoot->queueEndRendering();
        break;
    case SDL_WINDOWEVENT:
        if(event.window.event != SDL_WINDOWEVENT_RESIZED)
            continue;

        for(WindowList::iterator it = mWindows.begin(); it != mWindows.end(); ++it)
        {
            if(event.window.windowID != SDL_GetWindowID(it->native))
                continue;

            Ogre::RenderWindow* win = it->render;
            win->resize(event.window.data1, event.window.data2);
            windowResized(win);
        }
        break;
    case SDL_CONTROLLERDEVICEADDED:
        if(auto c = SDL_GameControllerOpen(event.cdevice.which))
        {
            const char* name = SDL_GameControllerName(c);
            Ogre::LogManager::getSingleton().stream() << "Opened Gamepad: " << (name ? name : "unnamed");
        }
        break;
    default:
        _fireInputEvent(convert(event), event.window.windowID);
        break;
    }
}
}