Forums Archived

The forums are being archived. See this topic for more information.

Sprite animation / dirty rects problem

Get help and support with Rubygame

Sprite animation / dirty rects problem

Postby antihcl » Tue Jun 30, 2009 11:51 am

Hello! First off I'd like to thank everyone who's worked on rubygame for this fantastic library. I'm using it to learn more about Ruby while having some fun.

My first project is a simple Lunar Lander game, which I've based on the code in samples/demo_rubygame.rb. I'm having a problem with sprite drawing.. it's being blanked incorrectly, but I can't find exactly where the problem is. Can anyone help find it?

p.s. The controls are just simple arrow key movement for now.
Attachments
lander.zip
(8.37 KiB) Downloaded 177 times
lander.tar.gz
(6.99 KiB) Downloaded 168 times
antihcl
 
Posts: 9
Joined: Sat Jun 27, 2009 1:43 pm

Re: Sprite animation / dirty rects problem

Postby jacius » Tue Jun 30, 2009 6:49 pm

You've actually just found a bug in demo_rubygame.rb. :lol:

In your Setup#step (demo_rubygame.rb's Game#step), the tick event should occur before the DrawSprites event. That way, the sprite is undrawn at the old position, then updated (possibly changing position), then drawn at the new position. The way I had it before (tick after DrawSprites) would undraw and draw it at the new position, meaning some of the old position wouldn't be undrawn, hence the trail. (The problem didn't show in demo_rubygame.rb because the panda sprites have some padding around the outside of the sprite, so even at the new position, they overlapped all the visible parts of the old position.)

The correct code is:

Code: Select all
  #Do everything needed for one frame.
  def step
    @queue << UndrawSprites.new( @screen, @background )
    @queue.fetch_sdl_events
    @queue << $game.clock.tick
    @queue << DrawSprites.new( @screen )
    @queue.each do |event|
      handle( event )
    end
    @screen.update()
  end


P.S. I had to add "include Rubygame::EventActions" to setup.rb to get your game to run, due to scope issues (it couldn't find MethodAction). You may also want to add "include Rubygame::EventTriggers", and possibly remove that from game.rb.
User avatar
jacius
Site Admin
 
Posts: 131
Joined: Fri Feb 06, 2009 11:13 pm

Re: Sprite animation / dirty rects problem

Postby antihcl » Tue Jun 30, 2009 7:42 pm

Ah-ha! Thank you very much. :D I'm looking forward to having a lot of fun with rubygame. Very good point about moving the include over as well. On to the fun stuff!
antihcl
 
Posts: 9
Joined: Sat Jun 27, 2009 1:43 pm

Re: Sprite animation / dirty rects problem

Postby shawn42 » Tue Jun 30, 2009 8:23 pm

in the tar file I downloaded, I had to add
class Setup
include EventHandler::HasEventHandler
include EventActions

to get it to work.

After that, I saw the undraw problems you were having..
from the rdocs at http://docs.rubygame.org/rdoc/2.5.2/ for Rubygame::Sprites::UpdateGroup:
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.


I haven't worked with the new event hooks system, but my guess is that you're not updating in between the undraw and the draw. You are probably updating, undrawing, then drawing. Which means that your undraw has undrawn itself at the new position (so you see artifacts).

Hope this helps, but hop on freenode in #rubygame for a better answer.
User avatar
shawn42
 
Posts: 109
Joined: Tue Feb 10, 2009 8:22 pm


Return to Help & Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron