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
         return (cache[c], o)
51
         return (cache[c], o)
52
 
52
 
53
     def drawGlyph(self, g, xOff, yOff):
53
     def drawGlyph(self, g, xOff, yOff):
54
+        if xOff >= self.gui.width:
55
+            return
56
+
54
         for x in range(0, g.width):
57
         for x in range(0, g.width):
55
             for y in range(0, g.height):
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
                 p = g.getpixel((x, y))
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
         w = 0
67
         w = 0
61
         for c in s:
68
         for c in s:
69
+            xOff = -offset + w
70
+            if earlyAbort:
71
+                if xOff >= self.gui.width:
72
+                    break
73
+
62
             g, y = self.getGlyph(c)
74
             g, y = self.getGlyph(c)
63
-            self.drawGlyph(g, -offset + w, y)
64
             w += g.width
75
             w += g.width
76
+
77
+            if xOff >= -10: # some wiggle room so chars dont disappear
78
+                self.drawGlyph(g, xOff, y)
65
         return w
79
         return w
66
 
80
 
67
 class ScrollText:
81
 class ScrollText:
72
         self.iterations = i
86
         self.iterations = i
73
         self.speed = 1.0 / s
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
         self.restart()
90
         self.restart()
77
 
91
 
78
     def restart(self):
92
     def restart(self):
91
                 self.offset = -self.gui.width
105
                 self.offset = -self.gui.width
92
                 self.count += 1
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
 if __name__ == "__main__":
110
 if __name__ == "__main__":
97
     import platform
111
     import platform

Loading…
Cancel
Save