Atlas - SDL_sysmain_callbacks.c

Home / ext / SDL / src / main / generic Lines: 1 | Size: 4208 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22#include "SDL_internal.h" 23#include "../SDL_main_callbacks.h" 24#include "../../video/SDL_sysvideo.h" 25 26#ifndef SDL_PLATFORM_IOS 27 28static Uint64 callback_rate_increment = 0; 29static bool iterate_after_waitevent = false; 30 31static void SDLCALL MainCallbackRateHintChanged(void *userdata, const char *name, const char *oldValue, const char *newValue) 32{ 33 iterate_after_waitevent = newValue && (SDL_strcmp(newValue, "waitevent") == 0); 34 if (iterate_after_waitevent) { 35 callback_rate_increment = 0; 36 } else { 37 const double callback_rate = newValue ? SDL_atof(newValue) : 0.0; 38 if (callback_rate > 0.0) { 39 callback_rate_increment = (Uint64) ((double) SDL_NS_PER_SECOND / callback_rate); 40 } else { 41 callback_rate_increment = 0; 42 } 43 } 44} 45 46static SDL_AppResult GenericIterateMainCallbacks(void) 47{ 48 if (iterate_after_waitevent) { 49 SDL_WaitEvent(NULL); 50 } 51 return SDL_IterateMainCallbacks(!iterate_after_waitevent); 52} 53 54int SDL_EnterAppMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit) 55{ 56 SDL_AppResult rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit); 57 if (rc == SDL_APP_CONTINUE) { 58 SDL_AddHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL); 59 60 Uint64 next_iteration = callback_rate_increment ? (SDL_GetTicksNS() + callback_rate_increment) : 0; 61 62 while ((rc = GenericIterateMainCallbacks()) == SDL_APP_CONTINUE) { 63 // !!! FIXME: this can be made more complicated if we decide to 64 // !!! FIXME: optionally hand off callback responsibility to the 65 // !!! FIXME: video subsystem (for example, if Wayland has a 66 // !!! FIXME: protocol to drive an animation loop, maybe we hand 67 // !!! FIXME: off to them here if/when the video subsystem becomes 68 // !!! FIXME: initialized). 69 70 // Try to run at whatever rate the hint requested. This makes this 71 // not eat all the CPU in simple things like loopwave. By 72 // default, we run as fast as possible, which means we'll clamp to 73 // vsync in common cases, and won't be restrained to vsync if the 74 // app is doing a benchmark or doesn't want to be, based on how 75 // they've set up that window. 76 if (callback_rate_increment == 0) { 77 next_iteration = 0; // just clear the timer and run at the pace the video subsystem allows. 78 } else { 79 const Uint64 now = SDL_GetTicksNS(); 80 if (next_iteration > now) { // Running faster than the limit, sleep a little. 81 SDL_DelayPrecise(next_iteration - now); 82 } else { 83 next_iteration = now; // if running behind, reset the timer. If right on time, `next_iteration` already equals `now`. 84 } 85 next_iteration += callback_rate_increment; 86 } 87 } 88 89 SDL_RemoveHintCallback(SDL_HINT_MAIN_CALLBACK_RATE, MainCallbackRateHintChanged, NULL); 90 } 91 SDL_QuitMainCallbacks(rc); 92 93 return (rc == SDL_APP_FAILURE) ? 1 : 0; 94} 95 96#endif // !SDL_PLATFORM_IOS 97
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.