Class: UT::FontRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/ut/font_renderer.rb

Overview

The FontRenderer uses the ‘Gosu::Font` class to draw the tiles on a `Gosu::Window`. `FontRenderer.new` takes an `options` hash as its only parameter. The `options` hash respects three members:

* `:window`: The `Gosu::Window`. _Defaults to `$window`._

* `:font_name`: Specifies the name of the font. Accepts the same values
as `Gosu::Font.new`. _Defaults to `nil`._

* `:tile_size`: The size of the tile on screen in _pixels_. Equivalent to the font
size. _Default to `0`_.

* `:scale_x`: Scales the font horizontally by this factor to reduce
spacing between the chars. _Default to 1_.

* `:scale_y`: Scales the font vertically by this factor to reduce spacing
between the chars. _Defaults to 1_.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FontRenderer

Returns a new instance of FontRenderer.



23
24
25
26
27
28
29
30
# File 'lib/ut/font_renderer.rb', line 23

def initialize options
  @window = options[:window] || $window
  @font_name = options[:font_name]
  @tile_size = options[:tile_size] || 0
  @scale_x = options[:scale_x] || 1
  @scale_y = options[:scale_y] || 1
  reload_font
end

Instance Attribute Details

#tile_sizeObject

Returns the value of attribute tile_size.



21
22
23
# File 'lib/ut/font_renderer.rb', line 21

def tile_size
  @tile_size
end

Instance Method Details

#fontObject

The ‘Gosu::Font` instance used for drawing the tiles



51
52
53
# File 'lib/ut/font_renderer.rb', line 51

def font
  @font
end

#font_nameObject



46
47
48
# File 'lib/ut/font_renderer.rb', line 46

def font_name
  @font_name
end

#font_name=(value) ⇒ Object



41
42
43
44
# File 'lib/ut/font_renderer.rb', line 41

def font_name= value
  @font_name = value
  reload_font
end

#render(tile, left, top) ⇒ Object

Renders the ‘tile` width the top-left corner at `[left,top]` on the `window`



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ut/font_renderer.rb', line 57

def render tile, left, top
  @window.draw_quad left          , top            , tile.background,
    left+tile_size, top            , tile.background,
    left          , top + tile_size, tile.background,
    left+tile_size, top + tile_size, tile.background

  @font.draw_rel tile.glyph,
                 left+tile_size/2, top+tile_size/2,
                 0, 0.5, 0.5,
                 @scale_x, @scale_y,
                 tile.foreground
end