SDL_Pollevent Issue

Hi Guys,

I’m using SDL with Qt to get some joystick input but i can’t seem to get my data input if i’m using the SDL_PollEvent function, while when using SDL_WaitEvent everything seem to be working perfectly.Have a look at my sample code below:
This is my first timeposting here so if there’s anything i’m missing please tell me.

QList<int> Rjoystick::datAxis(int i)

{
QList axis{0,0,0,0};

m_joystick = SDL_JoystickOpen(0);
SDL_JoystickEventState(SDL_ENABLE);
SDL_Event e;

while(SDL_PollEvent(&e)){
switch (e.type) {
case SDL_JOYAXISMOTION:
axis.insert(e.jaxis.axis,e.jaxis.value);

      qDebug()<<axis[4];


  break;

default:
    break;

return axis;

}

}

SDL_JoystickClose(m_joystick);

}

Hi and welcome to the forum!

Just to make sure that SDL is working properly and that your joystick is recognized by your computer and by SDL etc, set a breakpoint inside your SDL_JOYAXISMOTION switch case, move a stick on your joystick and then check that the program breaks/pauses there.
If the program isn’t breaking/pausing at that point, there’s some issue with your joystick and/or your joystick isn’t recognized/initalized properly by SDL.

Hi , thank you for the reply.
I checked and everything seem good. The joystick is recognised by SDL and the computer.
I have just realised that the problem come from the condition loop. : while(SDL_PollEvent(&e));
Its seems this condition never become true(1).
I have checked the SDL doc about SDL_PollEvent and its state that its return 1 if there is a event pending and 0 when when there’s none.
Interesting enough if i change the while loop to :while(1) and move the SDL_PollEvent inside the loop it is working but then my program get stuck in the while loop and doesn’t carry on.

Paste your complete code in a post here, or upload it to a github or similar, and I can check it out.

Hi, here is my code for the Joystick handling :
`#include “joystick.h”

Rjoystick::Rjoystick()
{
SDL_SetMainReady();
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
{
QMessageBox::critical(this,“Error”,“SDL couldn’t be initialised in this case”,QMessageBox::Ok,QMessageBox::Cancel);

//makes sure sdl is well initialised

}

}

int Rjoystick::numbersJoystick() // return the number of joystick on the system
{

return SDL_NumJoysticks();

}

QList Rjoystick::connectedJoysticks() // return a qlist of the connected joystick, first joystick name correspond to index 0 and so on
{
QList a;

for(int i= 0; i < numbersJoystick();i++)
{
m_joystick = SDL_JoystickOpen(i);
a.append(SDL_JoystickName(m_joystick));
SDL_JoystickClose(m_joystick);

}
return a;
}

QList Rjoystick::datAxis(int i)
{
QList axis{0,0,0,0};

m_joystick = SDL_JoystickOpen(0);
SDL_JoystickEventState(SDL_ENABLE);
SDL_Event e;





while(SDL_PollEvent(&e)){

switch (e.type) {
case SDL_JOYAXISMOTION:
axis.insert(e.jaxis.axis,e.jaxis.value);

    qDebug()<<axis[4];
      //qDebug() <<SDL_PollEvent(&e);
      break;

default:
    break;

}

}
SDL_JoystickClose(m_joystick);

return axis;

}

`

Yes, but I need your complete code base to get a good overview what you’re doing in your program, and in what order etc.

pro-tip: write your code blocks like

```c++
if(bla) {
    your = code(here);
}
```

then they become more readable here, my example would look like

if(bla) {
    your = code(here);
}





Based on the code you pasted first, I guess the problem is that you basically shutdown your program after the
while(SDL_PollEvent(&e)) {
 // ...
}

loop?

This is not how it’s supposed to be used - this while-loop will terminate all the time when out of events, you’re supposed to do it every frame, so the code should basically look like

bool keep_on_running = true;
while(keep_on_running) {
	SDL_Event e;
	while(SDL_PollEvent(&e)) {
		switch(e.type) {
		  case SDL_QUIT: // you get this if you close the window, for example
			keep_on_running = false;
			break;
			// ... and your other cases for input handling
		}
	}
	
	// now you've handled all the inputs for this frame
	
	// TODO: your draw code or whatever else you want to do each frame
	// ...

}

Hi, I did that, and the:

   while (keep_on_running) ;

loop is not on my main my program get stuck in there.
basically all my window rendering is done by Qt, all i’m using SDL for here is to get joytsick handling going.
Maybe i have to look for a way to put that loop into my main program so that it doesn’t get stuck.

Naith i will be looking into uploading it into github, there’s just too many files linked to that project and only this class here uses SDL.

@Daniel_Gibson: They can’t just loop forever in the function that gets the joystick axis data or wherever.

@T_f335: If you only check SDL_PollEvent() when your joystick axis function is called then there probably aren’t any events in the queue right then (dunno if events can time out in SDL).

Look into using SDL event callbacks (SDL_AddEventWatch() IIRC). Basically, SDL will call a function of yours when an event comes in, and if it’s a joystick event you can read all the joystick data and cache it, so when some other part of your program wants to know what the joystick X axis is or whatever, you’ll have it saved from the last event and won’t need to sit around pumping the event queue or waiting for an event to come in.

@sjr: I don’t know if they can, my assumption that this was the problem was mostly based on “it works with SDL_WaitEvent()” - which should indeed result in an endless loop that doesn’t return.
Maybe it’s not the ideal/proper solution here, but without having the full source (or enough for a proper context) it’s hard for us to give better advice.

@Daniel_Gibson: I’m assuming the reason the OP is looking into alternatives to SDL_WaitEvent() is because calling SDL_WaitEvent() locks up the whole app until an event comes in. Which is especially problematic when all the OP is using SDL for is joystick input.