On Dec 12, 2007 3:18 PM, Patrick Mullen <[EMAIL PROTECTED]> wrote:
>
> On Dec 12, 2007 1:10 PM, Dan Krol <[EMAIL PROTECTED]> wrote:
> > I need to do something similar, but I think I did it by just blitting
> > the subsection of the original surface I needed when it's time to draw
> > (and no other times). Doesn't this not use extra memory as well?  Any
> > reason this is a bad idea?

I did the same thing. ( Using source Rect()'s instead of creating
subsurfaces. ). I created a new sprite group, derived from
RenderPlain() to use source rects.

Then I created a class Unit() which derived pygame.sprite.Sprite()
adding a new member .rect_source . So .rect is the destination
coordinates, and .rect_source is the source rect. Unit().update() will
get called on every sprite in the sprite group.

# usage: file: game.py : Game():
from RenderSrcRect import RenderSrcRect

        # your Game.__init__():
        self.unit_sprites = RenderSrcRect()
        self.player = Unit()

        # add units, player, etc...
        self.units_sprites.add( self.player )
        
        # your Game.draw():
        self.unit_sprites.draw( self.screen )
        
        # your Game.update():
        self.unit_sprites.update()

#!/usr/bin/python
# file: RenderSrcRect.py
# Author: jake bolton [created: 2007/11/14]
# About: RenderSrcRect
        # child of RenderPlain, adds src rect support
        
import pygame
from pygame.locals import *

class RenderSrcRect(pygame.sprite.RenderPlain):
        """custom sprite group rendering class. Uses src rects of sprite.
        
        the inheritance is: RenderPlain() == Group() which derives
AbstractGroup()
                
        members used from the sprites to blit:
                .image = the sprite's surface
                .rect = the blit dest rect
                .rect_source = the blit source rect
        """
        
        def __init__(self):
                """init"""
                pygame.sprite.RenderPlain.__init__(self)
        
        def draw(self, surface):
                """same as .RenderPlain.draw() except spr.rect_source is used 
to blit"""
                sprites = self.sprites()
                surface_blit = surface.blit
                for spr in sprites:
                        # blit with src rect
                        self.spritedict[spr] = surface_blit(spr.image, spr.rect,
spr.rect_source)
                self.lostsprites = []   


> >
>
> For organizational purposes, it may at some point be very unclear what
> this means:
>
> screen.blit(source.subsurface([30,60,100,15]),[30,60])
> Whereas this might make more sense:
>
> screen.blit(player_head,[30,60])
>
> Also, the little bit of memory used in predefining a subsurface is
> going to gain some performance increase, which in blitting you really
> need as much as you can get.

Are you saying a performance increase better than using source
Rect()'s like my example above. ( Or just that its slower to keep
re-creating the same subsurf every frame vs creating it one time? )

>
> Also, if you are meaning you do things thusly:
>
> screen.blit(source,[30,60],[30,60,100,15]) this probably is as fast
> and as memory concious, but to me is even more confusing because the
> area defined belongs to the source but comes AFTER the coordinates.  I
> would forget what that rect is associated with.
>
> But if it works for you, then do it that way :)
>

Either way you blit, take a look at sprite groups ( if you don't
already know about them. )

pygame sprite docs: http://www.pygame.org/docs/ref/sprite.html
sprite group tutorial:
http://kai.vm.bytemark.co.uk/~piman/writing/sprite-tutorial.shtml

-- 
Jake

Reply via email to