[SDL] FPS is extremely high

Started by
5 comments, last by speciesUnknown 16 years, 2 months ago
When I start the application it has an FPS of 1,000,000,000 It keeps going down every second, and finnaly hangs around 10,000,000-20,000,000! The only thing I draw is an SDL surface and some input checking, the FPS count works like this:
int frames, fps, update;
fps = SDL_GetTicks();
update = SDL_GetTicks();

bool done = false;
while (!done) // the main loop, every loop redraws the scene
{
    // input checking and scene drawing
    if ((SDL_GetTicks() - update) > 1000)
    {
        stringstream caption;
        caption << frames / ((SDL_GetTicks() - fps) / 1000);
        SDL_WM_SetCaption(caption.str().c_str(), NULL);
        update = SDL_GetTicks();
    }
    frames++;

    SDL_Delay(10);
}
Is that normal? Because when I had less functionality the FPS was lower (around 10,000, and when I started the project it was ~80 (perhaps I did a lot of time consuming stuff at that stage that weren't needed though)). I even use the delay function, which is to keep the CPU usage low. PS: it keeps falling, after a few minuts its at 5,000,000
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
You are calculating it wrong. Here is how I would do that:

int frames=0, update;float fps;update = SDL_GetTicks();bool done = false;while (!done) // the main loop, every loop redraws the scene{    // input checking and scene drawing    if ((SDL_GetTicks() - update) > 1000)    {        stringstream caption;        fps = frames / (SDL_GetTicks()-update) * 1000;        caption << fps;        SDL_WM_SetCaption(caption.str().c_str(), NULL);        update = SDL_GetTicks();        frames = 0;    }    frames++;    SDL_Delay(10);}
Ah ok, thanks.

Its 86 now :), and 710 without the delay :)
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
And you also should use floats to calculate the fps! Even though fps is a float in fps = frames / (SDL_GetTicks()-update) * 1000; the calculation is not done with floats since frames and the SDL_GetTicks()... are both ints. You have to cast at leat one of them to float.
Thanks :), works perfect now :D
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Try this:

header file:
#ifndef class_fps_counter_h#define class_fps_counter_h 1#include <SDL/SDL.h>class fps_counter{private:        unsigned int app_ticks;//total ticks so far in this app        int frame_cap; //limit FPS to this         bool cap_frames; //do we need to?        float last_fps;        float last_timeslice;public:        fps_counter();        void set_cap(int fps);        void cap_on();        void cap_off();        void tick();        float get_fps(){return last_fps;};        float get_timeslice(){return last_timeslice;};};#endif


source file:
/* *  fps_counter.cpp *  worms * *  Created by Gavin Burton on 27/11/2007. *  Copyright 2007 __MyCompanyName__. All rights reserved. * */#include "fps_counter.h"fps_counter::fps_counter(){        frame_cap = 60;        cap_off();        last_fps=0;        last_timeslice=1;};void fps_counter::tick(){        float fps;        float total_ticks,last_ticks;        do{                total_ticks = SDL_GetTicks();                last_ticks = total_ticks - app_ticks;                fps = 1000.0 / last_ticks;        }while ( fps > 1000 || ((fps > frame_cap) && cap_frames));        app_ticks = total_ticks;        last_fps = fps;        last_timeslice = 1.0f / fps;        //printf("Timer updated, last_fps= %f , last_timeslice=%f\n",last_fps,last_timeslice);};void fps_counter::set_cap(int cap){        frame_cap = cap;};void fps_counter::cap_off(){        cap_frames = false;};void fps_counter::cap_on(){        cap_frames = true;};


I have several subsystems running at different framerates using this class.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
Use it thusly:


#include "fps_counter.h"fps_counter * counter = new fps_counter();while(we_don't_want_to_exit){    counter.tick();    float fps = counter.get_fps();    printf("We know that oop is running at %d fps by measuring time between calls of tick()\n");}


To measure the time between ticks. I have each subsystem use one of these, and accumilate the results of get_timeslice until its time to update, then fire off an event of some kind. (e.g. sprites updating at 15fps, physics running at 60, character animations running at 30, etc.)

I don't know why I called it an FPS counter, but its not really a timer (no callbacks) and its not limited to FPS it also does the timeslice.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!

This topic is closed to new replies.

Advertisement