gummworld2.basicmaprenderer (version $Id: basicmaprenderer.py 433 2014-03-21 03:39:35Z stabbingfinger@gmail.com $) | index c:\cygwin64\home\bw\dev\python\dist\gummworld2\gamelib\gummworld2\basicmaprenderer.py |
basicmaprenderer.py - Basic Map Renderer module for Gummworld2.
Defines the BasicMapRenderer, which serves an intermediate batch of tiles
for efficient rendering. This class is intended to be used instead of
BasicMap's collapse() and reduce() functions. The renderer is compatible with
a collapsed map; it is simply offered as a replacement that is significantly
easier to use.
LAYERED MAPS
On maps with many layers and images with alpha the gain is quite significant.
There are two ways to use the renderer: managing background layers only, and
managing dynamic layers.
MANAGING BACKGROUND LAYERS ONLY
In this case background layers are static, so very little care is needed. Here
is an example game loop:
def update(self, dt):
if self.movex or self.movey:
State.camera.position += self.movex,self.movey
State.camera.update()
self.renderer.set_rect(center=State.camera.rect.center)
def draw(self, dt):
State.screen.clear()
self.renderer.draw_tiles()
draw_my_foreground()
State.screen.flip()
MANAGING DYNAMIC LAYERS
In this case dynamic layers have sprites or tiles being moved, added, and
removed. The renderer needs to be told when these events occur. This is done
via the renderer's set_dirty() method. Here is an example game loop:
def update(self, dt):
if self.movex or self.movey:
State.camera.position += self.movex,self.movey
State.camera.update()
def draw(self, interp):
State.screen.clear()
renderer = self.renderer
# If panning, mark the renderer's tiles dirty where avatar is.
panning = False
camera = State.camera
camera_rect = camera.rect
dirty_rect = self.dirty_rect
camera_center = camera_rect.center
if camera.target.rect.center != camera_center:
dirty_rect.center = camera_center
renderer.set_dirty(dirty_rect)
panning = True
# Set render's rect and draw the screen.
renderer.set_rect(center=camera_center)
renderer.draw_tiles()
# Must mark dirty rect before next call to draw(), otherwise avatar
# leaves little artifacts behind.
if panning:
renderer.set_dirty(dirty_rect)
State.screen.flip()
WHEN CAMERA TARGET IS THE AVATAR
It is normal to make the avatar the camera target so that the camera tracks
the avatar's movement. If the avatar is inserted into a map layer for the
renderer to draw, there are two issues that need to be dealt with:
interpolation jitters, and tile order.
INTERPOLATION JITTERS
The camera's interpolation of the map tiles causes the avatar to jitter. The
renderer needs to be told to defeat interpolation. This is done by setting the
camera target's anti_interp attribute, e.g.:
class Avatar:
def __init__(self):
self.anti_interp = True
TILE ORDER
If tile order in a layer is critical to proper rendering, the renderer must be
told to sort the layer. Tile order is normally critical in 2.5D
implementations. This is done by setting the layer's sort_key attribute, e.g.:
map = TiledMap('my backyard.tmx')
map.layers[i].sort_key = lambda obj: obj.rect.bottom
GARBAGE CLEANUP
The class attribute DEFAULT_LIFESPAN is the number of checks to perform
before retiring a tile that has not been displayed recently. Consider this a
crudely tunable garbage collector.
MESSY EDGES
If an occasional black band appears at the edge of the display while
scrolling, increase the value of max_scroll_speed to the value of the
largest step (x or y) in use. This value can be changed situationally,
on the fly, without significant overhead. This issue is due to interpolation
of large scroll steps.
Modules | ||||||
|
Classes | ||||||||||||||
|
Data | ||
__all__ = ['BasicMapRenderer', 'BasicMapRendererTile'] __author__ = 'Gummbum, (c) 2011-2014' __version__ = '$Id: basicmaprenderer.py 433 2014-03-21 03:39:35Z stabbingfinger@gmail.com $' |
Author | ||
Gummbum, (c) 2011-2014 |