119 lines
No EOL
2.3 KiB
Python
Executable file
119 lines
No EOL
2.3 KiB
Python
Executable file
import pygame
|
|
import collections
|
|
|
|
def start_loop():
|
|
global g_timer, g_elapsed
|
|
g_elapsed = 0
|
|
g_elapsed_sec = 0.0
|
|
g_timer = pygame.time.get_ticks()
|
|
reset_fps_count()
|
|
|
|
def next_frame(skipping=False):
|
|
global g_timer, g_elapsed, g_elapsed_sec
|
|
|
|
newticks = pygame.time.get_ticks()
|
|
#if skipping:
|
|
# # reset g_timer to it's value from the previous frame
|
|
# g_timer -= g_elapsed
|
|
g_elapsed = newticks - g_timer
|
|
g_timer = newticks
|
|
g_elapsed_sec = float(g_elapsed) / 1000.0
|
|
|
|
if g_elapsed != 0:
|
|
update_fps_count(g_elapsed)
|
|
|
|
|
|
def reset_fps_count():
|
|
global g_framelist, update_fps_count
|
|
g_framelist = collections.deque()
|
|
update_fps_count = update_fps_count_empty
|
|
|
|
def update_fps_count_empty(elapsed):
|
|
global g_framelist, update_fps_count
|
|
g_framelist.append(elapsed)
|
|
if len(g_framelist) >= 25:
|
|
update_fps_count = update_fps_count_full
|
|
|
|
def update_fps_count_full(elapsed):
|
|
global g_framelist
|
|
g_framelist.popleft()
|
|
g_framelist.append(elapsed)
|
|
|
|
def update_fps_count(elapsed):
|
|
"this is replaced with either the _empty or _full variant, depending on the situation"
|
|
pass
|
|
|
|
def elapsed():
|
|
"""
|
|
get the amount of time in milliseconds passed since the last frame was displayed
|
|
"""
|
|
return g_elapsed
|
|
|
|
def elapsed_sec():
|
|
"""
|
|
get the amount of time in seconds passed since the last frame was displayed
|
|
"""
|
|
return g_elapsed_sec
|
|
|
|
def num_frames(delay, offset=0):
|
|
"""
|
|
if you want something to occur every "delay" milliseconds,
|
|
this will return the number of times you should make it happen
|
|
in this particular frame (can be 0)
|
|
"""
|
|
global g_elapsed, g_timer
|
|
return int((g_timer - offset) / delay) - int((g_timer - g_elapsed - offset) / delay)
|
|
|
|
def loop_frames(delay, offset=0):
|
|
return xrange(num_frames(delay, offset))
|
|
|
|
def get_timer():
|
|
return g_timer
|
|
|
|
def average(d):
|
|
#spf = (float(sum(g_framelist)) / (len(g_framelist) * 1000.0))
|
|
if len(d) == 0:
|
|
return 0.0
|
|
|
|
smooth = 0.85
|
|
#v2 = float(d[-1])
|
|
#for i in xrange(len(d) - 2, 0, -1):
|
|
# pass
|
|
|
|
v2 = float(d[0])
|
|
for i in xrange(1, len(d)):
|
|
v1 = float(d[i])
|
|
v2 = (smooth * v2) + ((1.0 - smooth) * v1)
|
|
|
|
return v2
|
|
|
|
|
|
def get_fps():
|
|
global g_framelist
|
|
#return ",".join([str(x) for x in sorted(g_framelist)])
|
|
if len(g_framelist) == 0.0:
|
|
return 0.0
|
|
spf = average(g_framelist) / 1000.0
|
|
if spf == 0.0:
|
|
return 0.0
|
|
#print "%s < %s" % (1.0 / spf, g_framelist)
|
|
return 1.0 / spf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|