Class: Rubydraw::Text
Overview
Text objects are instantiazed with the font (can be a TTF file) and with the text to display. They can be drawn at a position on the screen.
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#font ⇒ Object
readonly
Returns the value of attribute font.
-
#font_size ⇒ Object
readonly
Returns the value of attribute font_size.
Instance Method Summary collapse
-
#height ⇒ Object
See Rubydraw::Text#width.
-
#initialize(contents, color, font_name = "Times New Roman", font_size = 25) ⇒ Text
constructor
Create a new drawable Text object with the given font and contents.
-
#width ⇒ Object
Redefined because if @text is an empty string, it would return nil.
Methods inherited from Surface
#area, #blit, #fill, #flip, #get_pixel, load_img, load_text, #pixels, #rotozoom, #rotozoom!, #set_pixel, #size, #to_sdl
Constructor Details
#initialize(contents, color, font_name = "Times New Roman", font_size = 25) ⇒ Text
Create a new drawable Text object with the given font and contents.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rubydraw/text.rb', line 9 def initialize(contents, color, font_name="Times New Roman", font_size = 25) @contents, @font, @font_size, @color = contents, font_name, font_size, color if File.exist?(font_name) font_path = font_name else # The font doesn't exist in the program's directory. # Check in Rubydraw's font directory (rubydraw-x.x.x/lib/rubydraw/fonts) font_path = "#{File.dirname(__FILE__)}/../fonts/#{font_name}.ttf" end unless File.exists?(font_path) raise "Font file '#{font_name}' does not exist; attemped to load from '#{font_path}'" end sdl_text = SDL::TTF.OpenFont(font_path, font_size) raise(SDLError, "Failed to initialize font: #{SDL.GetError}") if sdl_text.pointer.null? sdl_color = @color.to_sdl @sdl_surface = SDL::TTF.RenderText_Blended(sdl_text, @contents, sdl_color) end |
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
6 7 8 |
# File 'lib/rubydraw/text.rb', line 6 def color @color end |
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
6 7 8 |
# File 'lib/rubydraw/text.rb', line 6 def contents @contents end |
#font ⇒ Object (readonly)
Returns the value of attribute font.
6 7 8 |
# File 'lib/rubydraw/text.rb', line 6 def font @font end |
#font_size ⇒ Object (readonly)
Returns the value of attribute font_size.
6 7 8 |
# File 'lib/rubydraw/text.rb', line 6 def font_size @font_size end |