Class: Hemi::Render::Font

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

Constant Summary collapse

ALLOWED_MODES =
%i[solid blended shaded].freeze
FONT_FILE_PATTERN =
/([\w-]*)_([\d]*)/.freeze

Constants inherited from Texture

Texture::ERR__INVALID_POSITION

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Texture

#texture

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Texture

[], purge!, register

Constructor Details

#initialize(name_size) ⇒ Font

Returns a new instance of Font.



14
15
16
17
18
19
20
21
22
# File 'lib/hemi/render/font.rb', line 14

def initialize(name_size)
  name, size = *name_size.to_s.scan(FONT_FILE_PATTERN).flatten
  @font_path = "assets/fonts/#{name}.ttf"
  @font      = SDL2::TTF.open(font_path, size.to_i)

  Font.register(name_size, self)
rescue SDL2::Error => _e
  raise ArgumentError, format(ERR__FONT_NOT_FOUND, font_path)
end

Class Attribute Details

.fontsObject (readonly)

Returns the value of attribute fonts.



34
35
36
# File 'lib/hemi/render/font.rb', line 34

def fonts
  @fonts
end

Instance Attribute Details

#fontObject (readonly)

Returns the value of attribute font.



24
25
26
# File 'lib/hemi/render/font.rb', line 24

def font
  @font
end

#font_pathObject (readonly)

Returns the value of attribute font_path.



24
25
26
# File 'lib/hemi/render/font.rb', line 24

def font_path
  @font_path
end

#sizeObject (readonly)

Returns the value of attribute size.



24
25
26
# File 'lib/hemi/render/font.rb', line 24

def size
  @size
end

Class Method Details

.bucketObject



36
37
38
# File 'lib/hemi/render/font.rb', line 36

def bucket
  :fonts
end

Instance Method Details

#render(text, position: nil, mode: :blended) ⇒ Object



26
27
28
29
30
31
# File 'lib/hemi/render/font.rb', line 26

def render(text, position: nil, mode: :blended)
  @texture = Hemi::Render::Window
             .renderer
             .create_texture_from(surface(text, mode: mode))
  Hemi::Render::Window.renderer.copy(texture, nil, rectangle(position: position))
end