Changeset 40:fa1bb94cfa76


Ignore:
Timestamp:
Apr 3, 2011, 6:24:47 PM (9 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Message:

Re-factor drawing to blit to a clip rect in the One True Level Surface before blitting that to the screen.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • scripts/level-editor

    r35 r40  
    2020def run(levelname):
    2121    clock = pygame.time.Clock()
    22     surface = pygame.display.get_surface()
     22    screen_surface = pygame.display.get_surface()
    2323    level = Level(levelname)
     24    level_surface = level.get_surface()
    2425    position = [0, 0]
    2526    while True:
     
    4849        position[1] = min(position[1], level.pixel_size[1] - SCREEN[1])
    4950
    50         level.draw(surface, position)
     51        clip_rect = pygame.Rect(position, screen_surface.get_size())
     52        level_surface.set_clip(clip_rect)
     53        level.draw(level_surface)
     54        screen_surface.blit(level_surface, (0, 0), area=clip_rect)
     55
    5156        pygame.display.flip()
    5257        clock.tick(FRAMERATE)
  • skaapsteker/level.py

    r35 r40  
    22
    33from pygame import Rect, Surface, constants
     4from pygame.sprite import Group
     5from pygame.locals import SRCALPHA, HWSURFACE
    46
    57import data
     
    4648    def build_tiles(self):
    4749        self.tileset = TileSet(self.level_data['tileset'])
    48         self.tiles = []
     50        self.tiles = Group()
    4951        tile_data = self.level_data['tiles']
    5052        self.tile_size = (len(tile_data[0]), len(tile_data))
     
    5456                tile = self.tileset.get_tile(char, (x, y))
    5557                if tile:
    56                     self.tiles.append(tile)
     58                    self.tiles.add(tile)
    5759        print "level:", self.tile_size, self.pixel_size
    5860
     
    6971        print self.sprites
    7072
     73    def get_surface(self):
     74        return Surface(self.pixel_size, flags=SRCALPHA)
     75
    7176
    7277    def get_background_pos(self, pos, display_size):
     
    8085
    8186
    82     def draw_background(self, surface, pos):
    83         bg_pos = self.get_background_pos(pos, surface.get_size())
    84         surface.blit(self.background.subsurface(Rect(bg_pos, surface.get_size())), (0, 0))
     87    def draw_background(self, surface):
     88        clip_rect = surface.get_clip()
     89        bg_pos = self.get_background_pos(clip_rect.topleft, clip_rect.size)
     90        surface.blit(self.background, clip_rect.topleft, area=Rect(bg_pos, clip_rect.size))
    8591
    8692
    87     def draw_tiles(self, surface, pos):
    88         colliderect = surface.get_rect().move(pos)
    89         for tile in self.tiles:
    90             if colliderect.colliderect(tile.rect):
    91                 surface.blit(tile.image, (tile.rect.left - pos[0], tile.rect.top - pos[1]))
     93    def draw_tiles(self, surface):
     94        self.tiles.draw(surface)
    9295
    9396
    94     def draw(self, surface, pos):
    95         self.draw_background(surface, pos)
    96         self.draw_tiles(surface, pos)
     97    def draw(self, surface):
     98        self.draw_background(surface)
     99        self.draw_tiles(surface)
    97100
    98101
  • skaapsteker/levelscene.py

    r16 r40  
    33import engine
    44import level
     5import physics
     6import pygame
    57
    68class LevelScene(engine.Scene):
     
    810    def __init__(self, leveldef):
    911        self._level = level.Level(leveldef)
     12        self._level_surface = self._level.get_surface()
    1013        self._pos = (0, 0)
     14        self._world = physics.World()
    1115
    12     def draw(self, surface):
    13         self._level.draw(surface, self._pos)
     16    def draw(self, screen_surface):
     17        self._world.update()
     18
     19        clip_rect = pygame.Rect(self._pos, screen_surface.get_size())
     20        self._level_surface.set_clip(clip_rect)
     21
     22        self._level.draw(self._level_surface)
     23        self._world.draw(self._level_surface)
     24
     25        screen_surface.blit(self._level_surface, (0, 0), area=clip_rect)
    1426
    1527    def dispatch(self, ev):
Note: See TracChangeset for help on using the changeset viewer.