Browse Source

glyph caching

Thomas Buck 1 year ago
parent
commit
62cda06a50
6 changed files with 29 additions and 11 deletions
  1. 17
    10
      draw.py
  2. 3
    0
      life.py
  3. 5
    0
      manager.py
  4. 2
    1
      solid.py
  5. 1
    0
      splash.py
  6. 1
    0
      weather.py

+ 17
- 10
draw.py View File

@@ -28,21 +28,27 @@ class DrawText:
28 28
             if filename == "iv18x16u.bdf":
29 29
                 offset = 6
30 30
 
31
-            data = (font, offset)
31
+            data = (font, offset, {})
32 32
             self.fonts.append(data)
33 33
 
34 34
     def getGlyph(self, c):
35
-        f, o = self.fonts[0] # TODO selection of fonts
36
-        g = f.glyph(c).draw()
35
+        f, o, cache = self.fonts[0] # TODO selection of fonts
37 36
 
38
-        # invert color
39
-        g = g.replace(1, 2).replace(0, 1).replace(2, 0)
37
+        # only render glyphs once, cache resulting image data
38
+        if not c in cache:
39
+            g = f.glyph(c).draw()
40 40
 
41
-        # render to pixel data
42
-        img = Image.frombytes('RGBA',
43
-                              (g.width(), g.height()),
44
-                              g.tobytes('RGBA'))
45
-        return (img, o)
41
+            # invert color
42
+            g = g.replace(1, 2).replace(0, 1).replace(2, 0)
43
+
44
+            # render to pixel data
45
+            img = Image.frombytes('RGBA',
46
+                                (g.width(), g.height()),
47
+                                g.tobytes('RGBA'))
48
+
49
+            cache[c] = img
50
+
51
+        return (cache[c], o)
46 52
 
47 53
     def drawGlyph(self, g, xOff, yOff):
48 54
         for x in range(0, g.width):
@@ -97,5 +103,6 @@ if __name__ == "__main__":
97 103
     else:
98 104
         from test import TestGUI
99 105
         t = TestGUI()
106
+
100 107
     d = ScrollText(t, "Hello, World!")
101 108
     t.debug_loop(d.draw)

+ 3
- 0
life.py View File

@@ -99,9 +99,12 @@ if __name__ == "__main__":
99 99
     else:
100 100
         from test import TestGUI
101 101
         t = TestGUI()
102
+
102 103
     g = GameOfLife(t)
104
+
103 105
     def helper():
104 106
         if g.finished():
105 107
             g.restart()
106 108
         g.draw()
109
+
107 110
     t.debug_loop(helper)

+ 5
- 0
manager.py View File

@@ -7,6 +7,9 @@ class Manager:
7 7
         self.gui = g
8 8
         self.screens = []
9 9
         self.index = 0
10
+        self.restart()
11
+
12
+    def restart(self):
10 13
         self.lastTime = time.time()
11 14
 
12 15
     def add(self, s, d = None):
@@ -17,6 +20,7 @@ class Manager:
17 20
         self.screens[self.index][0].draw()
18 21
 
19 22
         if self.screens[self.index][1] == None:
23
+            # let screen decide when it is done
20 24
             if self.screens[self.index][0].finished():
21 25
                 self.index = (self.index + 1) % len(self.screens)
22 26
                 self.lastTime = time.time()
@@ -61,4 +65,5 @@ if __name__ == "__main__":
61 65
     m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), 20.0))
62 66
     m.add(Solid(t, 1.0))
63 67
 
68
+    m.restart()
64 69
     t.debug_loop(m.loop)

+ 2
- 1
solid.py View File

@@ -29,5 +29,6 @@ if __name__ == "__main__":
29 29
     else:
30 30
         from test import TestGUI
31 31
         t = TestGUI()
32
-    d = ScrollText(t, "Hello, World!")
32
+
33
+    d = Solid(t, 1.0, (0, 255, 0))
33 34
     t.debug_loop(d.draw)

+ 1
- 0
splash.py View File

@@ -33,6 +33,7 @@ if __name__ == "__main__":
33 33
     else:
34 34
         from test import TestGUI
35 35
         t = TestGUI()
36
+
36 37
     s = SplashScreen(t)
37 38
     s.draw()
38 39
     t.debug_loop(s.draw)

+ 1
- 0
weather.py View File

@@ -67,6 +67,7 @@ if __name__ == "__main__":
67 67
     else:
68 68
         from test import TestGUI
69 69
         t = TestGUI()
70
+
70 71
     s = WeatherScreen(t)
71 72
     s.draw()
72 73
     t.debug_loop(s.draw)

Loading…
Cancel
Save