Module: Rubygame::Sprites::UpdateGroup
- Defined in:
- lib/rubygame/sprite.rb
Overview
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:
-
#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.
Instance Attribute Summary collapse
-
#dirty_rects ⇒ Object
Returns the value of attribute dirty_rects.
Class Method Summary collapse
-
.extend_object(obj) ⇒ Object
Defines @dirty_rects when an existing object is extended.
Instance Method Summary collapse
-
#draw(dest) ⇒ Object
call-seq: draw(dest) -> Array.
-
#initialize ⇒ Object
Initialize the Group, calling
super
and defining @dirty_rects. -
#undraw(dest, background) ⇒ Object
Draw over part of
dest
with image data from the corresponding part ofbackground
.
Instance Attribute Details
#dirty_rects ⇒ Object
Returns the value of attribute dirty_rects.
389 390 391 |
# File 'lib/rubygame/sprite.rb', line 389 def dirty_rects @dirty_rects end |
Class Method Details
.extend_object(obj) ⇒ Object
Defines @dirty_rects when an existing object is extended.
392 393 394 395 |
# File 'lib/rubygame/sprite.rb', line 392 def UpdateGroup.extend_object(obj) super obj.dirty_rects = [] end |
Instance Method Details
#draw(dest) ⇒ Object
call-seq: draw(dest) -> Array
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.
408 409 410 411 412 413 414 415 |
# 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 |
#initialize ⇒ Object
Initialize the Group, calling super
and defining @dirty_rects.
398 399 400 401 |
# File 'lib/rubygame/sprite.rb', line 398 def initialize super @dirty_rects = [] end |
#undraw(dest, background) ⇒ Object
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).
421 422 423 424 425 |
# File 'lib/rubygame/sprite.rb', line 421 def undraw(dest,background) self.each { |sprite| @dirty_rects.push( sprite.undraw(dest, background) ) } end |