/lib/pods/SDLx/Text.pod

http://github.com/PerlGameDev/SDL · Unknown · 282 lines · 169 code · 113 blank · 0 comment · 0 complexity · 164c7848dd8741b42a2aebac6dbe113c MD5 · raw file

  1. =pod
  2. =head1 NAME
  3. SDLx::Text - SDL extension for manipulating text
  4. =head1 CATEGORY
  5. Extension
  6. =head1 SYNOPSIS
  7. use SDL;
  8. use SDLx::App;
  9. use SDLx::Text;
  10. my $app = SDLx::App->new;
  11. my $message = SDLx::Text->new;
  12. $message->write_to( $app, "Hello, World!" );
  13. $app->update;
  14. =head1 DESCRIPTION
  15. The standard SDL API for handling fonts and rendering text is full of quirks
  16. and and low-level functions scattered all over the place. This is a sugar
  17. layer meant to let you easily handle text in your SDL apps.
  18. =head1 CONSTRUCTION
  19. =head2 new
  20. =head2 new ( %attributes )
  21. Instantiates a new SDLx::Text object. All attributes are optional:
  22. my $text = SDLx::Text->new(
  23. font => '/path/to/my/font.ttf',
  24. color => [255, 255, 255], # "white"
  25. size => 24,
  26. x => 0,
  27. y => 0,
  28. h_align => 'left',
  29. shadow => 1,
  30. bold => 1,
  31. text => 'All your base are belong to us.'
  32. );
  33. Please check the accessors below for more information on each attribute.
  34. All values shown above are the B<default values>, except for "text",
  35. which defaults to C<undef>; and "font", which if not provided will load
  36. the C<Gentium Basic> free font (see "L</"COPYRIGHT & LICENSE">" for more
  37. information).
  38. =head1 ACCESSORS
  39. All accessors below can be used to get and set their respective attributes.
  40. For example:
  41. my $font_size = $obj->size; # gets the font size
  42. $obj->size( 42 ); # sets a new font size
  43. Unless otherwise noticed, all accessors return their current value, after being set.
  44. =head2 x
  45. Gets/sets the left (x) positioning of the text to be rendered, relative
  46. to whatever surface you are placing it into. Note that if you set the
  47. C<h_align> property to anything other than 'C<left>', the text might
  48. not start exactly where you set C<x> to be, but relative to that value.
  49. Default value is 0, meaning leftmost corner.
  50. =head2 y
  51. Gets/sets the top (y) positioning of the text to be rendered, relative
  52. to whatever surface you are placing it into.
  53. Default value is 0, meaning top.
  54. =head2 h_align
  55. Gets/sets the horizontal alignment of the text to be rendered relative to
  56. whatever surface you are placing it into. Available alignments are 'C<left>',
  57. 'C<right>' and 'C<center>'. Default is 'C<left>'.
  58. =head2 color
  59. Gets/sets the text color. The color is an array reference in C<[R, G, B]> or C<[R, G, B, A]> format. See L<SDL::Color> for more information on colors.
  60. =head2 size
  61. Gets/sets the font size.
  62. =head2 font
  63. Pass it a string containing the path to your desired font to use it. Fonts
  64. can be in TTF, OTF or FON formats. Generates an exception if the font
  65. cannot be loaded.
  66. Returns the L<SDL::TTF::Font> object representing the font.
  67. =head2 shadow
  68. Set this to true to create a nice 3D shadow effect on the rendered text.
  69. This is achieved by creating another version of the text below the
  70. original one, at a given offset.
  71. =head2 shadow_color
  72. Set the RGB color array for the shadow. Defaults to black ( C<[0,0,0]> ).
  73. =head2 shadow_offset
  74. Sets the offset in which to display the shadow. Defaults to 1, meaning
  75. 1 pixel below and 1 pixel to the right of the original text.
  76. =head2 Setting the Font Style
  77. The following accessors can be used to set a rendering file for the B<loaded> font.
  78. They will only work for the current font, so if you change fonts, make sure to
  79. apply your modifiers again. A single font can have more than one modifier applied to it, eg:
  80. my $text = SDLx::Text->new;
  81. $text->bold(1);
  82. $text->italic(1);
  83. Set them to a true value to enable, false to disable.
  84. =head3 normal
  85. Sets the font style to normal.
  86. =head3 bold
  87. Sets the font style to bold.
  88. =head3 italic
  89. Sets the font style to italic.
  90. =head3 underline
  91. Sets the font style to underline.
  92. B<Note>: Due to libSDL design and depending on the chosen font, sometimes
  93. the underline may be outside of the generated text surface, and thus not
  94. visible when blitted to the screen. In these cases, you should probably turn
  95. off the option and draw your own underlines in the target surface.
  96. =head3 strikethrough
  97. Sets the font style to strikethrough.
  98. B<Note>: Due to libSDL design and depending on the chosen font, sometimes
  99. the strikethrough may be outside of the generated text surface, and thus not
  100. visible when blitted to the screen. In these cases, you should probably turn
  101. off the option and draw your own strikethroughs in the target surface.
  102. =head1 METHODS
  103. =head2 text( $some_text )
  104. Sets the text to be displayed by the SDLx::Text object. If you call it
  105. without any arguments, you'll get the current text string:
  106. my $string = $obj->text();
  107. Otherwise, C<text()> will return the SDLx::Text object itself, so you can
  108. easily chain this method around.
  109. my $obj = SDLx::Text->new->text( "Hello, Dave." );
  110. Note that this will happen even if it's an empty string, or undef.
  111. B<You pass an argument, you get an object>.
  112. Text will always be rendered as utf8, so you can pass any string containing
  113. regular ASCII or valid utf8 characters.
  114. =head2 write_to( $target_surface )
  115. =head2 write_to( $target_surface, "text to write" )
  116. Renders the text onto C<$target_surface> (usually the app itself). The
  117. text string may be passed as a parameter, otherwise it will use whatever
  118. is already set (via the C<new()> or C<text()> methods.
  119. =head2 write_xy( $target_surface, $x, $y )
  120. =head2 write_xy( $target_surface, $x, $y, $text )
  121. Same as C<write_to()>, but will render the text using the given top (y) and
  122. left (x) coordinates.
  123. =head1 READ-ONLY ATTRIBUTES
  124. As you set or update your text, font or size, SDLx::Text updates the surface
  125. that represents it. You don't usually need to worry about this at all, and
  126. we strongly recommend you use the L<"/METHODS"> above to render your text.
  127. If however you need to know how tall or wide the rendered text will be
  128. (in pixels), or if you want to retrieve the surface so you can manipulate and
  129. render it yourself, you can use the following getters:
  130. =head2 surface
  131. Returns the underlying surface representing the text itself. You probably don't need this, unless you're doing something really funky.
  132. =head2 w
  133. Shortcut to see the width of the underlying surface representing the text.
  134. =head2 h
  135. Shortcut to see the height of the underlying surface representing the text.
  136. =head2 font_filename
  137. Returns the file name for the loaded font. Use C<font()> to get the font object itself, or to set a new font.
  138. =head1 ERRORS AND DIAGNOSTICS
  139. =over 4
  140. =item * "SDL_ttf support has not been compiled"
  141. In order to render text in SDL you must have enabled SDL_ttf while building L<Alien::SDL>.
  142. =item * "Cannot init TTF: <some error>"
  143. In order to load fonts and render text, we need to initialize L<SDL::TTF>
  144. - that is, in case it hasn't been initialized already. The error above will
  145. appear in the rare event of any problem during initialization.
  146. =item * "Error opening font: <some error>"
  147. The font file you set either via C<< font( 'font.ttf' ) >> or during
  148. construction could not be loaded. The file may not exist in the given path,
  149. have wrong permissions, be corrupted, or of an unsupported format. Please
  150. refer the C<< <some error> >> message in the message itself for further
  151. information.
  152. =item * "TTF rendering error: <some error>"
  153. When you call C<text()>, it renders the provided string onto an internal
  154. L<SDL::Surface> object. If there was any problem rendering the text, this
  155. message will appear.
  156. =back
  157. =head1 BUGS
  158. Please report any bugs or feature requests to the bug tracker. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.
  159. =head1 SUPPORT
  160. You can find documentation for this module with the perldoc command.
  161. perldoc SDLx::Text
  162. =head1 AUTHORS
  163. See L<SDL/AUTHORS>.
  164. =head1 COPYRIGHT & LICENSE
  165. This program is free software; you can redistribute it and/or modify it
  166. under the same terms as Perl itself.
  167. This module ships with a default font, "I<Gentium Basic>", Copyright 2003-2008 SIL International (http://sil.org), released under the SIL Open Font License 1.1 (OFL). The font is available for use in free and commercial products, with some minor restrictions. Please read the C<OFL.txt> and C<OFL-FAQ.txt> for further information.
  168. =head1 SEE ALSO
  169. L<SDL>, L<SDLx::App>, L<SDL::TTF>