summaryrefslogtreecommitdiff
path: root/plugins/dfOpenGL/fps.c
blob: 21f95c7fa88e750db2db9a6d29684b6469e72473 (plain) (blame)
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
#include <sys/time.h>
#include <unistd.h>
#include "gpu_i.h"

static const double NTSC = 100000000/5994;
static const double PAL = 100000000/5000;

static double lastvsync = 0;
#define FRAMESAMPLES 10

double GetTime()	//in microseconds
{
 struct timeval tv;
 gettimeofday(&tv, 0);                                 // well, maybe there are better ways
 return (double)tv.tv_sec * 1000000 + tv.tv_usec;      // to do that, but at least it works
}

void waitforrealtime()
{
	double currenttime,tickstogo;
	double target;

	currenttime = GetTime();

	if (currenttime < lastvsync + 1000000)
		target = lastvsync + (psxDisp.pal ? PAL : NTSC);
	else
		target = currenttime;

	lastvsync = target;

	while (currenttime < target)
	{
		tickstogo = target - currenttime;
		if (tickstogo >= 500.0f)
		{
			usleep((useconds_t)tickstogo-200);	//usleep in microseconds
		}
		currenttime = GetTime();
	}
}