Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added draw_gfx_text to SDLx:;Surface

  • Loading branch information...
commit 92b0bbc5199b3c32348e353a4c3a766bf530d7fe 1 parent 4f02899
@kthakore kthakore authored
Showing with 76 additions and 6 deletions.
  1. +40 −6 lib/SDLx/Surface.pm
  2. +20 −0 lib/pods/SDLx/Surface.pod
  3. +16 −0 t/sdlx_surface.t
View
46 lib/SDLx/Surface.pm
@@ -281,8 +281,6 @@ sub draw_line {
return;
}
- require SDL::GFX::Primitives;
-
my $result;
if ( Scalar::Util::looks_like_number($color) ) {
if ($antialias) {
@@ -328,7 +326,6 @@ sub draw_circle {
return $self;
}
-=pod
sub draw_trigon {
my ( $self, $center, $vextexes, $color) = @_;
@@ -360,11 +357,48 @@ sub draw_bezier {
}
-sub draw_text {
- my ($self, $vector, $height, $color, %options ) = @_;
+sub draw_gfx_text {
+ my ($self, $vector, $color, $text, $font ) = @_;
+
+ unless ( SDL::Config->has('SDL_gfx_primitives') ) {
+ Carp::carp("SDL_gfx_primitives support has not been compiled");
+ return;
+ }
+
+ if( $font )
+ {
+ if( ref($font) eq 'HASH' && exists $font->{data} && exists $font->{cw} && exists $font->{ch} )
+ {
+ SDL::GFX::Primitives::set_font( $font->{data}, $font->{cw}, $font->{ch} );
+ }
+ else
+ {
+ Carp::carp "Set font data as a hash of type \n \$font = {data => \$data, cw => \$cw, ch => \$ch}. \n Refer to perldoc SDL::GFX::Primitives set_font for initializing this variables.";
+ }
+ }
+ Carp::croak "vector needs to be an array ref of size 2. [x,y] " unless( ref($vector) eq 'ARRAY' && scalar(@$vector) == 2 );
+
+
+ my $result;
+ if ( Scalar::Util::looks_like_number($color) ) {
+ $result = SDL::GFX::Primitives::string_color(
+ $self->surface, $vector->[0], $vector->[1], $text,
+ $color
+ );
+ } elsif ( ref($color) eq 'ARRAY' && scalar(@$color) >= 4 ) {
+
+ $result = SDL::GFX::Primitives::string_RGBA(
+ $self->surface, $vector->[0], $vector->[1], $text,
+ @$color
+ );
+ }
+
+
+ Carp::croak "Error drawing line: " . SDL::get_error() if ( $result == -1 );
+
+ return $self;
}
-=cut
1;
View
20 lib/pods/SDLx/Surface.pod
@@ -165,6 +165,26 @@ Draws a line on the surface. Antialias is turned on if $antialias is true.
Returns $self
+=head2 draw_gfx_text
+
+Draw text using gfx (not pretty but fast) at give vector, color.
+
+ $surf->draw_gfx_text( [0,0], 0xffffffff, "fooo");
+ $surf->draw_gfx_text( [10,10], [20,20,20,20], "fooo");
+
+You can also set the gfx font but passing a hash reference as shown below.
+
+ my $f = '';
+ open( my $FH, '<', 'test/data/5x7.fnt');
+ binmode ($FH);
+ read($FH, $f, 4096);
+ close ($FH);
+
+ my $font = {data=>$f, cw => 5, ch => 7};
+ $surf->draw_gfx_text( [0,0], 0xffffffff, "fooo", $font );
+
+Returns $self
+
=head1 AUTHOR
kthakore
View
16 t/sdlx_surface.t
@@ -104,12 +104,28 @@ pass 'SDL::Rect array update';
$surfs[0]->draw_rect( [ 0, 0, 10, 20 ], 0xFF00FFFF );
pass 'draw_rect works';
+SKIP:
+{
+ skip ('SDL_gfx_primitives needed', 2) unless SDL::Config->has('SDL_gfx_primitives');
+$surfs[1]->draw_line( [ 0, 10 ], [ 20, 10 ], 0xff00ff );
$surfs[1]->draw_line( [ 0, 10 ], [ 20, 10 ], 0xff00ffff );
$surfs[1]->draw_line( [ 0, 10 ], [ 20, 10 ], 0xff00ffff, 1 );
$surfs[1]->draw_line( [ 0, 10 ], [ 20, 10 ], [ 255, 255, 0, 255 ] );
$surfs[1]->draw_line( [ 0, 10 ], [ 20, 10 ], [ 255, 255, 0, 255 ], 1 );
pass 'draw_line works';
+$surfs[1]->draw_gfx_text( [0,0], 0xffffffff, "fooo");
+$surfs[1]->draw_gfx_text( [10,10], [20,20,20,20], "fooo");
+my $f = '';
+open( my $FH, '<', 'test/data/5x7.fnt');
+binmode ($FH);
+read($FH, $f, 4096);
+close ($FH);
+my $font = {data=>$f, cw => 5, ch => 7};
+$surfs[1]->draw_gfx_text( [0,0], 0xffffffff, "fooo", $font );
+pass 'draw_gfx_text works';
+}
+
TODO:
{
Please sign in to comment.
Something went wrong with that request. Please try again.