Changeset 349:c4285f19894c


Ignore:
Timestamp:
Sep 6, 2013, 1:53:28 PM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
rebase_source:
b75f8e03416af3e4e7890b98ef377027a2c1b325
Message:

Add a tiled renderer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/render.py

    r307 r349  
    22
    33import pygame
     4import pygame.locals as pgl
    45import pymunk
    56
    67from nagslang.options import options
     8from nagslang.utils import tile_surface
    79from nagslang.widgets.text import LabelWidget
    810
     
    185187        self.widget.rect.topleft = (x, y)
    186188        self.widget.draw(surface)
     189
     190
     191class TiledRenderer(Renderer):
     192    """Tile the given image to fit the given outline
     193
     194       Outline is assumed to be in pymunk coordinates"""
     195
     196    def __init__(self, outline, tile_image):
     197        self._tile_image = tile_image
     198        self.outline = outline
     199        self._tiled = None
     200
     201    def _make_surface(self, surface):
     202        if not self._tiled:
     203            size = surface.get_size()
     204            self._tiled = tile_surface(size, self._tile_image)
     205            mask = pygame.surface.Surface(size, pgl.SRCALPHA)
     206            mask.fill(pygame.color.Color(0, 0, 0, 0))
     207            pointlist = [pymunk.pygame_util.to_pygame(p, surface)
     208                         for p in self.outline]
     209            pygame.draw.polygon(mask, pygame.color.THECOLORS['white'],
     210                                pointlist, 0)
     211            self._tiled.blit(mask, (0, 0), special_flags=pgl.BLEND_RGBA_MULT)
     212        return self._tiled
     213
     214    def render(self, surface):
     215        tiled = self._make_surface(surface)
     216        surface.blit(tiled, (0, 0))
     217        super(TiledRenderer, self).render(surface)
Note: See TracChangeset for help on using the changeset viewer.