-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmapfont.py
109 lines (80 loc) · 3.27 KB
/
bitmapfont.py
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
100
101
102
103
104
105
106
107
import pygame
NUM_CHARS = 96
TEXT_CACHING = True
UPPERCASE_MODE = False
class BitmapFont(object):
def __init__(self, filename, font_w=8, font_h=8, colors=None, zoom=1, scr_w=320, scr_h=240):
self.lastxpos, self.lastypos = 0, 0
self.font_w = font_w
self.font_h = font_h
self.scr_w = scr_w
self.scr_h = scr_h
self.fonts = {}
if not colors:
colors = [(0, 0, 0), (255, 255, 255)]
self.lastcolor = (255, 255, 255)
for c in colors:
font = pygame.image.load(filename)
font = pygame.transform.scale(font, (font_w * NUM_CHARS * zoom, font_h * zoom))
font.fill(c, special_flags=pygame.BLEND_MULT)
self.fonts[c] = font
self.font_w *= zoom
self.font_h *= zoom
self.textCache = {}
def drawText(self, output, text, x=None, y=None, fgcolor=None, bgcolor=None, blink=False):
global tick
if blink:
if tick % 90 >= 45:
return
if x is None:
x = self.lastxpos
if y is None:
y = self.lastypos
if fgcolor is None:
fgcolor = self.lastcolor
else:
self.lastcolor = fgcolor
if UPPERCASE_MODE:
text = text.upper()
if bgcolor is not None:
output.fill(bgcolor, (x * self.font_w,
(y * self.font_h),
len(text) * self.font_w,
(self.font_h))
)
if TEXT_CACHING:
key = (text, fgcolor, bgcolor)
if key not in self.textCache:
cacheSurface = pygame.Surface((len(text) * self.font_w, self.font_h), flags=pygame.SRCALPHA)
for i, c in enumerate(text):
grabx = (ord(c) - 32) * self.font_w
blitx = i * self.font_w
blity = (self.font_h - self.font_h + 1) / 2
cacheSurface.blit(self.fonts[fgcolor], (blitx, blity), (grabx, 0, self.font_w, self.font_h))
self.textCache[key] = cacheSurface
else:
cacheSurface = self.textCache[key]
blitx = x * self.font_w
blity = y * self.font_h + (self.font_h - self.font_h + 1) / 2
output.blit(cacheSurface, (blitx, blity))
else:
for i, c in enumerate(text):
grabx = (ord(c) - 32) * self.font_w
blitx = (x + i) * self.font_w
blity = y * self.font_h + (self.font_h - self.font_h + 1) / 2
output.blit(self.fonts[fgcolor], (blitx, blity), (grabx, 0, self.font_w, self.font_h))
self.lastxpos = x
self.lastypos = y + 1
def centerText(self, output, text, y=None, fgcolor=None, bgcolor=None, blink=False):
x = ((self.scr_w / self.font_w) - len(text)) / 2
self.drawText(output, text, x, y, fgcolor, bgcolor, blink)
def locate(self, x=None, y=None):
if x is not None:
self.lastxpos = x
if y is not None:
self.lastypos = y
def locateRel(self, x=None, y=None):
if x is not None:
self.lastxpos += x
if y is not None:
self.lastypos += y