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
             if filename == "iv18x16u.bdf":
28
             if filename == "iv18x16u.bdf":
29
                 offset = 6
29
                 offset = 6
30
 
30
 
31
-            data = (font, offset)
31
+            data = (font, offset, {})
32
             self.fonts.append(data)
32
             self.fonts.append(data)
33
 
33
 
34
     def getGlyph(self, c):
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
     def drawGlyph(self, g, xOff, yOff):
53
     def drawGlyph(self, g, xOff, yOff):
48
         for x in range(0, g.width):
54
         for x in range(0, g.width):
97
     else:
103
     else:
98
         from test import TestGUI
104
         from test import TestGUI
99
         t = TestGUI()
105
         t = TestGUI()
106
+
100
     d = ScrollText(t, "Hello, World!")
107
     d = ScrollText(t, "Hello, World!")
101
     t.debug_loop(d.draw)
108
     t.debug_loop(d.draw)

+ 3
- 0
life.py View File

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

+ 5
- 0
manager.py View File

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

+ 2
- 1
solid.py View File

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

+ 1
- 0
splash.py View File

33
     else:
33
     else:
34
         from test import TestGUI
34
         from test import TestGUI
35
         t = TestGUI()
35
         t = TestGUI()
36
+
36
     s = SplashScreen(t)
37
     s = SplashScreen(t)
37
     s.draw()
38
     s.draw()
38
     t.debug_loop(s.draw)
39
     t.debug_loop(s.draw)

+ 1
- 0
weather.py View File

67
     else:
67
     else:
68
         from test import TestGUI
68
         from test import TestGUI
69
         t = TestGUI()
69
         t = TestGUI()
70
+
70
     s = WeatherScreen(t)
71
     s = WeatherScreen(t)
71
     s.draw()
72
     s.draw()
72
     t.debug_loop(s.draw)
73
     t.debug_loop(s.draw)

Loading…
Cancel
Save