Class: Doom::Wad::Flat

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

Constant Summary collapse

WIDTH =
64
HEIGHT =
64
SIZE =
WIDTH * HEIGHT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pixels) ⇒ Flat

Returns a new instance of Flat.



20
21
22
23
# File 'lib/doom/wad/flat.rb', line 20

def initialize(name, pixels)
  @name = name
  @pixels = pixels
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/doom/wad/flat.rb', line 10

def name
  @name
end

#pixelsObject (readonly)

Returns the value of attribute pixels.



10
11
12
# File 'lib/doom/wad/flat.rb', line 10

def pixels
  @pixels
end

Class Method Details

.load(wad, name) ⇒ Object

Raises:



42
43
44
45
46
47
48
# File 'lib/doom/wad/flat.rb', line 42

def self.load(wad, name)
  data = wad.read_lump(name)
  return nil unless data
  raise Error, "Invalid flat size: #{data.size}" unless data.size == SIZE

  new(name, data.bytes)
end

.load_all(wad) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/doom/wad/flat.rb', line 50

def self.load_all(wad)
  entries = wad.lumps_between('F_START', 'F_END')
  entries.map do |entry|
    next if entry.size != SIZE

    data = wad.read_lump_at(entry)
    new(entry.name, data.bytes)
  end.compact
end

Instance Method Details

#[](x, y) ⇒ Object



25
26
27
# File 'lib/doom/wad/flat.rb', line 25

def [](x, y)
  @pixels[(y & 63) * WIDTH + (x & 63)]
end

#heightObject



16
17
18
# File 'lib/doom/wad/flat.rb', line 16

def height
  HEIGHT
end

#to_png(palette, filename) ⇒ Object



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

def to_png(palette, filename)
  require 'chunky_png'

  img = ChunkyPNG::Image.new(WIDTH, HEIGHT)
  @pixels.each_with_index do |color_index, i|
    x = i % WIDTH
    y = i / WIDTH
    r, g, b = palette[color_index]
    img[x, y] = ChunkyPNG::Color.rgb(r, g, b)
  end
  img.save(filename)
end

#widthObject



12
13
14
# File 'lib/doom/wad/flat.rb', line 12

def width
  WIDTH
end