Title: Fast Digit Rendering class

Author: Adam Feuer
Submission date: Oct 3, 2001

Description: Speeds up rendering of integers by precaching integer images.

Download: fastdigits.py

pygame version required: 1.1
SDL version required: Any
Python version required: 2.0

Comments: This class prerenders the integers from 0-9 in a font and color you specify. You can then feed it a number between 0 and 9,999,999, and it will quickly fetch those images and render your number in the font you specified. Mr. Feuer has predefined a few colors and font sizes for you, or you can use the default font and size of 30.

Messages: 0


# fast digit rendering
#
# Adam Feuer 
#
# This code is placed into the public domain.
# I hope you find it useful.
#
# This is code is for fast rendering of integers.
# It needs Pygame 1.1 or greater and Python 2.0 or greater.
#

import os
import pygame
from pygame.locals import *
import game, gfx

FONTSIZE_TINY   = 17
FONTSIZE_SMALL  = 35
FONTSIZE_MEDIUM = 50
FONTSIZE_LARGE  = 80

GREY      = ( 50, 150, 150)
BLUE_GREY = (114, 124, 193)
WHITE     = (255, 255, 255)
GREEN     = (123, 204,  83)
WHITE     = (255, 255, 255)

FONT = None
FONTSIZE = 30


class FastDigits:
    """Build an array of digit images, and provide a speedy method for converting an integer to an image...
       speeds up digit rendering by about 10x. You can get another 2-3x by using an image/rectangle cache.

    Usage:

    fd= FastDigits()
    digits_image, digits_rect = fd.num2image(frames_per_second)

    """
    def __init__(self,fontpath=None,fontsize=FONTSIZE,fontcolor=WHITE):
        # build digit table
        self.digits = []
        font = pygame.font.Font(fontpath,fontsize)
        for num in range(0,10):
            d_img,d_rect = gfx.text(font,fontcolor,"%d"%num)
            self.digits.append([d_img,d_rect])
        self.digitwidth = self.digits[0][1].width
        self.digitheight = self.digits[0][1].height

    def getdigits(self,num,zeropad=0):
        """convert a number to sequence of digits- handles integers up to 9,999,999;
             zeropad is number of digits to zeropad"""
        digitseq = []

        if zeropad > 0:
            fmt = "%" + ("0.%dd" % zeropad)
        else:
            fmt = "%d"
            
        s = fmt % num
        if len(s)>7:
            s = s[-7:]
        for c in s:
            digitseq.append(ord(c)-ord('0'))
        return digitseq

    def num2image(self,num,zeropad=0):
        """convert an integer to an image - pad image with zeros to zeropad places
             returns image, image.get_rect()"""
        digitseq = self.getdigits(num,zeropad)
        dsurface = pygame.Surface((self.digitwidth * len(digitseq), self.digitheight))
        bgd = 0,0,0
        dsurface.set_colorkey(bgd)

        x = 0
        for d in digitseq:
            dsurface.blit(self.digits[d][0],(x,0))
            x += self.digits[d][1].width 
        return dsurface.convert(),dsurface.get_rect()

Main - Repository - Submit - News

Feedback