SDL2_gfx
1.0.1
GraphicsprimitivesandsurfacefunctionsforSDL2
|
00001 /* 00002 00003 SDL_framerate.c: framerate manager 00004 00005 Copyright (C) 2001-2012 Andreas Schiffler 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for any damages 00009 arising from the use of this software. 00010 00011 Permission is granted to anyone to use this software for any purpose, 00012 including commercial applications, and to alter it and redistribute it 00013 freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must not 00016 claim that you wrote the original software. If you use this software 00017 in a product, an acknowledgment in the product documentation would be 00018 appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must not be 00021 misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any source 00024 distribution. 00025 00026 Andreas Schiffler -- aschiffler at ferzkopp dot net 00027 00028 */ 00029 00030 #include "SDL2_framerate.h" 00031 00037 Uint32 _getTicks() 00038 { 00039 Uint32 ticks = SDL_GetTicks(); 00040 00041 /* 00042 * Since baseticks!=0 is used to track initialization 00043 * we need to ensure that the tick count is always >0 00044 * since SDL_GetTicks may not have incremented yet and 00045 * return 0 depending on the timing of the calls. 00046 */ 00047 if (ticks == 0) { 00048 return 1; 00049 } else { 00050 return ticks; 00051 } 00052 } 00053 00062 void SDL_initFramerate(FPSmanager * manager) 00063 { 00064 /* 00065 * Store some sane values 00066 */ 00067 manager->framecount = 0; 00068 manager->rate = FPS_DEFAULT; 00069 manager->rateticks = (1000.0f / (float) FPS_DEFAULT); 00070 manager->baseticks = _getTicks(); 00071 manager->lastticks = manager->baseticks; 00072 00073 } 00074 00086 int SDL_setFramerate(FPSmanager * manager, Uint32 rate) 00087 { 00088 if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) { 00089 manager->framecount = 0; 00090 manager->rate = rate; 00091 manager->rateticks = (1000.0f / (float) rate); 00092 return (0); 00093 } else { 00094 return (-1); 00095 } 00096 } 00097 00107 int SDL_getFramerate(FPSmanager * manager) 00108 { 00109 if (manager == NULL) { 00110 return (-1); 00111 } else { 00112 return ((int)manager->rate); 00113 } 00114 } 00115 00126 int SDL_getFramecount(FPSmanager * manager) 00127 { 00128 if (manager == NULL) { 00129 return (-1); 00130 } else { 00131 return ((int)manager->framecount); 00132 } 00133 } 00134 00146 Uint32 SDL_framerateDelay(FPSmanager * manager) 00147 { 00148 Uint32 current_ticks; 00149 Uint32 target_ticks; 00150 Uint32 the_delay; 00151 Uint32 time_passed = 0; 00152 00153 /* 00154 * No manager, no delay 00155 */ 00156 if (manager == NULL) { 00157 return 0; 00158 } 00159 00160 /* 00161 * Initialize uninitialized manager 00162 */ 00163 if (manager->baseticks == 0) { 00164 SDL_initFramerate(manager); 00165 } 00166 00167 /* 00168 * Next frame 00169 */ 00170 manager->framecount++; 00171 00172 /* 00173 * Get/calc ticks 00174 */ 00175 current_ticks = _getTicks(); 00176 time_passed = current_ticks - manager->lastticks; 00177 manager->lastticks = current_ticks; 00178 target_ticks = manager->baseticks + (Uint32) ((float) manager->framecount * manager->rateticks); 00179 00180 if (current_ticks <= target_ticks) { 00181 the_delay = target_ticks - current_ticks; 00182 SDL_Delay(the_delay); 00183 } else { 00184 manager->framecount = 0; 00185 manager->baseticks = _getTicks(); 00186 } 00187 00188 return time_passed; 00189 }