SDL_gfx: SDL_framerate.c Source File

- Main Page
- Data Structures
- Files

- File List
- Globals

# SDL_framerate.c

Go to the documentation of this file.

    00001 /*
    00002
    00003 SDL_framerate: framerate manager
    00004
    00005 LGPL (c) A. Schiffler
    00006
    00007 */
    00008
    00009 #include "SDL_framerate.h"
    00010
    00019 void SDL_initFramerate(FPSmanager * manager)
    00020 {
    00021         /*
    00022         * Store some sane values
    00023         */
    00024         manager->framecount = 0;
    00025         manager->rate = FPS_DEFAULT;
    00026         manager->rateticks = (1000.0f / (float) FPS_DEFAULT);
    00027         manager->lastticks = SDL_GetTicks();
    00028 }
    00029
    00041 int SDL_setFramerate(FPSmanager * manager, int rate)
    00042 {
    00043         if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) {
    00044                 manager->framecount = 0;
    00045                 manager->rate = rate;
    00046                 manager->rateticks = (1000.0f / (float) rate);
    00047                 return (0);
    00048         } else {
    00049                 return (-1);
    00050         }
    00051 }
    00052
    00062 int SDL_getFramerate(FPSmanager * manager)
    00063 {
    00064         if (manager == NULL) {
    00065                 return (-1);
    00066         } else {
    00067                 return (manager->rate);
    00068         }
    00069 }
    00070
    00081 int SDL_getFramecount(FPSmanager * manager)
    00082 {
    00083         if (manager == NULL) {
    00084                 return (-1);
    00085         } else {
    00086                 return ((Uint32)manager->framecount);
    00087         }
    00088 }
    00089
    00099 void SDL_framerateDelay(FPSmanager * manager)
    00100 {
    00101         Uint32 current_ticks;
    00102         Uint32 target_ticks;
    00103         Uint32 the_delay;
    00104
    00105         /*
    00106         * No manager, no delay
    00107         */
    00108         if (manager == NULL)
    00109                 return;
    00110
    00111         /*
    00112         * Initialize uninitialized manager
    00113         */
    00114         if (manager->lastticks == 0)
    00115                 SDL_initFramerate(manager);
    00116
    00117         /*
    00118         * Next frame
    00119         */
    00120         manager->framecount++;
    00121
    00122         /*
    00123         * Get/calc ticks
    00124         */
    00125         current_ticks = SDL_GetTicks();
    00126         target_ticks = manager->lastticks + (Uint32) ((float) manager->framecount * manager->rateticks);
    00127
    00128         if (current_ticks <= target_ticks) {
    00129                 the_delay = target_ticks - current_ticks;
    00130                 SDL_Delay(the_delay);
    00131         } else {
    00132                 manager->framecount = 0;
    00133                 manager->lastticks = SDL_GetTicks();
    00134         }
    00135 }