[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Strange fps throttling results with pygame.time.set_timer



Hi,

When I added fps throttling to my engine, the actual fps I get seems to be very off from the fps target. The engine steps when it gets a EVT_STEP event. Those events are coming from a timer that I set by calling pygame.time.set_timer(int(1000.0 / targetFPS + 0.5)).

If the targetFPS is set any higher than 95, I get a fps of ~100. If it's 95 or lower (down to 48), I get a fps of ~50.

Here are some of the ranges of the targetFPS values and the actual fps values I get:
target fps real fps
25...32 ~25
33...48 ~34
49...95 ~51
>= 96 ~100


I'm using Windows XP, but in #pygame piman had the same problem (although with differing ranges, e.g. he got 100 fps when the target fps was anything over 50) with Linux.

Could it be that the clock is not precise enough? With this problem I can only limit the fps to 50 or 100, nothing in between. Is there another way to implement the throttling? I'd like to use the event system because the engine is mostly event driven.

Here is the code that I used to get these results:


import pygame

EVT_STEP = pygame.USEREVENT

targetFPS = 60

pygame.init()
pygame.display.set_mode((1, 1))
pygame.time.set_timer(EVT_STEP, int(1000.0 / targetFPS + 0.5))

print "Stepping every %d ticks" % int(1000.0 / targetFPS + 0.5)

frames = 0
lastFPSTime = pygame.time.get_ticks()

quit = False
while not quit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit = True
        elif event.type == pygame.KEYDOWN and \
             event.key == pygame.K_ESCAPE:
            quit = True
        elif event.type == EVT_STEP:
            frames += 1
            if pygame.time.get_ticks() - lastFPSTime >= 1000:
                print "FPS:", frames
                frames = 0
                lastFPSTime = pygame.time.get_ticks()


-- dOb