SDLx::Text - SDL extension for manipulating text
use SDL; use SDLx::App; use SDLx::Text; my $app = SDLx::App->new; my $message = SDLx::Text->new; $message->write_to( $app, "Hello, World!" ); $app->update;
The standard SDL API for handling fonts and rendering text is full of quirks and and low-level functions scattered all over the place. This is a sugar layer meant to let you easily handle text in your SDL apps.
Instantiates a new SDLx::Text object. All attributes are optional:
my $text = SDLx::Text->new( font => '/path/to/my/font.ttf', color => [255, 255, 255], # "white" size => 24, x => 0, y => 0, h_align => 'left', shadow => 1, bold => 1, text => 'All your base are belong to us.' );
Please check the accessors below for more information on each attribute. All values shown above are the default values, except for "text", which defaults to
undef; and "font", which if not provided will load the
Gentium Basic free font (see ""COPYRIGHT & LICENSE"" for more information).
All accessors below can be used to get and set their respective attributes. For example:
my $font_size = $obj->size; # gets the font size $obj->size( 42 ); # sets a new font size
Unless otherwise noticed, all accessors return their current value, after being set.
Gets/sets the left (x) positioning of the text to be rendered, relative to whatever surface you are placing it into. Note that if you set the
h_align property to anything other than '
left', the text might not start exactly where you set
x to be, but relative to that value.
Default value is 0, meaning leftmost corner.
Gets/sets the top (y) positioning of the text to be rendered, relative to whatever surface you are placing it into.
Default value is 0, meaning top.
Gets/sets the horizontal alignment of the text to be rendered relative to whatever surface you are placing it into. Available alignments are '
right' and '
center'. Default is '
Gets/sets the text color. The color is an array reference in
[R, G, B] or
[R, G, B, A] format. See SDL::Color for more information on colors.
Gets/sets the font size.
Pass it a string containing the path to your desired font to use it. Fonts can be in TTF, OTF or FON formats. Generates an exception if the font cannot be loaded.
Returns the SDL::TTF::Font object representing the font.
Set this to true to create a nice 3D shadow effect on the rendered text. This is achieved by creating another version of the text below the original one, at a given offset.
Set the RGB color array for the shadow. Defaults to black (
Sets the offset in which to display the shadow. Defaults to 1, meaning 1 pixel below and 1 pixel to the right of the original text.
The following accessors can be used to set a rendering file for the loaded font. They will only work for the current font, so if you change fonts, make sure to apply your modifiers again. A single font can have more than one modifier applied to it, eg:
my $text = SDLx::Text->new; $text->bold(1); $text->italic(1);
Set them to a true value to enable, false to disable.
Sets the font style to normal.
Sets the font style to bold.
Sets the font style to italic.
Sets the font style to underline.
Note: Due to libSDL design and depending on the chosen font, sometimes the underline may be outside of the generated text surface, and thus not visible when blitted to the screen. In these cases, you should probably turn off the option and draw your own underlines in the target surface.
Sets the font style to strikethrough.
Note: Due to libSDL design and depending on the chosen font, sometimes the strikethrough may be outside of the generated text surface, and thus not visible when blitted to the screen. In these cases, you should probably turn off the option and draw your own strikethroughs in the target surface.
Sets the text to be displayed by the SDLx::Text object. If you call it without any arguments, you'll get the current text string:
my $string = $obj->text();
text() will return the SDLx::Text object itself, so you can easily chain this method around.
my $obj = SDLx::Text->new->text( "Hello, Dave." );
Note that this will happen even if it's an empty string, or undef. You pass an argument, you get an object.
Text will always be rendered as utf8, so you can pass any string containing regular ASCII or valid utf8 characters.
Renders the text onto
$target_surface (usually the app itself). The text string may be passed as a parameter, otherwise it will use whatever is already set (via the
write_to(), but will render the text using the given top (y) and left (x) coordinates.
As you set or update your text, font or size, SDLx::Text updates the surface that represents it. You don't usually need to worry about this at all, and we strongly recommend you use the "METHODS"" in " above to render your text.
If however you need to know how tall or wide the rendered text will be (in pixels), or if you want to retrieve the surface so you can manipulate and render it yourself, you can use the following getters:
Returns the underlying surface representing the text itself. You probably don't need this, unless you're doing something really funky.
Shortcut to see the width of the underlying surface representing the text.
Shortcut to see the height of the underlying surface representing the text.
Returns the file name for the loaded font. Use
font() to get the font object itself, or to set a new font.
In order to render text in SDL you must have enabled SDL_ttf while building Alien::SDL.
In order to load fonts and render text, we need to initialize SDL::TTF - that is, in case it hasn't been initialized already. The error above will appear in the rare event of any problem during initialization.
The font file you set either via
font( 'font.ttf' ) or during construction could not be loaded. The file may not exist in the given path, have wrong permissions, be corrupted, or of an unsupported format. Please refer the
<some error> message in the message itself for further information.
When you call
text(), it renders the provided string onto an internal SDL::Surface object. If there was any problem rendering the text, this message will appear.
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.
You can find documentation for this module with the perldoc command.
See "AUTHORS" in SDL.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This module ships with a default font, "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
OFL-FAQ.txt for further information.