Class: Doom::TextureLoader

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

Overview

TextureLoader is responsible for loading and parsing texture data from a WAD file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wad_loader) ⇒ TextureLoader

Initialize a new TextureLoader with the given WADLoader

Parameters:

  • wad_loader (WADLoader)

    the WAD loader to use



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

def initialize(wad_loader)
  @wad_loader = wad_loader
  @textures = {}
  @patch_names = []
  load_patch_names
  load_textures
end

Instance Attribute Details

#patch_namesObject (readonly)

Returns the value of attribute patch_names.



6
7
8
# File 'lib/doom/texture_loader.rb', line 6

def patch_names
  @patch_names
end

#texturesObject (readonly)

Returns the value of attribute textures.



6
7
8
# File 'lib/doom/texture_loader.rb', line 6

def textures
  @textures
end

Instance Method Details

#get_texture(name) ⇒ Hash?

Get a specific texture by name

Parameters:

  • name (String)

    the name of the texture

Returns:

  • (Hash, nil)

    the texture data or nil if not found



121
122
123
# File 'lib/doom/texture_loader.rb', line 121

def get_texture(name)
  @textures[name]
end

#load_patch_namesObject

Load the PNAMES lump which contains patch names



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/doom/texture_loader.rb', line 19

def load_patch_names
  pnames_data = @wad_loader.get_lump("PNAMES")
  return unless pnames_data
  
  # First 4 bytes is the number of patch names
  num_patches = pnames_data[0..3].unpack("V")[0]
  
  # Each patch name is 8 bytes
  num_patches.times do |i|
    offset = 4 + (i * 8)
    patch_name = pnames_data[offset..offset + 7].unpack("Z*")[0]
    @patch_names << patch_name
  end
end

#load_texture_lump(lump_name) ⇒ Object

Load a specific texture lump

Parameters:

  • lump_name (String)

    the name of the texture lump (TEXTURE1 or TEXTURE2)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/doom/texture_loader.rb', line 42

def load_texture_lump(lump_name)
  texture_data = @wad_loader.get_lump(lump_name)
  return unless texture_data
  
  # First 4 bytes is the number of textures
  num_textures = texture_data[0..3].unpack("V")[0]
  
  # Next num_textures * 4 bytes are offsets to each texture definition
  offsets = []
  num_textures.times do |i|
    offset = 4 + (i * 4)
    texture_offset = texture_data[offset..offset + 3].unpack("V")[0]
    offsets << texture_offset
  end
  
  # Parse each texture definition
  offsets.each do |offset|
    parse_texture_definition(texture_data, offset)
  end
end

#load_texturesObject

Load the TEXTURE1 and TEXTURE2 lumps which contain texture definitions



35
36
37
38
# File 'lib/doom/texture_loader.rb', line 35

def load_textures
  load_texture_lump("TEXTURE1")
  load_texture_lump("TEXTURE2")
end

#parse_texture_definition(texture_data, offset) ⇒ Object

Parse a texture definition

Parameters:

  • texture_data (String)

    the texture lump data

  • offset (Integer)

    the offset to the texture definition



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
# File 'lib/doom/texture_loader.rb', line 66

def parse_texture_definition(texture_data, offset)
  # Texture definition structure:
  # - 8 bytes: texture name
  # - 4 bytes: flags (unused)
  # - 2 bytes: width
  # - 2 bytes: height
  # - 4 bytes: column directory (unused)
  # - 2 bytes: number of patches
  # - patches: array of patch definitions
  
  texture_name = texture_data[offset..offset + 7].unpack("Z*")[0]
  # Skip flags (4 bytes)
  width = texture_data[offset + 12..offset + 13].unpack("v")[0]
  height = texture_data[offset + 14..offset + 15].unpack("v")[0]
  # Skip column directory (4 bytes)
  num_patches = texture_data[offset + 20..offset + 21].unpack("v")[0]
  
  patches = []
  
  # Parse each patch definition
  num_patches.times do |i|
    patch_offset = offset + 22 + (i * 10)
    
    # Patch definition structure:
    # - 2 bytes: x offset
    # - 2 bytes: y offset
    # - 2 bytes: patch number (index into PNAMES)
    # - 2 bytes: step dir (unused)
    # - 2 bytes: color map (unused)
    
    x_offset = texture_data[patch_offset..patch_offset + 1].unpack("s<")[0]
    y_offset = texture_data[patch_offset + 2..patch_offset + 3].unpack("s<")[0]
    patch_num = texture_data[patch_offset + 4..patch_offset + 5].unpack("v")[0]
    
    patch_name = @patch_names[patch_num] if patch_num < @patch_names.size
    
    patches << {
      x_offset: x_offset,
      y_offset: y_offset,
      patch_num: patch_num,
      patch_name: patch_name
    }
  end
  
  @textures[texture_name] = {
    name: texture_name,
    width: width,
    height: height,
    patches: patches
  }
end

#texture_exists?(name) ⇒ Boolean

Check if a texture exists

Parameters:

  • name (String)

    the name of the texture

Returns:

  • (Boolean)

    true if the texture exists



134
135
136
# File 'lib/doom/texture_loader.rb', line 134

def texture_exists?(name)
  @textures.key?(name)
end

#texture_namesArray<String>

List all texture names

Returns:

  • (Array<String>)

    array of texture names



127
128
129
# File 'lib/doom/texture_loader.rb', line 127

def texture_names
  @textures.keys
end