SDL_sysmain_callbacks.c 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "../SDL_main_callbacks.h"
  20. #include "../../video/SDL_sysvideo.h"
  21. #ifndef SDL_PLATFORM_IOS
  22. static int callback_rate_increment = 0;
  23. static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name, const char *oldValue, const char *newValue)
  24. {
  25. const int callback_rate = newValue ? SDL_atoi(newValue) : 60;
  26. if (callback_rate > 0) {
  27. callback_rate_increment = ((Uint64) 1000000000) / ((Uint64) callback_rate);
  28. } else {
  29. callback_rate_increment = 0;
  30. }
  31. }
  32. int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
  33. {
  34. int rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
  35. if (rc == 0) {
  36. SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
  37. Uint64 next_iteration = callback_rate_increment ? (SDL_GetTicksNS() + callback_rate_increment) : 0;
  38. while ((rc = SDL_IterateMainCallbacks(SDL_TRUE)) == 0) {
  39. // !!! FIXME: this can be made more complicated if we decide to
  40. // !!! FIXME: optionally hand off callback responsibility to the
  41. // !!! FIXME: video subsystem (for example, if Wayland has a
  42. // !!! FIXME: protocol to drive an animation loop, maybe we hand
  43. // !!! FIXME: off to them here if/when the video subsystem becomes
  44. // !!! FIXME: initialized).
  45. // !!! FIXME: maybe respect this hint even if there _is_ a window.
  46. // if there's no window, try to run at about 60fps (or whatever rate
  47. // the hint requested). This makes this not eat all the CPU in
  48. // simple things like loopwave. If there's a window, we run as fast
  49. // as possible, which means we'll clamp to vsync in common cases,
  50. // and won't be restrained to vsync if the app is doing a benchmark
  51. // or doesn't want to be, based on how they've set up that window.
  52. if ((callback_rate_increment == 0) || SDL_HasWindows()) {
  53. next_iteration = 0; // just clear the timer and run at the pace the video subsystem allows.
  54. } else {
  55. const Uint64 now = SDL_GetTicksNS();
  56. if (next_iteration > now) { // Running faster than the limit, sleep a little.
  57. SDL_DelayNS(next_iteration - now);
  58. } else {
  59. next_iteration = now; // running behind (or just lost the window)...reset the timer.
  60. }
  61. next_iteration += callback_rate_increment;
  62. }
  63. }
  64. SDL_DelHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL);
  65. }
  66. SDL_QuitMainCallbacks();
  67. return (rc < 0) ? 1 : 0;
  68. }
  69. #endif // !SDL_PLATFORM_IOS