Commits

Patrick Dawson committed fa3a3ed

.

  • Participants
  • Parent commits 7f1a424

Comments (0)

Files changed (3)

 
 import sgc
 from sgc.locals import *
+from sgc.widgets._locals import Font
 
 import sys
 
 from common import *
 from screens import *
 
+wfont = None
+
 def main():
     pygame.mixer.init(44100, -16, 2, 1024)
     pygame.init()
     pygame.font.init()
-    
+        
     screen = sgc.surface.Screen((SCREEN_X,SCREEN_Y))
     pygame.display.set_caption("Fort in the Woods")
     
+    wfont = FC("Artifika-Regular", 14)
+    Font._fonts["widget"] = wfont
+
     clock = pygame.time.Clock()
     
     sm = ScreenManager(screen)
     @classmethod
     def play(cls, fn):
         cls.load(fn).play()
+        
 
+class FontCache(object):
+    _cache = {}
+    
+    @classmethod
+    def load(cls, fn, size):
+        try:
+            return cls._cache[(fn,size)]
+        except KeyError:
+            f = pygame.font.Font('data/%s.ttf'%fn, size)
+            cls._cache[(fn,size)] = f
+            return f
 
 class ImageLoader(object):
     _cache = {}
-    _font = None
     
     @classmethod
     def load(cls, fn):
     
     @classmethod
     def damage(cls, num):
-        if cls._font is None:
-            cls._font = pygame.font.Font('data/Artifika-Regular.ttf', 24)
-        txt = cls._font.render(str(num), True, WHITE)
+        txt = FC('Artifika-Regular',24).render(str(num), True, WHITE)
         r = txt.get_rect()
         r.center = (32,32)
         surf = pygame.Surface((64,64), flags=pygame.SRCALPHA)
 
 class TextLog(sgc.widgets.Label):
     def __init__(self, *args, **kwargs):
-        sgc.widgets.Label.__init__(self, *args, **kwargs)
+        sgc.widgets.Label.__init__(self, font=FC('Artifika-Regular', 18), *args, **kwargs)
         self.lines = []
         self.max_lines = 6
         
         self.lines.append(line)
         if len(self.lines) > self.max_lines:
             self.lines = self.lines[1:]
-        self.config(text='\n'.join(self.lines))        
+        self.config(text='\n'.join(self.lines))
 
 
 def IL(fn):
     return ImageLoader.load(fn)
+    
+def FC(fn, size):
+    return FontCache.load(fn, size)
         self.exit_btn.click = sys.exit
         self.add(self.exit_btn)
 
-        font = pygame.font.Font('data/Artifika-Regular.ttf', 48)
-        title_surf = font.render('Fort in the Woods', True, (255,255,255))
-        title_rect = title_surf.get_rect()
-        title_rect.centerx = SCREEN_X/2
-        title_rect.top = 200
+        title_font = FC('Artifika-Regular', 48)    
+        self.title = sgc.widgets.Label(text="Fort in the Woods", font=title_font)
+        self.title.rect.centerx = SCREEN_X/2
+        self.title.rect.top = 200
+        self.add(self.title)
         
-        font = pygame.font.Font('data/Artifika-Regular.ttf', 14)
-        signature_surf = font.render('by Patrick Dawson <pat@dw.is>', True, WHITE)
-        signature_rect = signature_surf.get_rect()
-        signature_rect.bottomright = 1024 - 8, 768 - 8
-    
-        self.title = sgc.widgets.Simple(surf=title_surf, pos=title_rect.topleft)
-        self.add(self.title)
-        self.signature = sgc.widgets.Simple(surf=signature_surf, pos=signature_rect.topleft)
+        sig_font = FC('Artifika-Regular', 14)
+        self.signature = sgc.widgets.Label(text='by Patrick Dawson <pat@dw.is>', font=sig_font)
+        self.signature.rect.bottomright = 1024 - 8, 768 - 8
         self.add(self.signature)
 
     def clickStart(self):
         
         Now you must survive. Build farms or hunt to feed everyone, and train
         troops to fend off attacks from enemy scouting parties.
+        
+        Survive for 20 weeks until reinforcements arrive.
         """
         
-        self.txt = sgc.widgets.Label(text=mytxt, pos=(100,200))
+        self.txt = sgc.widgets.Label(text=mytxt, font=FC('Artifika-Regular', 16), pos=(100,200))
         self.add(self.txt)
         
         self.btn_continue = MyButton(label="Continue", pos=(500,400))
         self.fs_widget = sgc.widgets.Simple(surf=self.fs.canvas, pos=(256-4,192-4))
         self.rd_widget = sgc.widgets.Simple(surf=self.rd.canvas, pos=(256-4,24-4))
         
-        self.font1 = pygame.font.Font('data/Artifika-Regular.ttf', 32)
-        self.font2 = pygame.font.Font('data/Artifika-Regular.ttf', 22)
-        t1 = self.font1.render('TURN 1', True, WHITE)
-        t2 = self.font2.render('Assign 3 workers', True, WHITE)
-        self.txt1 = sgc.widgets.Simple(surf=t1, pos=(50,60))
-        self.txt2 = sgc.widgets.Simple(surf=t2, pos=(20,100))
+        font1 = FC('Artifika-Regular', 32)
+        font2 = FC('Artifika-Regular', 22)
+
+        self.txt1 = sgc.widgets.Label(text='TURN 1', font=font1, pos=(50,60))
+        self.txt2 = sgc.widgets.Label(text='Assign 3 workers', font=font2, pos=(20,100))
         
         self.btn_build_farm = MyButton(label="Build Farm\n-10 Wood", pos=(10,200))
         self.btn_build_barracks = MyButton(label="Build Barracks\n-20 Wood", pos=(130,200))
         self.size = (12*64, (2*64)+16)
         self.canvas = pygame.Surface(self.size, flags=pygame.SRCALPHA)
         self.canvas.fill((50,50,50))
-        self.font = pygame.font.Font('data/Artifika-Regular.ttf', 18)
-        self.nfont = pygame.font.Font('data/Artifika-Regular.ttf', 32)
+        self.font = FC('Artifika-Regular', 18)
+        self.nfont = FC('Artifika-Regular', 32)
         self.lbl_workers = self.font.render('Workers', True, WHITE)
         self.lbl_militia = self.font.render('Militia', True, WHITE)
         self.lbl_infantry = self.font.render('Infantry', True, WHITE)