Class: Airsprite::SpriteSheet
- Inherits:
-
Object
- Object
- Airsprite::SpriteSheet
- Defined in:
- lib/airsprite/base.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#max_side ⇒ Object
Returns the value of attribute max_side.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#sprites ⇒ Object
Returns the value of attribute sprites.
-
#surface ⇒ Object
Returns the value of attribute surface.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(name, directory) ⇒ SpriteSheet
constructor
A new instance of SpriteSheet.
- #output ⇒ Object
- #place ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, directory) ⇒ SpriteSheet
Returns a new instance of SpriteSheet.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/airsprite/base.rb', line 38 def initialize(name, directory) self.name = name self.path = directory self.sprites = [] Dir["#{self.path}*/"].each do |dir| self.sprites += [Sprite.new(self, dir.split('/').last, dir)] end Dir["#{self.path}*.png"].each do |file| self.sprites += [Sprite.new(self, File.basename(file, '.png'), file)] end end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
36 37 38 |
# File 'lib/airsprite/base.rb', line 36 def height @height end |
#max_side ⇒ Object
Returns the value of attribute max_side.
36 37 38 |
# File 'lib/airsprite/base.rb', line 36 def max_side @max_side end |
#name ⇒ Object
Returns the value of attribute name.
35 36 37 |
# File 'lib/airsprite/base.rb', line 35 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
35 36 37 |
# File 'lib/airsprite/base.rb', line 35 def path @path end |
#sprites ⇒ Object
Returns the value of attribute sprites.
35 36 37 |
# File 'lib/airsprite/base.rb', line 35 def sprites @sprites end |
#surface ⇒ Object
Returns the value of attribute surface.
36 37 38 |
# File 'lib/airsprite/base.rb', line 36 def surface @surface end |
#width ⇒ Object
Returns the value of attribute width.
36 37 38 |
# File 'lib/airsprite/base.rb', line 36 def width @width end |
Instance Method Details
#output ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/airsprite/base.rb', line 125 def output frames = sprites.map(&:animations).flatten.map(&:frames).flatten.sort_by(&:area) draw = Magick::Draw.new image = Magick::Image.new(@width, @height) draw.matte(0, 0, Magick::ResetMethod) frames.map do |frame| next if frame.x.nil? || frame.y.nil? #draw.composite(frame.data, frame.x, frame.y, Magick::CopyCompositeOp) draw.composite(frame.x, frame.y, frame.width, frame.height, frame.data, Magick::CopyCompositeOp) #image.store_pixels(frame.x, frame.y, frame.width, frame.height, frame.data.get_pixels(0, 0, frame.width, frame.height)) end STDERR.puts "writing to #{self.path[0..-2]}.itx" File.open("#{self.path[0..-2]}.itx", 'w') {|f| f.write(self.to_s)} STDERR.puts "writing to #{self.path[0..-2]}.png" image.set_channel_depth(Magick::AllChannels, 8) draw.draw(image) image.write("#{self.path[0..-2]}.png") end |
#place ⇒ Object
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/airsprite/base.rb', line 63 def place # biggest first frames = sprites.map(&:animations).flatten.map(&:frames).flatten.sort_by(&:area).reverse # figure out lowest possible power of 2 @surface = frames.map(&:area).inject(0) {|sum, area| sum + area} @max_side = [frames.map(&:width), frames.map(&:height)].flatten.sort.last @width = 0 @height = 0 STDERR.puts "surface needed: #{@surface}" Airsprite.twos.map do |power_of_two| # lowest two must be >= biggest sprite size next unless power_of_two >= @max_side if (@surface / power_of_two) <= power_of_two if (power_of_two * Airsprite.twos[Airsprite.twos.index(power_of_two) - 1]) > @surface @width = power_of_two @height = Airsprite.twos[Airsprite.twos.index(power_of_two) - 1] else @width = power_of_two @height = power_of_two end STDERR.puts "surface generated: #{@width * @height}" break end end # set these sprite frames up! rects = [Rect.new(@width, @height, 0, 0)] # place frames frames.map do |frame| # find first square that can contain this sprite raise "No rects left to fill" if rects.empty? STDERR.puts "\n\n" STDERR.puts frame.to_short_s STDERR.puts rects.map(&:to_s).join("\n") rects.map do |rect| if rect.area >= frame.area && rect.width >= frame.width && rect.height >= frame.height frame.x = rect.x frame.y = rect.y # delete rect from list rects.delete(rect) unless rect.height == frame.height && rect.width == frame.width if rect.height == frame.height rects << Rect.new(rect.width - frame.width, rect.height, rect.x + frame.width, rect.y) elsif rect.width == frame.width rects << Rect.new(rect.width, rect.height - frame.height, rect.x, rect.y + frame.height) else rects << Rect.new(frame.width, rect.height - frame.height, rect.x, rect.y + frame.height) rects << Rect.new(rect.width - frame.width, rect.height, rect.x + frame.width, rect.y) end end break end end end end |
#to_s ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/airsprite/base.rb', line 52 def to_s <<-EOS SpriteSheet { name "#{name}" #{sprites.map(&:to_s).join} } EOS end |