summaryrefslogtreecommitdiff
path: root/src/frame_skip.cpp
blob: d656a0d6eff4d36fea03bcf9167780ed20b798ea (plain)
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
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include "frame_skip.h"
#include "memmap.h"

#ifndef uclock_t
#define uclock_t unsigned int
#endif

#define TICKS_PER_SEC 1000000UL
//#define CPU_FPS 60
static int CPU_FPS=60;
static uclock_t F;

#define MAX_FRAMESKIP 10


static char init_frame_skip = 1;
char skip_next_frame = 0;
static struct timeval init_tv = { 0, 0 };


void reset_frame_skip(void)
{
	//static Uint8 init=0;

	init_tv.tv_usec = 0;
	init_tv.tv_sec = 0;
	skip_next_frame = 0;
	init_frame_skip = 1;
	CPU_FPS=Memory.ROMFramesPerSecond;

	F = (uclock_t) ((double) TICKS_PER_SEC / CPU_FPS);
}

uclock_t get_ticks(void)
{
	struct timeval tv;

	gettimeofday(&tv, 0);
	if (init_tv.tv_sec == 0)
		init_tv = tv;
	return (tv.tv_sec - init_tv.tv_sec) * TICKS_PER_SEC + tv.tv_usec -
		init_tv.tv_usec;


}

int frame_skip(void)
{
	static int f2skip;
	static uclock_t sec = 0;
	static uclock_t rfd;
	static uclock_t target;
	static int nbFrame = 0;
	static int skpFrm = 0;

	if (init_frame_skip) {
		init_frame_skip = 0;
		target = get_ticks();
		nbFrame = 0;
		//f2skip=0;
		//skpFrm=0;
		sec = 0;
		return 0;
	}

	target += F;
	if (f2skip > 0) {
		f2skip--;
		skpFrm++;
		return 1;
	} else
		skpFrm = 0;


	rfd = get_ticks();

	if (rfd < target && f2skip == 0) {
		while (get_ticks() < target);
	} else {
		f2skip = (rfd - target) / (double) F;
		if (f2skip > MAX_FRAMESKIP) {
			f2skip = MAX_FRAMESKIP;
			reset_frame_skip();
		}
		// printf("Skip %d frame(s) %lu %lu\n",f2skip,target,rfd);
	}
	

	nbFrame++;
	if (get_ticks() - sec >= TICKS_PER_SEC) {
		nbFrame = 0;
		sec = get_ticks();
	}
	return 0;
}