Using SDL_INIT_JOYSTICK without SDL_INIT_VIDEO
Asked Answered
M

2

7

I've lost some time trying to get joystick support for my an application using SDL, mostly because the initialization steps where these:

SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_VIDEO);
SDL_JoystickEventState(SDL_ENABLE);

It didn't work if I just initialized the joystick, it needed to be done together with the video. I want this to be very minimalistic, so it would be nice to know if there's a way to initialize just the joystick. If not, can someone tell me if there's any disadvantage of initializing the video and never using it?

I'm on Ubuntu here, but I expect this to run in various platforms. Does this behavior vary in different environments?

Thanks :)

Manson answered 3/2, 2011 at 19:42 Comment(0)
R
9

I had similiar issues when developing a small CLI app that took joypad input. Basically, it didn't work without SDL_INIT_VIDEO because SDLs event system stems from the video driver, at least on Linux (X11).

It is perfectly fine to init video without ever creating a window (it works at least). I've tested this approach on both Win32 and Linux, so it does seem to work fine multiplatform as well.

Redeemable answered 3/2, 2011 at 22:50 Comment(1)
I am trying this on windows7 and it seems that this isn't true. Turning off SDL_CreateWindow makes event loop being triggerd by buttons but not analog sticks on Logitech Gamepad. With window created it works fine.Quemoy
I
6

I have a similar issue, I solved it by not using the event loop. Instead I manually update the joysticks and then use the SDL functions to check the joysticks.

SDL_Init(SDL_INIT_JOYSTICK);
SDL_JoystickEventState(SDL_DISABLE);
joystick = SDL_JoystickOpen(0);
SDL_Joystick* joystick;
while(true)
{
    SDL_JoystickUpdate();
    SDL_JoystickGetAxis(joystick, 0);
}
Immediately answered 5/2, 2014 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.