Class: Doom::Wad::Texture

Inherits:
Object
  • Object
show all
Defined in:
lib/doom/wad/texture.rb

Defined Under Namespace

Classes: PatchRef

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, width, height, patch_refs) ⇒ Texture



10
11
12
13
14
15
16
# File 'lib/doom/wad/texture.rb', line 10

def initialize(name, width, height, patch_refs)
  @name = name
  @width = width
  @height = height
  @patch_refs = patch_refs
  @columns = nil
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/doom/wad/texture.rb', line 8

def height
  @height
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/doom/wad/texture.rb', line 8

def name
  @name
end

#patch_refsObject (readonly)

Returns the value of attribute patch_refs.



8
9
10
# File 'lib/doom/wad/texture.rb', line 8

def patch_refs
  @patch_refs
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/doom/wad/texture.rb', line 8

def width
  @width
end

Class Method Details

.load_all(wad) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/doom/wad/texture.rb', line 27

def self.load_all(wad)
  pnames = load_pnames(wad)
  textures = {}

  %w[TEXTURE1 TEXTURE2].each do |lump_name|
    data = wad.read_lump(lump_name)
    next unless data

    parse_texture_lump(data).each do |tex|
      textures[tex.name] = tex
    end
  end

  { textures: textures, pnames: pnames }
end

.load_pnames(wad) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/doom/wad/texture.rb', line 43

def self.load_pnames(wad)
  data = wad.read_lump('PNAMES')
  return [] unless data

  count = data[0, 4].unpack1('V')
  count.times.map do |i|
    data[4 + i * 8, 8].delete("\x00").upcase
  end
end

.parse_texture(data, offset) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/doom/wad/texture.rb', line 64

def self.parse_texture(data, offset)
  name = data[offset, 8].delete("\x00").upcase
  width = data[offset + 12, 2].unpack1('v')
  height = data[offset + 14, 2].unpack1('v')
  patch_count = data[offset + 20, 2].unpack1('v')

  patch_refs = patch_count.times.map do |i|
    po = offset + 22 + i * 10
    PatchRef.new(
      data[po, 2].unpack1('s<'),
      data[po + 2, 2].unpack1('s<'),
      data[po + 4, 2].unpack1('v')
    )
  end

  new(name, width, height, patch_refs)
end

.parse_texture_lump(data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/doom/wad/texture.rb', line 53

def self.parse_texture_lump(data)
  num_textures = data[0, 4].unpack1('V')
  offsets = num_textures.times.map do |i|
    data[4 + i * 4, 4].unpack1('V')
  end

  offsets.map do |offset|
    parse_texture(data, offset)
  end
end

Instance Method Details

#build_columnsObject



22
23
24
25
# File 'lib/doom/wad/texture.rb', line 22

def build_columns
  # Will be built when patches are composited
  nil
end

#columnsObject



18
19
20
# File 'lib/doom/wad/texture.rb', line 18

def columns
  @columns ||= build_columns
end