Page 1 of 1

Displaying Background and Objects

PostPosted: Fri Sep 24, 2010 11:18 pm
by Fridgecrisis
I'm working on an RPG project, and I'm having a problem with displaying the background map and the objects on it correctly. My current approach is to have two surfaces: a background surface containing a .png image, and a second surface for displaying objects. At the beginning of the program, I tell the background surface to draw to the screen, only once. Then on each update, the object surface fills with a certain grey color I've set as that surface's colorkey (so it's as good as transparent) and then I draw my objects onto that surface.

So you'd think this would let me keep my background image on the screen and have this second, mostly transparent surface updating over it, but the background just looks black when I run the program. I currently have the background drawing again on each frame, but that slows down the program considerably.

I'm wondering what the "theory" is behind drawing and updating backgrounds and the objects over them. What's the best approach?

Here's the applicable part of my code. A second class that inherits from this one supplies the name of the map, a couple objects, and turns @active to true.

Code: Select all
class Map
 
  def initialize(name)
    @background = Rubygame::Surface.load("#{name}.png")
    @w = @background.w
    @h = @background.h
    @surface = Rubygame::Surface.new([@w, @h])
    @surface.colorkey = [100, 100, 100]
    @screen_rect = Rubygame::Rect.new(0, 0, $screen.w, $screen.h)
    @objects = {}
    @active = false
    draw_background
  end

  def draw
    return if @active == false
    @surface.fill([100, 100, 100])
    draw_objects
    @background.blit($screen, [0, 0], [0, 0, @w, @h]) #This makes the program run slower, but it's the only way it seems to work...
    @surface.blit($screen, [0, 0], @screen_rect)
  end
 
  def draw_objects
    return if @active == false
    @objects.each_value do |obj|
      obj.draw(@surface)
    end
  end
 
  def draw_background
    @background.blit($screen, [0, 0], [0, 0, @w, @h])
  end
 
end


EDIT--
Okay, I discovered a screen.fill in my main script. Removing it allowed the background to be seen, but everything was being drawn on top of the old stuff, creating hundreds of little guys all over the screen.

I've been thinking, and it seems that I will need to draw the background to the screen on every frame, whether I want to or not. But why does it affect the program's performance so much? The surface is a simple .png file, 1000 x 1000 pixels, stored in an instance variable. What am I doing wrong?