1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include "fps.h"
Uint32 fps_gamestart;
Uint32 fps_lasttime;
Uint32 fps_frames;
double fps_factor;
int fps;
int fps_method;
int fps_display;
#define AVG_FRAMES 10
double fps_factor_avg[AVG_FRAMES];
int fps_factor_avg_idx;
extern GAMESTATE state;
extern SDL_Surface *screen;
void fps_init()
{
int i;
fps_gamestart=SDL_GetTicks();
fps_lasttime=fps_gamestart;
fps_frames=0;
fps_factor=1;
fps_method=MAXFPS;
fps_display=0;
for(i=0;i<AVG_FRAMES;i++)
fps_factor_avg[i]=1;
fps_factor_avg_idx=0;
}
void fps_show()
{
char fpstxt[9];
SDL_Surface *t;
SDL_Rect r;
static int fps_index=0;
static int fps_array[50];
int fps_avg;
static int fps_avg_old=0;
int i;
if((fps_display) && (state.mainstate==ST_GAME_PLAY)) {
if(fps_method==MAXFPS) {
fps_array[fps_index]=fps;
fps_avg=0;
for(i=0;i<50;i++) {
fps_avg+=fps_array[i];
}
fps_avg/=50;
if(++fps_index>49) {
fps_index=0;
fps_avg_old=fps_avg;
}
sprintf(fpstxt,"%03d FPS",fps_avg_old);
} else {
sprintf(fpstxt,"- FIX -");
}
t=font_render(fpstxt,FONT07);
r.x=screen->w-t->w;
r.y=screen->h-t->h;
r.w=t->w;
r.h=t->h;
SDL_BlitSurface(t,NULL,screen,&r);
SDL_FreeSurface(t);
}
}
void toggle_fps()
{
if(fps_method==MAXFPS) {
fps_method=WAITFORFRAME;
fps_factor=1;
} else {
fps_method=MAXFPS;
fps_factor=1;
}
}
void fps_newframe()
{
static Uint32 nexttime=0;
static Uint32 now;
int i;
fps_frames++;
fps_lasttime=now;
now=SDL_GetTicks();
switch(fps_method) {
case WAITFORFRAME:
if(nexttime<=now)
nexttime=now+TICK_INTERVAL;
else
SDL_Delay(nexttime-now);
break;
case MAXFPS:
// fps_factor=(double)(now-fps_lasttime)*PIXEL_SPEED/1000;
// fps_factor=(double)(now-fps_lasttime)/TICK_INTERVAL;
fps_factor_avg[fps_factor_avg_idx++]=(double)(now-fps_lasttime)/TICK_INTERVAL;
if(fps_factor_avg_idx==AVG_FRAMES)
fps_factor_avg_idx=0;
fps_factor=0;
for(i=0;i<AVG_FRAMES;i++)
fps_factor+=fps_factor_avg[i];
fps_factor/=AVG_FRAMES;
fps=1/((double)(now-fps_lasttime)/1000);
break;
}
}
|