Exemple #1
0
 def __init__(self):
     self._l = StdoutLogger()
     self._l.log("Creating engine...")
     
     self.printed = 0
     
     self._mm = BitmapManager("maps", self._l)
     self._fm = FontManager(self._l)
     
     self._size = 640, 480
     self._dc = DrawContext(self._size[0], self._size[1])
     self._dc.mm = self._mm
     self._dc.fm = self._fm
     
     self._last_render = self._get_tick()
     self._quit = 0
     
     self._zbuffer = [0 for _ in range((self._size[0] * self._size[1]))]
     
     self._view = None
    def setFont( self, font=None ):
	"""
	Set the font in which to render this text
	"""
	if not font:
	    return

	elif isinstance( font, types.StringType ):

	    # get the font manager if we haven't already
	    if not self.fontMgr:
		self.fontMgr = FontManager()

	    # load the font into memory
	    self.font = self.fontMgr.loadFont( font )

	elif isinstance( font, Font ):
	    self.font = font

	# clear the letter render list so that it will be rebuilt
	self.renderList = []
from types import *
from ui.Font import Font
from ui.UIText import UIText
from managers.FontManager import *

fontMgr = FontManager()
# make the font
aFont = fontMgr.loadFont( 'JohnDoe' )
bFont = fontMgr.loadFont( 'JohnDoe' )

print type(aFont)

# make the text object from the font
aUIText = UIText( value="abcdefghi6789", font=aFont, size=20, pos=[0,0] )

aUIText.setFont( bFont )
class UIText(UIObject):

    def __init__( self, value="", pos=[0,0], font=None, scale=10):
	"""
	Init: (self, value="", pos=[0,0], font=None, scale=10)
	"""
	UIObject.__init__(self)
	self.setsMgr = None
	self.pos = pos
	self.value = value
	# string or font object
	self.setFont( font )
	self.setScale(scale)
	self.fontMgr = None
	self.renderList = []


    def render( self, scrScroll=(0,0) ):
	"""
	This is the function called by the screen on all renderable objects.
	"""

	if not self.visible:
	    return []

	# compile a list for each letter to be rendered
	#   ( id, texClipBox, pos, quadBox )

	# if nothing has changed in this object(as would be indicated
	#   by this list being empty) use the previous render list
	#   (this list is reset in "Set" accessors)
	if len(self.renderList) > 0:
	    return self.renderList

	id = self.font.getTexID()

	# the area covered by the string will depend on the length and scale
	self.calcSize()

	# adjust the pos by screen scroll
	pos = (self.pos[0]-scrScroll[0], self.pos[1]-scrScroll[1])
	if self.centerAround:
	    pos = (pos[0]-self.size[0]/2, pos[1]-self.size[1]/2)
	# init to the left pos of first letter
	horizPos = pos[0]

	# loop over all of letters in the text string
	for character in self.value:
	    # build a renderList-item that represents all that is needed
	    #	to put all or part of a texture into a quad of particular
	    #	size at some spot on the screen.
	    self.renderList.append (\
				(id
				, self.font.getCharBox(character)
				, (horizPos, pos[1]
				, horizPos+self.scale[0]
				, pos[1]+self.scale[1])))

	    horizPos = horizPos + self.scale[0]
	
	return self.renderList

    def calcSize( self ):
	"""
	This function calculates the area of this object
	"""
	self.size = [ len(self.value)*self.scale[0], self.scale[1] ]
	self.halfSize = [self.size[0]/2, self.size[1]/2 ]
	return self.size

    def getText( self ):
	"""
	returns the value of the string
	"""
	return self.value

    def setText( self, value="" ):
	"""
	Set the contents of this text object
	"""
	# ...
	self.value = value

	# clear this so it must be rebuilt
	self.renderList = []

	# calculate the size
	self.calcSize()

    def pop( self ):
	"""
	Takes a letter off of the end of the value string
	"""
	self.value = self.value[0:-1]
	# clear this so it must be rebuilt
	self.renderList = []

    def push( self, str ):
	"""
	Puts the input string at the end of the value string
	"""
	self.value += str
	# clear the renderList
	self.renderList = []


    def setFont( self, font=None ):
	"""
	Set the font in which to render this text
	"""
	if not font:
	    return

	elif isinstance( font, types.StringType ):

	    # get the font manager if we haven't already
	    if not self.fontMgr:
		self.fontMgr = FontManager()

	    # load the font into memory
	    self.font = self.fontMgr.loadFont( font )

	elif isinstance( font, Font ):
	    self.font = font

	# clear the letter render list so that it will be rebuilt
	self.renderList = []

    def setPos( self, pos=(0,0) ):
	UIObject.setPos(self, pos)
	# clear the letter render list so that it will be rebuilt
	self.renderList = []

    def setScale( self, factor=0.01 ):
	if not self.setsMgr:
	    self.setsMgr = SettingsManager()

	res = self.setsMgr.screenRes

	self.scale = [res[0]*factor, res[1]*factor]
	# clear the letter render list so that it will be rebuilt
	self.renderList = []
Exemple #5
0
class Engine(object):
    """Main game object. F**k yeah."""
    _view_controller = None
    def __init__(self):
        self._l = StdoutLogger()
        self._l.log("Creating engine...")
        
        self.printed = 0
        
        self._mm = BitmapManager("maps", self._l)
        self._fm = FontManager(self._l)
        
        self._size = 640, 480
        self._dc = DrawContext(self._size[0], self._size[1])
        self._dc.mm = self._mm
        self._dc.fm = self._fm
        
        self._last_render = self._get_tick()
        self._quit = 0
        
        self._zbuffer = [0 for _ in range((self._size[0] * self._size[1]))]
        
        self._view = None
    def _get_tick(self):
        microseconds = datetime.datetime.now().microsecond
        seconds = datetime.datetime.now().second
        return seconds * 1000 + microseconds / 1000
    def _process_event(self, e):
        if e.type == pygame.QUIT:
            self._quit = 1
        if self._view:
            self._view.process_event(e, (0, 0))
    def _render(self, dt):
        self._dc.screen.fill((0, 0, 0))
        if self._view != None:
            self._view.render(dt, self._dc, (0, 0))
        fps_text = "%02i fps" % (1000.0 / dt)
        fps = self._fm.render_font("lucidaconsole", 12, fps_text, (255, 255, 255))
        fps_pos = fps.get_rect(top=5, left=5)
        self._dc.screen.blit(fps, fps_pos)
    def push_view(self, view):
        self._view = view
    def quit(self):
        self._quit = 1
    def start(self):
        self._l.log("Starting engine...")
        self._dc.start()
        #loop da loop
        while not self._quit:
            for event in pygame.event.get():
                self._process_event(event)
            #limit to 60 fps because this is ghey
            time.sleep(1/60.0)
            new_render = self._get_tick()
            delta = new_render - self._last_render
            
            if self._view_controller:
                self._view_controller.tick(delta)
            
            self._render(delta)
            
            self._dc.flip()
            self._last_render = new_render
            
        self._l.log("Exiting gracefully... (woohoo!)")