PageRenderTime 29ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/t_backcompat/testfonttool.pl

http://github.com/PerlGameDev/SDL
Perl | 104 lines | 83 code | 20 blank | 1 comment | 20 complexity | 3b8775ebc2580711c3b00d232d9d518f MD5 | raw file
  1#!/usr/bin/env perl
  2
  3use strict;
  4use SDL;
  5use SDLx::App;
  6use SDL::Event;
  7use SDL::Tool::Font;
  8use SDL::Color;
  9
 10my ( %options, $app, $mode );
 11
 12die "usage: $0 [-hw] [-fullscreen] [-width 640] [-height 480] [-bpp 24]\n"
 13	if ( SDL::in( $ARGV[0], qw/ -h -? --help/ ) );
 14
 15chdir 'test' if -d 'test';
 16die "$0 must be run in the SDL_perl/test/ directory!"
 17	unless ( -d 'data' );
 18
 19for ( 0 .. @ARGV - 1 ) {
 20	$options{ $ARGV[$_] } = $ARGV[ $_ + 1 ] || 1;
 21}
 22
 23$options{-flags} = SDL_SWSURFACE;
 24$options{-flags} |= SDL_HWPALETTE  if ( $options{-hw} );
 25$options{-flags} |= SDL_FULLSCREEN if ( $options{-fullscreen} );
 26
 27$options{-title} = $0;
 28
 29$options{-width}  ||= 800;
 30$options{-height} ||= 600;
 31$options{-depth}  ||= $options{-bpp} || 24;
 32
 33$app = SDLx::App->new(%options);
 34
 35my %ttfonts = (
 36	'aircut3.ttf'    => 0,
 37	'electrohar.ttf' => 0,
 38);
 39
 40my %sfonts = (
 41	'24P_Arial_NeonYellow.png' => 0,
 42	'24P_Copperplate_Blue.png' => 0,
 43);
 44
 45my @fonts;
 46
 47for ( reverse keys %ttfonts ) {
 48	for $mode (qw/ -normal -bold -italic -underline /) {
 49		if ( -e "data/$_" ) {
 50			print STDERR "Loading $_\n";
 51			$ttfonts{"$_$mode"} = SDL::Tool::Font->new(
 52				$mode   => 1,
 53				-ttfont => "data/$_",
 54				-size   => 20,
 55				-fg     => $SDL::Color::black,
 56				-bg     => $SDL::Color::black
 57			);
 58			push @fonts, $ttfonts{"$_$mode"};
 59		}
 60	}
 61}
 62
 63%ttfonts = reverse %ttfonts;
 64
 65for ( reverse keys %sfonts ) {
 66	if ( -e "data/$_" ) {
 67		print STDERR "Loading $_\n";
 68		$sfonts{$_} = SDL::Tool::Font->new( -sfont => "data/$_" );
 69		push @fonts, $sfonts{$_};
 70	}
 71}
 72
 73%sfonts = reverse %sfonts;
 74
 75sub DrawFonts {
 76	$app->fill( 0, $SDL::Color::white );
 77	my ( $x, $y ) = @_;
 78	for my $font (@fonts) {
 79		$font->print(
 80			$app, $x, $y,
 81			"SDLperl font test. ",
 82			"This is " . ( $ttfonts{$font} || $sfonts{$font} )
 83		);
 84		$y += 40;
 85	}
 86	$app->flip();
 87}
 88
 89DrawFonts( 10, 10 );
 90
 91$app->loop(
 92	{   SDL_KEYDOWN() => sub {
 93			my ($event) = @_;
 94			$app->warp( $options{-width} / 2, $options{-height} / 2 )
 95				if ( $event->key_sym() == SDLK_SPACE );
 96			$app->fullscreen() if ( $event->key_sym() == SDLK_f );
 97			exit(0) if ( $event->key_sym() == SDLK_ESCAPE );
 98		},
 99		SDL_QUIT() => sub {
100			exit(0);
101			}
102	}
103);
104