Class: Gosu::BlockAllocator

Inherits:
Object
  • Object
show all
Defined in:
lib/gosu_android/graphics/blockAllocator.rb

Defined Under Namespace

Classes: Block, Impl

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ BlockAllocator

Returns a new instance of BlockAllocator.



48
49
50
# File 'lib/gosu_android/graphics/blockAllocator.rb', line 48

def initialize(width, height)
  @pimpl = Impl.new(width, height, nil, 0, 0, width, height)
end

Instance Method Details

#alloc(a_width, a_height) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gosu_android/graphics/blockAllocator.rb', line 60

def alloc(a_width, a_height)
  #The rect wouldn't even fit onto the texture!
  if a_width > width || a_height > height
      return [false]
  end     
  #We know there's no space left.
  if a_width > @pimpl.max_w && a_height > @pimpl.max_h
      return [false]
  end
  #Start to look for a place next to the last returned rect. Chances are
  #good we'll find a place there.
  b = Block.new(@pimpl.first_x, @pimpl.first_y, a_width, a_height)
  if @pimpl.is_block_free(b)
    @pimpl.mark_block_used(b, a_width, a_height)
    return [true, b]
  end

  b.top = 0
  b.left = 0
  #Brute force: Look for a free place on this texture.
  while(b.top <= (height - a_height)) do 
    while(b.left <= (width - a_width)) do
      if @pimpl.is_block_free(b)
        #Found a nice place!
  
        #Try to make up for the large for()-stepping.
        while (b.top > 0 and @pimpl.is_block_free(Block.new(b.left, b.top - 1, a_width, a_height))) do
            b.top -= 1
        end    
        while (b.left > 0 and @pimpl.is_block_free(Block.new(b.left - 1, b.top, a_width, a_height))) do
            b.left -= 1
        end
        @pimpl.mark_block_used(b, a_width, a_height)
        return [true, b]          
      end
      b.top += 16
      b.left += 8
    end      
  end

  #So there was no space for the bitmap. Remember this for later.
  @pimpl.max_w = a_width - 1
  @pimpl.max_h = a_height - 1
  return [false]           
end

#free(left, top) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gosu_android/graphics/blockAllocator.rb', line 106

def free (left, top)
  @pimpl.blocks.delete_if do |block|
    if block.left == left and block.top == top
      @pimpl.max_w = @pimpl.max_w - 1
      @pimpl.max_h = @pimpl.max_h - 1
      true
    else
      false  
    end
  end
end

#heightObject



56
57
58
# File 'lib/gosu_android/graphics/blockAllocator.rb', line 56

def height
  @pimpl.height
end

#widthObject



52
53
54
# File 'lib/gosu_android/graphics/blockAllocator.rb', line 52

def width
  @pimpl.width
end