module Rubygame::Sprites::UpdateGroup
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 #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:
-
undraw; clear the old positions of the sprites.
-
update; update the sprites to their new positions.
-
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.
Attributes
Public Class Methods
Defines @dirty_rects when an existing object is extended.
# 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.
# File lib/rubygame/sprite.rb, line 398 def initialize super @dirty_rects = [] end
Public Instance Methods
Draw every sprite on Surface
dest
. See Rubygame::Sprites::Group#draw. Returns
an Array of Rects representing the portions of dest
which were
affected by the last undraw
and this draw.
# 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).
# File lib/rubygame/sprite.rb, line 421 def undraw(dest,background) self.each { |sprite| @dirty_rects.push( sprite.undraw(dest, background) ) } end