Class: Doom::Render::Font

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

Overview

DOOM’s built-in font loaded from STCFN patches (ASCII 33-121). Uppercase only – lowercase is auto-uppercased.

Constant Summary collapse

SPACE_WIDTH =
4

Instance Method Summary collapse

Constructor Details

#initialize(wad, hud_graphics) ⇒ Font

Returns a new instance of Font.



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

def initialize(wad, hud_graphics)
  @chars = {}
  (33..121).each do |ascii|
    name = "STCFN%03d" % ascii
    sprite = hud_graphics.send(:load_graphic, name)
    @chars[ascii] = sprite if sprite
  end
end

Instance Method Details

#draw_centered(framebuffer, text, y, screen_width: 320, screen_height: 240) ⇒ Object

Draw text centered horizontally



52
53
54
55
56
# File 'lib/doom/render/font.rb', line 52

def draw_centered(framebuffer, text, y, screen_width: 320, screen_height: 240)
  w = text_width(text)
  x = (screen_width - w) / 2
  draw_text(framebuffer, text, x, y, screen_width: screen_width, screen_height: screen_height)
end

#draw_text(framebuffer, text, x, y, screen_width: 320, screen_height: 240) ⇒ Object

Draw text into framebuffer at (x, y). Returns width drawn.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/doom/render/font.rb', line 20

def draw_text(framebuffer, text, x, y, screen_width: 320, screen_height: 240)
  cursor_x = x
  text.upcase.each_char do |char|
    if char == ' '
      cursor_x += SPACE_WIDTH
      next
    end

    sprite = @chars[char.ord]
    next unless sprite

    draw_char(framebuffer, sprite, cursor_x, y, screen_width, screen_height)
    cursor_x += sprite.width
  end
  cursor_x - x
end

#text_width(text) ⇒ Object

Measure text width without drawing



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/doom/render/font.rb', line 38

def text_width(text)
  width = 0
  text.upcase.each_char do |char|
    if char == ' '
      width += SPACE_WIDTH
      next
    end
    sprite = @chars[char.ord]
    width += sprite.width if sprite
  end
  width
end