I was wonder what the best(fastest) way to get(or poll) joysticks event without having a loop.
I am write a simple GUI program to test joysticks and the image below is what i have so far(started a few hours ago).
But my issue is if i used SDL_PollEvent(SDL_Event* event) i will need a loop, and if i use SDL_WaitEvent my program will stop.
So how can i go around this ? I was thinking of using SDL_AddEventWatch or even run the joystick events on a different thread Any Ideas ? Thank you
Because i want the program to carry on and not stay in the loop for the time i am using the joystick. and all the approaches, i tried so far will eventually get the program stopped.
Your program can run normally - because that while-loop frequently runs out of current events and exits, so afterwards your normal program can run on.
This is what games do every frame - and event handling only takes a tiny amount of time per frame, even at 120Hz, so you shouldn’t have to worry that your application becomes unresponsive.
I understand what you are saying. Now i am having troubling implementing it.
Should i make another function and call it x amount of time i need the joystick inputs ?
PS. Since this is not a game there’s not main loop.
for example:
void Joystick::JoystickInputs(int index)
{
//make sure the joystick is opened and ready
if((myJoystick = SDL_JoystickOpen(index)))
{
//Enable the joystick event state
SDL_JoystickEventState(SDL_ENABLE);
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
case SDL_JOYAXISMOTION:
if(event.jaxis.value < -3200 || event.jaxis.value > 3200)
{
if (event.jaxis.axis == 0) //left to right
{
qDebug() << event.jaxis.value;
}
if(event.jaxis.axis == 1) //up and down
{
}
}
}
}
//close the current opened joystick
SDL_JoystickClose(myJoystick);
}
}