Module Rubygame::Sprites::UpdateGroup
In: lib/rubygame/sprite.rb

UpdateGroup is a mix-in module that extends Group to allow it to “erase” (i.e. draw over with a background Surface) and re-draw each sprite in its new position. This eliminates the “trail” of images that sprites would otherwise leave as they move around.

UpdateGroup adds a new attribute, @dirty_rects, which is an Array storing all Rects which need to be updated, including the old positions of the sprites. This attribute is returned when UpdateGroup#draw is called, so that it can be used to update the parts of the Screen that have changed.

The general order of calls each frame should be:

  1. undraw; clear the old positions of the sprites.
  2. update; update the sprites to their new positions.
  3. draw; draw the sprites in their new positions.

This module can extend either a class or an already-existing Group instance (either empty or with members) without any special preparation.

Methods

draw   extend_object   new   undraw  

Attributes

dirty_rects  [RW] 

Public Class methods

Defines @dirty_rects when an existing object is extended.

[Source]

# File lib/rubygame/sprite.rb, line 392
                        def UpdateGroup.extend_object(obj)
                                super
                                obj.dirty_rects = []
                        end

Initialize the Group, calling super and defining @dirty_rects.

[Source]

# File lib/rubygame/sprite.rb, line 398
                        def initialize
                                super
                                @dirty_rects = []
                        end

Public Instance methods

Draw every sprite on Surface dest. See Group#draw. Returns an Array of Rects representing the portions of dest which were affected by the last undraw and this draw.

[Source]

# File lib/rubygame/sprite.rb, line 408
                        def draw(dest)
                                self.each { |sprite| 
                                        @dirty_rects.push( sprite.draw(dest) ) 
                                }
                                rects = @dirty_rects
                                @dirty_rects = []
                                return rects
                        end

Draw over part of dest with image data from the corresponding part of background. For best results, background should be at least as big as dest (or, rather, the part of dest that will ever be drawn over).

[Source]

# File lib/rubygame/sprite.rb, line 421
                        def undraw(dest,background)
                                self.each { |sprite|
                                        @dirty_rects.push( sprite.undraw(dest, background) )
                                }
                        end

[Validate]