Source

Fat x Fast / FatxFast / gui / hud / gameframe.py

Full commit
# This file is part of FatxFast.                                                
#                                                                               
#    FatxFast is free software: you can redistribute it and/or modify           
#    it under the terms of the GNU General Public License as published by       
#    the Free Software Foundation, either version 3 of the License, or          
#    (at your option) any later version.                                        
#                                                                               
#    FatxFast is distributed in the hope that it will be useful,                
#    but WITHOUT ANY WARRANTY; without even the implied warranty of             
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              
#    GNU General Public License for more details.                               
#                                                                               
#    You should have received a copy of the GNU General Public License          
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.          
                                                                                
# Versioning based on:                                                          
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage         
__author__ = "dryatu (c) 2013"                                                  
__version__ = "1.2.4" 

from FatxFast.gui.hud.frame import Frame
from FatxFast.gui.hud.buffframe import BuffFrame
from FatxFast.gui.hud.attributesframe import AttributesFrame
from FatxFast.gui.hud.rageframe import RageFrame
import pygame

class GameFrame(Frame):
    # Frame which is bound to a player 
    def __init__(self, game, player, camera, **kwargs):
        super(GameFrame, self).__init__(game, **kwargs)
        self.player = player
#        self.add_widget("rage-frame-"+str(id(player)), 
#            RageFrame(game, player, **self.fetch_template("rageframe")))
        self.add_widget("buff-frame-"+str(id(player)), 
            BuffFrame(game, player, **self.fetch_template("buffbar"))) 
        self.add_widget(
            "attribute-frame-"+str(id(player)), 
            AttributesFrame(game, player, 
                **self.fetch_template("attributebar")))
        self.tilemap = self.game.tilemap
        self.camera = camera
        self._temp_surf = pygame.Surface(self.camera.rect.size)

    def _set_camera_zoom(self):
        # Optimize to cache a resized surface
        if self._temp_surf.get_size() != self.camera.rect.size:
            self._temp_surf = pygame.Surface(self.camera.rect.size)

    def draw(self, surface):
        # If camera size differs from the game frame size
        if self.camera.rect.size != self.rect.size:
            self._set_camera_zoom()
            self.tilemap.draw(self._temp_surf, self.camera)
            surf = pygame.transform.scale(self._temp_surf, self.rect.size)
            surface.blit(surf, (0,0))
        else:
            surf_rect = surface.get_rect()
            sub = surface.subsurface(self.rect.clip(surf_rect))
            self.tilemap.draw(sub, self.camera)
        super(GameFrame, self).draw(surface)