Browse Source

try to optimize text drawing

Thomas Buck 1 year ago
parent
commit
d1755034c1
1 changed files with 19 additions and 5 deletions
  1. 19
    5
      draw.py

+ 19
- 5
draw.py View File

@@ -51,17 +51,31 @@ class DrawText:
51 51
         return (cache[c], o)
52 52
 
53 53
     def drawGlyph(self, g, xOff, yOff):
54
+        if xOff >= self.gui.width:
55
+            return
56
+
54 57
         for x in range(0, g.width):
55 58
             for y in range(0, g.height):
59
+                xTarget = xOff + x
60
+                if (xTarget < 0) or (xTarget >= self.gui.width):
61
+                    continue
62
+
56 63
                 p = g.getpixel((x, y))
57
-                self.gui.set_pixel(xOff + x, yOff + y, p)
64
+                self.gui.set_pixel(xTarget, yOff + y, p)
58 65
 
59
-    def text(self, s, offset = 0):
66
+    def text(self, s, offset = 0, earlyAbort = True):
60 67
         w = 0
61 68
         for c in s:
69
+            xOff = -offset + w
70
+            if earlyAbort:
71
+                if xOff >= self.gui.width:
72
+                    break
73
+
62 74
             g, y = self.getGlyph(c)
63
-            self.drawGlyph(g, -offset + w, y)
64 75
             w += g.width
76
+
77
+            if xOff >= -10: # some wiggle room so chars dont disappear
78
+                self.drawGlyph(g, xOff, y)
65 79
         return w
66 80
 
67 81
 class ScrollText:
@@ -72,7 +86,7 @@ class ScrollText:
72 86
         self.iterations = i
73 87
         self.speed = 1.0 / s
74 88
 
75
-        self.width = self.drawer.text(self.text)
89
+        self.width = self.drawer.text(self.text, 0, False)
76 90
         self.restart()
77 91
 
78 92
     def restart(self):
@@ -91,7 +105,7 @@ class ScrollText:
91 105
                 self.offset = -self.gui.width
92 106
                 self.count += 1
93 107
 
94
-        self.drawer.text(self.text, self.offset)
108
+        self.drawer.text(self.text, self.offset, True)
95 109
 
96 110
 if __name__ == "__main__":
97 111
     import platform

Loading…
Cancel
Save