Cache infobar more aggressively. Rrrr.
authorSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 16:42:22 +0000 (18:42 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 16:42:22 +0000 (18:42 +0200)
tabakrolletjie/infobar.py

index f468ef60aa5611eec8aa7508cd9ccf0786318510..5a4d3ccc0aebde1cb0329fe775a44db318b1e31a 100644 (file)
@@ -29,21 +29,29 @@ class InfoBar(object):
             "power usage {scene.power_usage}"
         ])
         self._font = loader.load_font(FONTS['sans'], size=20)
+        self._text = None
+        self._infobar = None
 
     def render(self, surface):
+        if self._infobar is None:
+            self._update_infobar()
         surface.blit(self._infobar, self.pos, None)
 
-    def update(self, gamestate):
-        options = {
-            "gamestate": gamestate, "battery": self.battery,
-            "scene": self.scene, "time_of_day": self.time_of_day,
-        }
-        text = self.template.format(**options)
-        text_img = self._font.render(text, True, self.color)
-
+    def _update_infobar(self):
+        text_img = self._font.render(self._text, True, self.color)
         width = text_img.get_width() + 10
         height = text_img.get_height() + 10
         self._infobar = pygame.surface.Surface(
             (width, height), pgl.SWSURFACE).convert_alpha()
         self._infobar.fill((0, 0, 0, 64))
         self._infobar.blit(text_img, (5, 3), None)
+
+    def update(self, gamestate):
+        options = {
+            "gamestate": gamestate, "battery": self.battery,
+            "scene": self.scene, "time_of_day": self.time_of_day,
+        }
+        text = self.template.format(**options)
+        if text != self._text:
+            self._text = text
+            self._infobar = None