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

Re: [pygame] fundamental (stupid) question about blitting part of a background image



On 2/3/06, Horst F. JENS <horst.jens@xxxxxx> wrote:
> i can "paint" my stars but while doing so i slowly destroy my background
> image. (code see below) I need a method to copy/clipping a part of my
> background image at a given position but i found no examples how to do that.
> If i use the out-commented code behind the ## chars all i get is a big,
> black rectangle without nether stars nor background in it.
> Could you give me some hints ?
> thank you in advance,
>   /Horst
>
Hi Horst,
   What you are talking about is commonly part of what's called a
"dirty rectangle" system, so if you want to do research on this kind
of thing, those are probably the best keywords to use. Dirty
Rectangles is a great way to do things if you are using software
rendering & not scrolling the screen - in fact it's often the only way
to get really great frame rates with software rendering at all.

I think what you are looking for is the "area" parameter of
surface.blit, it's what allows you to draw just a portion of one
surface to another.

I'm assuming your stars are blit'ing to the screen, and when they do
that, surface.blit returns a rectangle. In order to erase that star,
you can pass that returned rectangle in to a blit from the background
to the screen.

so like to draw a star you might:
 starrect = screen.blit(starimage, starpos)

then later to clear the background behind that star you might:
 screen.blit(background, [0,0], starrect)

also, I think dirty rect systems are best when you wrap the screen in
some object that handles all that stuff. That way you don't have to
have to implement special erasing funcs for every thing you ever draw.
Here's like a terrible little pseduo-python thing to describe what I
mean...

 class DirtyRectScreen:
    def __init__(self, screen):
         self.rect_list = ()
         self.screen = screen

    def blit(self, source, dest):
         self.rect_list.append(self.screen.blit(source, dest))

    def erase(self, background_image):
         for rect in self.rect_list:
              self.screen.blit(background_image, (0,0), rect)
         self.rect_list = ()

     def update(self):
          self.screen.update(self.rect_list)