"""ScreenFpsClock - a clock to manage a game's FPS stuff & display FPS on the screen""" import os import pygame.time import gfx from FpsClock import FpsClock from fastdigits import FastDigits from basespriteobj import Sprite from text import * FPS_FONT = None FPS_FONTSIZE = FONTSIZE_TINY FPS_FONTCOLOR = GREY FPS_DISPLAY_RECT = [730,510,60,20] FPS_OFFSET = 30 class ScreenFpsClock(FpsClock,FastDigits): "class for managing FPS related stuff" def __init__(self, surface, background, desired_fps=30, do_report=0): "create FpsClock instance, give desired running fps and enable report" FpsClock.__init__(self,desired_fps,do_report) FastDigits.__init__(self,fontpath=None,fontsize=FPS_FONTSIZE,fontcolor=FPS_FONTCOLOR) self.font = pygame.font.Font(None,FPS_FONTSIZE) # make the 'fps' image self.fps_image, self.fps_rect = gfx.text(self.font, FPS_FONTCOLOR, 'fps') self.fps_rect.left += FPS_OFFSET # make subsurface for us to draw on self.display_rect = pygame.Rect(FPS_DISPLAY_RECT) self.surface = surface.subsurface(self.display_rect) self.rect = self.surface.get_rect() self.background = background.subsurface(FPS_DISPLAY_RECT) self.digits_image, self.digits_rect = self.num2image(0) self.fps_cache = {} # true if we've been drawn self.on_screen = 0 def erase(self): self.surface.blit(self.background,self.rect) gfx.dirty(self.display_rect) self.on_screen = 0 def report(self): # erase - inlined here for speed self.surface.blit(self.background,self.rect) fps = int(self.current_fps) if self.fps_cache.has_key(fps): self.digits_image, self.digits_rect = self.fps_cache[fps] else: self.digits_image, self.digits_rect = self.num2image(fps) self.digits_rect.left = 0 self.digits_rect.top = 0 self.fps_cache[fps] = (self.digits_image,self.digits_rect) # draw fps digits self.surface.blit(self.digits_image, self.digits_rect) # draw 'fps' self.fps_rect.left = self.digits_rect.width self.surface.blit(self.fps_image,self.fps_rect) # outline corners of subsurface (to see it better) #self.surface.set_at((0,0),WHITE) #self.surface.set_at((0,self.display_rect.height-1),WHITE) #self.surface.set_at((self.display_rect.width-1,0),WHITE) #self.surface.set_at((self.display_rect.width-1,self.display_rect.height-1),WHITE) gfx.dirty(self.display_rect) self.on_screen = 1