Created
June 1, 2017 11:31
-
-
Save Alynva/4b326757b65df8d26cb3f56e608bc8b4 to your computer and use it in GitHub Desktop.
Um código simples pra testar o framerate no SDL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "SDL2/SDL.h" | |
| #include <stdio.h> | |
| // How many frames time values to keep | |
| // The higher the value the smoother the result is... | |
| // Don't make it 0 or less :) | |
| #define FRAME_VALUES 10 | |
| // An array to store frame times: | |
| Uint32 frametimes[FRAME_VALUES]; | |
| // Last calculated SDL_GetTicks | |
| Uint32 frametimelast; | |
| // total frames rendered | |
| Uint32 framecount; | |
| // the value you want | |
| float framespersecond; | |
| // This function gets called once on startup. | |
| void fpsinit() { | |
| // Set all frame times to 0ms. | |
| for (int i = 0; i < FRAME_VALUES; i++) | |
| frametimes[i] = 0; | |
| framecount = 0; | |
| framespersecond = 0; | |
| frametimelast = SDL_GetTicks(); | |
| } | |
| void fpsthink() { | |
| Uint32 frametimesindex; | |
| Uint32 getticks; | |
| Uint32 count; | |
| Uint32 i; | |
| // frametimesindex is the position in the array. It ranges from 0 to FRAME_VALUES. | |
| // This value rotates back to 0 after it hits FRAME_VALUES. | |
| frametimesindex = framecount % FRAME_VALUES; | |
| // store the current time | |
| getticks = SDL_GetTicks(); | |
| // save the frame time value | |
| frametimes[frametimesindex] = getticks - frametimelast; | |
| // save the last frame time for the next fpsthink | |
| frametimelast = getticks; | |
| // increment the frame count | |
| framecount++; | |
| // Work out the current framerate | |
| // The code below could be moved into another function if you don't need the value every frame. | |
| // I've included a test to see if the whole array has been written to or not. This will stop | |
| // strange values on the first few (FRAME_VALUES) frames. | |
| if (framecount < FRAME_VALUES) { | |
| count = framecount; | |
| } else { | |
| count = FRAME_VALUES; | |
| } | |
| // add up all the values and divide to get the average frame time. | |
| framespersecond = 0; | |
| for (i = 0; i < count; i++) { | |
| framespersecond += frametimes[i]; | |
| } | |
| framespersecond /= count; | |
| // now to make it an actual frames per second value... | |
| framespersecond = 1000.f / framespersecond; | |
| } | |
| int main(int argc, char** argv) { | |
| SDL_Init(0); | |
| // put this as close as possible to the start of the loop (before it starts!) | |
| fpsinit(); | |
| while (1) { | |
| // render stuff here | |
| // draw(); | |
| SDL_Delay(500); // 500 should make 2 frames per second. | |
| fpsthink(); | |
| printf("%f\n", framespersecond); | |
| } | |
| SDL_Quit(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment