Browse Source

Only pump events once per frame and process all currently pending events

If you continually poll for events it's possible that new events can come in while you're still processing the last one, delaying rendering. This is more likely with high update rate sensors.
Sam Lantinga 4 years ago
parent
commit
ce8261dd6d
2 changed files with 10 additions and 2 deletions
  1. 5 1
      test/testgamecontroller.c
  2. 5 1
      test/testsensor.c

+ 5 - 1
test/testgamecontroller.c

@@ -305,7 +305,11 @@ loop(void *arg)
     int i;
     int i;
     SDL_bool showing_front = SDL_TRUE;
     SDL_bool showing_front = SDL_TRUE;
 
 
-    while (SDL_PollEvent(&event)) {
+    /* Update to get the current event state */
+    SDL_PumpEvents();
+
+    /* Process all currently pending events */
+    while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1) {
         switch (event.type) {
         switch (event.type) {
         case SDL_CONTROLLERDEVICEADDED:
         case SDL_CONTROLLERDEVICEADDED:
             SDL_Log("Game controller device %d added.\n", (int) SDL_JoystickGetDeviceInstanceID(event.cdevice.which));
             SDL_Log("Game controller device %d added.\n", (int) SDL_JoystickGetDeviceInstanceID(event.cdevice.which));

+ 5 - 1
test/testsensor.c

@@ -95,7 +95,11 @@ main(int argc, char **argv)
 
 
         SDL_CreateWindow("Sensor Test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
         SDL_CreateWindow("Sensor Test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
         while (!done) {
         while (!done) {
-            while (SDL_PollEvent(&event) > 0) {
+            /* Update to get the current event state */
+            SDL_PumpEvents();
+
+            /* Process all currently pending events */
+            while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1) {
                 switch (event.type) {
                 switch (event.type) {
                 case SDL_SENSORUPDATE:
                 case SDL_SENSORUPDATE:
                     HandleSensorEvent(&event.sensor);
                     HandleSensorEvent(&event.sensor);