Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'sdlx-text2' of https://github.com/PerlGameDev/SDL into …

…sdlx-text2
  • Loading branch information...
commit 4492d2989423fd6666b20704919d93efdba11706 2 parents e38fe93 + 9bdf10a
@rshadow rshadow authored
View
16 examples/SDLx/SDLx_text.pl
@@ -58,6 +58,19 @@
$y += 60;
}
+# Clip text
+$text = SDLx::Text->new(
+ font => 'share/GenBasR.ttf',
+ size => 36,
+ color => 0xFFFF00,
+ mode => 'utf8',
+ clip => SDL::Rect->new(5, 5, 172, 23),
+ h_align => 'center',
+ v_align => 'top',
+);
+$text->write_xy($app, 400, 120, 'CLIPPED TEXT');
+
+
# Use rect for position text
$text = SDLx::Text->new(
font => 'share/GenBasR.ttf',
@@ -109,6 +122,9 @@
$text->write_xy($app, 400, 70,
'Red text is example of different aligns on write_to for SDLx::App target'.
' surface without any limitations.');
+$text->color(0xFFFF00);
+$text->write_xy($app, 400, 100,
+ 'Clipped text example.');
# Run application
$app->flip;
View
14 lib/SDLx/Text.pm
@@ -167,6 +167,10 @@ sub write_to {
$self->text($text) if defined $text;
if ( my $surface = $self->{surface} ) {
+ # Set source rect
+ my $clip = $self->clip;
+ $clip = SDL::Rect->new(0, 0, $surface->w, $surface->h) unless $clip;
+
# Set target rect
my $rect = $self->rect;
$rect = SDL::Rect->new(0, 0, $target->w, $target->h) unless $rect;
@@ -194,7 +198,7 @@ sub write_to {
}
SDL::Video::blit_surface(
- $surface, SDL::Rect->new(0,0,$surface->w, $surface->h),
+ $surface, SDL::Rect->new($clip->x,$clip->y,$clip->w, $clip->h),
$target, SDL::Rect->new($self->{x}, $self->{y}, $surface->w, $surface->h)
);
}
@@ -210,7 +214,11 @@ sub write_xy {
$self->text($text) if defined $text;
if ( my $surface = $self->{surface} ) {
- # Set target rect
+ # Set source rect
+ my $clip = $self->clip;
+ $clip = SDL::Rect->new(0, 0, $surface->w, $surface->h) unless $clip;
+
+ # Set target rect
my $rect = $self->rect;
$rect = SDL::Rect->new(0, 0, $target->w, $target->h) unless $rect;
@@ -235,7 +243,7 @@ sub write_xy {
}
SDL::Video::blit_surface(
- $surface, SDL::Rect->new(0,0,$surface->w, $surface->h),
+ $surface, SDL::Rect->new($clip->x,$clip->y,$clip->w, $clip->h),
$target, SDL::Rect->new($self->{x}, $self->{y}, $surface->w, $surface->h)
);
}
View
29 lib/pods/SDLx/Text.pod
@@ -44,6 +44,7 @@ Instantiates a new SDLx::Text object. All attributes are optional:
x => 0,
y => 0,
h_align => 'left',
+ v_align => 'top',
text => 'All your base are belong to us.'
);
@@ -73,12 +74,28 @@ to whatever surface you are placing it into.
Gets/sets the top (y) positioning of the text to be rendered, relative
to whatever surface you are placing it into.
+=head2 rect
+
+Gets/sets the SDL::Rect for destination. Use it if you want write relative some
+rectangle on target surface.
+
+=head2 clip
+
+Gets/sets the SDL::Rect for choose only a small subset of the object's internal
+surface to be used on calls to C<write_to> or C<write_xy>.
+
=head2 h_align
Gets/sets the horizontal alignment of the text to be rendered relative to
whatever surface you are placing it into. Available alignments are 'C<left>',
'C<right>' and 'C<center>'. Default is 'C<left>'.
+=head2 v_align
+
+Gets/sets the vertical alignment of the text to be rendered relative to
+whatever surface you are placing it into. Available alignments are 'C<top>',
+'C<bottom>' and 'C<middle>'. Default is 'C<top>'.
+
=head2 color
Gets/sets the text color. The color is an array reference in C<[R, G, B]> or
@@ -125,12 +142,20 @@ Renders the text onto C<$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 C<new()> or C<text()> methods.
+This method use C<h_align()>, C<v_align()> and C<rect()> for set text position
+relative target surface. After write_to called C<x()> and C<y()> represent
+current text position.
+
=head2 write_xy( $target_surface, $x, $y )
=head2 write_xy( $target_surface, $x, $y, $text )
-Same as C<write_to()>, but will render the text using the given top (y) and
-left (x) coordinates.
+Same as C<write_to()>, but will render the text using the given $x and
+$y coordinates.
+
+This method use C<h_align()> and C<v_align()> to set text position relative $x
+and $y. And use C<rect()> for set text position relative target surface. After
+write_xy called C<x()> and C<y()> represent current text position.
=head1 READ-ONLY ATTRIBUTES
View
79 t/sdlx_text.t
@@ -2,12 +2,13 @@ use strict;
use SDL;
use SDL::Config;
use SDL::Color;
+use SDL::Rect;
use SDL::Surface;
use SDLx::App;
BEGIN {
use FindBin;
use Test::More;
- use lib 't/lib';
+ use lib qw(t/lib lib);
use SDL::TestTool;
if ( !SDL::Config->has('SDL_ttf') ) {
@@ -30,6 +31,7 @@ isa_ok( $score, 'SDLx::Text');
is($score->x, 0, 'default x position');
is($score->y, 0, 'default y position');
is($score->h_align, 'left', 'default horizontal alignment');
+is($score->v_align, 'top', 'default vertical alignment');
isa_ok( $score->font, 'SDL::TTF::Font' );
isa_ok($score->color, 'SDL::Color', 'default color');
is($score->size, 24, 'default size');
@@ -37,14 +39,85 @@ is($score->size, 24, 'default size');
$score->text('Hello');
is( $score->text, 'Hello', 'text() as a getter' );
-is( $score->w, 53, 'Hello! is 53 px wide!' );
-is( $score->h, 28, 'Hello! is 28 px high!' );
isa_ok($score->surface, 'SDL::Surface');
my $value = undef;
my $other_self = $score->text($value);
isa_ok($score, 'SDLx::Text');
+# Next tests relay on fixed width=53 and height=28 fot text 'Hello'
+# for GenBasR.ttf font. Draw on surface 200x200
+$score->text('Hello');
+is( $score->w, 53, 'Hello! is 53 px wide!' );
+is( $score->h, 28, 'Hello! is 28 px high!' );
+
+# Set rect for complete write_xx test
+is $score->rect, undef, 'default rect undefined';
+my ($rect_x, $rect_y, $rect_w, $rect_h) = (100, 100, 100, 100);
+$score->rect( SDL::Rect->new($rect_x,$rect_y,100,100) );
+isa_ok($score->rect, 'SDL::Rect');
+
+# Coordinates for write_xy
+my ($xy_x, $xy_y) = (50, 50);
+
+# Create target surface
+my $target = SDLx::Surface->new(width => 200, height => 200);
+
+# Test matrix write_xx for different aligns
+my @test = (
+ { h_align => 'left', v_align => 'top',
+ to_x => $rect_x + 0, to_y => $rect_y + 0,
+ xy_x => $rect_x + $xy_x, xy_y => $rect_y + $xy_y },
+ { h_align => 'left', v_align => 'middle',
+ to_x => $rect_x + 0, to_y => $rect_y + $rect_h/2-$score->h/2,
+ xy_x => $rect_x + $xy_x, xy_y => $rect_y + $xy_y-$score->h/2 },
+ { h_align => 'left', v_align => 'bottom',
+ to_x => $rect_x + 0, to_y => $rect_y + $rect_h-$score->h,
+ xy_x => $rect_x + $xy_x, xy_y => $rect_y + $xy_y-$score->h},
+ { h_align => 'center', v_align => 'top',
+ to_x => $rect_x + $rect_w/2-$score->w/2, to_y => $rect_y + 0,
+ xy_x => $rect_x + $xy_x-$score->w/2, xy_y => $rect_y + $xy_y},
+ { h_align => 'center', v_align => 'middle',
+ to_x => $rect_x + $rect_w/2-$score->w/2, to_y => $rect_y + $rect_h/2-$score->h/2,
+ xy_x => $rect_x + $xy_x-$score->w/2, xy_y => $rect_y + $xy_y-$score->h/2},
+ { h_align => 'center', v_align => 'bottom',
+ to_x => $rect_x + $rect_w/2-$score->w/2, to_y => $rect_y + $rect_h-$score->h,
+ xy_x => $rect_x + $xy_x-$score->w/2, xy_y => $rect_y + $xy_y-$score->h},
+ { h_align => 'right', v_align => 'top',
+ to_x => $rect_x + $rect_w-$score->w, to_y => $rect_y + 0,
+ xy_x => $rect_x + $xy_x-$score->w, xy_y => $rect_y + $xy_y},
+ { h_align => 'right', v_align => 'middle',
+ to_x => $rect_x + $rect_w-$score->w, to_y => $rect_y + $rect_h/2-$score->h/2,
+ xy_x => $rect_x + $xy_x-$score->w, xy_y => $rect_y + $xy_y-$score->h/2},
+ { h_align => 'right', v_align => 'bottom',
+ to_x => $rect_x + $rect_w-$score->w, to_y => $rect_y + $rect_h-$score->h,
+ xy_x => $rect_x + $xy_x-$score->w, xy_y => $rect_y + $xy_y-$score->h},
+
+);
+
+for my $test (@test)
+{
+ $score->h_align( $test->{h_align} );
+ $score->v_align( $test->{v_align} );
+ $score->write_to($target);
+
+ is $score->x, $test->{to_x},
+ sprintf 'horizontal position write_to for %s:%s', $score->h_align, $score->v_align;
+ is $score->y, $test->{to_y},
+ sprintf 'vertical position write_to for %s:%s', $score->h_align, $score->v_align;
+
+ $score->write_xy($target, $xy_x, $xy_y);
+ is $score->x, $test->{xy_x},
+ sprintf 'horizontal position write_xy for %s:%s', $score->h_align, $score->v_align;
+ is $score->y, $test->{xy_y},
+ sprintf 'vertical position write_xy for %s:%s', $score->h_align, $score->v_align;
+}
+
+# Clip test
+$score->clip( SDL::Rect->new(10, 10, 50, 50) );
+isa_ok($score->clip, 'SDL::Rect');
+
+
END {
if ($videodriver) {
Please sign in to comment.
Something went wrong with that request. Please try again.