Page 1 of 1

SDL2 gamepad input capture

Posted: Tue Nov 11, 2025 8:45 pm
by jordiperezarti

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


Re: SDL2 gamepad input capture

Posted: Sat Nov 15, 2025 2:13 pm
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?


Re: SDL2 gamepad input capture

Posted: Sat Nov 15, 2025 6:03 pm
by rpgplayerrobin

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


Re: SDL2 gamepad input capture

Posted: Sat Nov 15, 2025 8:26 pm
by jordiperezarti

Now runs very smooth and very well, thanks.


Re: SDL2 gamepad input capture

Posted: Tue Nov 18, 2025 3:32 pm
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?


Re: SDL2 gamepad input capture

Posted: Tue Nov 18, 2025 4:34 pm
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


Re: SDL2 gamepad input capture

Posted: Sun Nov 30, 2025 6:48 pm
by jordiperezarti

Code: Select all

while (SDL_PollEvent(&e)){};

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


Re: SDL2 gamepad input capture

Posted: Mon Dec 01, 2025 1:36 am
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;
    }
}
}