Permalink
Browse files

Added preliminary for SDL_ttf. Needs more thought for design. Perhaps…

… a SDLx::TTF
  • Loading branch information...
kthakore
kthakore committed Sep 16, 2010
1 parent e296954 commit c0f86ef65c20a9cfe7e74c9a2bce22b7ae4ec8b5
Showing with 53 additions and 5 deletions.
  1. +48 −5 lib/SDLx/Surface.pm
  2. +5 −0 t/sdlx_surface.t
View
@@ -15,6 +15,8 @@ use SDL::Surface;
use SDL::PixelFormat;
use SDL::GFX::Primitives;
+use SDL::TTF;
+use SDL::TTF::Font;
use Tie::Simple;
use SDLx::Validate;
@@ -37,6 +39,9 @@ bootstrap SDLx::Surface;
# Inside out object
my %_tied_array;
+# Keep track of if TTF was inited or not
+my %_ttf;
+
sub new {
my ( $class, %options ) = @_;
my $self;
@@ -71,11 +76,6 @@ sub new {
return $self;
}
-sub DESTROY {
- my $self = shift;
- delete $_tied_array{$$self};
-}
-
sub display {
my $disp = SDL::Video::get_video_surface;
return SDLx::Surface->new( surface => $disp ) if $disp;
@@ -384,5 +384,48 @@ sub draw_gfx_text {
return $self;
}
+sub draw_ttf_text {
+ my ( $self, $position, $font, $size, $color, $text, $style ) = @_;
+
+ unless ( SDL::Config->has('SDL_ttf') ) {
+ Carp::cluck("SDL_ttf support has not been compiled");
+ }
+ unless ( SDL::TTF::was_init() )
+ {
+ Carp::cluck ("Cannot init TTF: " . SDL::get_error() ) unless SDL::TTF::init() == 0;
+ $_ttf{ $$self } = {};
+ $_ttf{ $$self }->{inited} = 1;
+ $_ttf{ $$self }->{style} = {
+ normal => TTF_STYLE_NORMAL,
+ bold => TTF_STYLE_BOLD,
+ italic => TTF_STYLE_ITALIC,
+ underline => TTF_STYLE_UNDERLINE,
+ strikethrough => TTF_STYLE_STRIKETHROUGH
+ };
+ }
+
+ my $ttf_font;
+ unless (my $ttf_font = SDL::TTF::open_font($font, $size ))
+ {
+
+ Carp::cluck ("Cannot make a TTF font from location ($font) or size($size), due to: ". SDL::get_error );
+
+ }
+
+ if ( $style && ( my $t_style = $_ttf{ $$self }->{style}->{$style} ) )
+ {
+ SDL::TTF::set_font_style($ttf_font, $t_style);
+ }
+
+}
+
+
+
+sub DESTROY {
+ my $self = shift;
+ delete $_tied_array{$$self};
+ SDL::TTF::quit if $_ttf{$$self}->{inited};
+ delete $_ttf{$$self};
+}
1;
View
@@ -180,6 +180,11 @@ is( $surf_dup->format->BitsPerPixel,
'Duplicate surf has same bpp'
);
+
+#TTF test
+
+$surf_dup->draw_ttf_text( [10,10], 'test/data/aircut3.ttf', 24, 0xFF00FFFF, 'Foo');
+
if ($videodriver) {
$ENV{SDL_VIDEODRIVER} = $videodriver;
} else {

0 comments on commit c0f86ef

Please sign in to comment.