Class: Doom::Wad::Sprite
- Inherits:
-
Object
- Object
- Doom::Wad::Sprite
- Defined in:
- lib/doom/wad/sprite.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#left_offset ⇒ Object
readonly
Returns the value of attribute left_offset.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#top_offset ⇒ Object
readonly
Returns the value of attribute top_offset.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
-
.load(wad, lump_name) ⇒ Object
Load a sprite from a WAD patch lump.
Instance Method Summary collapse
- #column_pixels(x) ⇒ Object
-
#initialize(name, width, height, left_offset, top_offset, columns) ⇒ Sprite
constructor
A new instance of Sprite.
Constructor Details
#initialize(name, width, height, left_offset, top_offset, columns) ⇒ Sprite
Returns a new instance of Sprite.
8 9 10 11 12 13 14 15 |
# File 'lib/doom/wad/sprite.rb', line 8 def initialize(name, width, height, left_offset, top_offset, columns) @name = name @width = width @height = height @left_offset = left_offset @top_offset = top_offset @columns = columns end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
6 7 8 |
# File 'lib/doom/wad/sprite.rb', line 6 def height @height end |
#left_offset ⇒ Object (readonly)
Returns the value of attribute left_offset.
6 7 8 |
# File 'lib/doom/wad/sprite.rb', line 6 def left_offset @left_offset end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/doom/wad/sprite.rb', line 6 def name @name end |
#top_offset ⇒ Object (readonly)
Returns the value of attribute top_offset.
6 7 8 |
# File 'lib/doom/wad/sprite.rb', line 6 def top_offset @top_offset end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
6 7 8 |
# File 'lib/doom/wad/sprite.rb', line 6 def width @width end |
Class Method Details
.load(wad, lump_name) ⇒ Object
Load a sprite from a WAD patch lump
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/doom/wad/sprite.rb', line 22 def self.load(wad, lump_name) entry = wad.directory.find { |e| e.name == lump_name } return nil unless entry data = wad.read_lump_at(entry) return nil if data.size < 8 width = data[0, 2].unpack1('v') height = data[2, 2].unpack1('v') left_offset = data[4, 2].unpack1('s<') top_offset = data[6, 2].unpack1('s<') # Read column offsets column_offsets = [] width.times do |i| column_offsets << data[8 + i * 4, 4].unpack1('V') end # Read columns columns = [] width.times do |x| column = Array.new(height, nil) # nil = transparent offset = column_offsets[x] # Read posts for this column loop do break if offset >= data.size row_start = data[offset].ord break if row_start == 255 # End of column marker post_height = data[offset + 1].ord break if post_height == 0 || offset + 3 + post_height > data.size # Skip padding byte, read pixels, skip trailing padding post_height.times do |i| y = row_start + i if y < height column[y] = data[offset + 3 + i].ord end end offset += post_height + 4 # row_start + count + padding + pixels + padding end columns << column end new(lump_name, width, height, left_offset, top_offset, columns) end |
Instance Method Details
#column_pixels(x) ⇒ Object
17 18 19 |
# File 'lib/doom/wad/sprite.rb', line 17 def column_pixels(x) @columns[x] || [] end |