DebugViews can now be added (created a DebugFPS, which just shows
DebugViews can now be added (created a DebugFPS, which just shows
'TEST') to an overlay and are automatically packed into the available area.

file:a/game.py -> file:b/game.py
--- a/game.py
+++ b/game.py
@@ -38,6 +38,7 @@
 
         # Initialize the game system
         self.debugOverlay=GObjDebugOverlay(self)
+        self.debugFPS=DebugFPS(self.debugOverlay,self)
 
     def run(self):
         velocityIterations=6
@@ -148,12 +149,110 @@
                 self.remove()
 
 class GObjDebugOverlay(GameObject):
+    # Font initialization is postponed until first object is created (when pygame's font
+    # engine is ready)
+    font=None
+    font_color=None
+    letter_height=None
+
     def __init__(self,game):
         super(GObjDebugOverlay,self).__init__(game,name='DEBUG')
 
-class GObjDebugScreen(GameObject):
-    def __init__(self,game,**kwargs):
-        super(GObjDebugScreen,self).__init__(game,**kwargs)
+        if GObjDebugOverlay.font==None: # Initialize font
+                GObjDebugOverlay.font=pygame.font.Font(None, 20)
+                GObjDebugOverlay.font_color=(255,255,255)
+                GObjDebugOverlay.letter_height=GObjDebugOverlay.font.size('A')[1]
+
+        self.views=[]
+
+    def add_view(self,view):
+        # Repack the rectangles using very basic algorith (stop at step 3 of
+        # 'Basic Algorithm') from
+        # http://www.codeproject.com/Articles/210979/Fast-optimizing-rectangle-packing-algorithm-for-bu
+
+        # Step 1: Insert the view such that the list remains in sorted order from
+        #         highest to shortest
+        inserted=False
+        for i in range(len(self.views)):
+            if view.size[1]<self.views[i].size[1]:
+                self.views.insert(i,view)
+                inserted=True
+                break
+        if not inserted:
+            self.views.append(view)
+
+        #print 'PACK START'
+        # Step 2: Indicate the maximum height based on screen height
+        max_height=self.game.screen.get_size()[1]
+
+        # Step 3: Insert view as left as possible
+        views=[] # Already inserted views
+        for view in self.views:
+            # Possible positions are to start at top right corner of already placed views
+            guesses=[(0,0)] # Top-left
+            for existing in views:
+                guesses.append((existing.position[0]+existing.size[0],existing.position[1]))
+            #print guesses
+
+            for guess in guesses:
+                #print 'Try',guess
+                if guess[1]+view.size[1]>max_height: continue # Won't work, goes over height
+
+                for existing in views: # Make sure it doesn't overlap with anything else
+                    # view to left of existing
+                    cond1=(guess[0]+view.size[0]<existing.position[0])
+                    #print 'cond1',cond1
+                    # view to right of existing
+                    cond2=(guess[0]>existing.position[0]+existing.size[0])
+                    #print 'cond2',cond2
+                    # view to top of existing
+                    cond3=(guess[1]+view.size[1]<existing.position[1])
+                    #print 'cond3',cond3
+                    # view to bottom of existing
+                    cond4=(guess[1]>existing.position[1]+existing.size[1])
+                    #print 'cond4',cond4
+
+                    if not cond1 and not cond2 and not cond3 and not cond4:
+                        # They overlap, so try placing it below the existing one
+                        guess=(guess[0],existing.position[1]+existing.size[1])
+                        #print 'OVERLAP',guess
+
+                # This guess should work
+                if guess[1]+view.size[1]<max_height:
+                    view.position=guess
+                    break
+
+
+            views.append(view)
+
+        # Step '4': Save
+        self.views=views
+
+    def draw(self,screen):
+        for view in self.views:
+            img=pygame.Surface(view.size)
+            view.draw(img)
+
+            screen.blit(img,view.position)
+
+
+class DebugView(object):
+    def __init__(self,overlay,game,size):
+        self.game=game
+        self.size=size
+
+        overlay.add_view(self)
+
+    def draw(self,screen):
+        pass
+
+
+class DebugFPS(DebugView):
+    def __init__(self,overlay,game):
+        super(DebugFPS,self).__init__(overlay,game,(100,GObjDebugOverlay.letter_height))
+    def draw(self,screen):
+        img=GObjDebugOverlay.font.render('TEST',True,GObjDebugOverlay.font_color)
+        screen.blit(img,(0,0))
 
 
 class EventListener(object):