[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Feature request: fill with texture



fred@frad.org wrote:
> It'd be very useful with a function to "flood fill" a surface (or section
> of a surface) with a (repeating) texture. Of course this can be done with
> some Python code and regular blitting, but I'd rather not (mainly for
> efficiency).

Same here. Difference is that I have already written the code:

def tiled_blit(src, dest, destrect=None, offset=(0, 0), srcrect=None):
    """Repeats blitting src to dest until destrect is filled."""
    if not destrect:
        destrect = dest.get_rect()
    if srcrect:
        size = srcrect.size
        s = pygame.Surface(size)
        s.blit(src, (0, 0), srcrect)
        src = s
    else:
        srcrect = src.get_rect()
        size = srcrect.size

    ofx, ofy = offset
    for x in xrange(destrect.left, destrect.right + ofx, size[0]):
        for y in xrange(destrect.top, destrect.bottom + ofy, size[1]):
            blitpos = (x-ofx, y-ofy)
            blitrect = pygame.Rect(blitpos + srcrect.size)
            if not destrect.contains(blitrect): # we're on the edge of destrect
                trueblitrect = blitrect.clip(destrect) # stay within the lines
                trueblitpos = trueblitrect.topleft
                localsrcrect = trueblitrect.size
                srcpos = [trueblitpos[i]-blitpos[i] for i in (0, 1)]
                srcsize = trueblitrect.size
                dest.blit(src, trueblitpos, pygame.Rect(srcpos, srcsize))
            else:
                dest.blit(src, blitpos)

Maybe I should send this to the PCR. Of course, this should be done in C.
Caveat: I don't know C.

Gerrit.

-- 
247. If any one hire an ox, and put out its eye, he shall pay the owner
one-half of its value.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
	http://www.sp.nl/