PageRenderTime 37ms CodeModel.GetById 16ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/t/lib/SDL/TestTool.pm

http://github.com/PerlGameDev/SDL
Perl | 64 lines | 61 code | 3 blank | 0 comment | 9 complexity | 98690f71d515a9a59abfb1195a9b2e78 MD5 | raw file
 1package SDL::TestTool;
 2use strict;
 3use warnings;
 4use Capture::Tiny qw(capture);
 5use SDL;
 6use SDL::AudioSpec;
 7use SDL::Audio;
 8my %inits = (
 9	0x00000001 => 'SDL_INIT_TIMER',
10	0x00000010 => 'SDL_INIT_AUDIO',
11	0x00000020 => 'SDL_INIT_VIDEO',
12	0x00000100 => 'SDL_INIT_CDROM',
13	0x00000200 => 'SDL_INIT_JOYSTICK',
14	0x00100000 => 'SDL_INIT_NOPARACHUTE',
15	0x01000000 => 'SDL_INIT_EVENTTHREAD',
16	0x0000FFFF => 'SDL_INIT_EVERYTHING'
17);
18
19sub init {
20	my ( $self, $init ) = @_;
21	my $stdout = '';
22	my $stderr = '';
23	my $result = 0;
24
25	if ( $init == SDL_INIT_VIDEO ) {
26		if ( $^O !~ /win/i && !$ENV{DISPLAY} && !$ENV{SDL_VIDEODRIVER} ) {
27			warn '$DISPLAY is not set! Cannot Init Video';
28			return;
29		}
30	}
31
32	if ( $init == SDL_INIT_AUDIO ) {
33		if ( test_audio_open() != 0 ) {
34			warn "Couldn't use a valid audio device: " . SDL::get_error();
35			return;
36		}
37		SDL::quit();
38	}
39
40	($stdout, $stderr, $result ) = capture { SDL::init($init) };
41	if ( $result != 0 ) {
42		warn 'Init ' . $inits{$init} . ' failed with SDL error: ' . SDL::get_error() . "\nand stderr $stderr\n";
43	}
44
45	return $result == 0;
46}
47
48sub test_audio_open {
49	my $desired = SDL::AudioSpec->new;
50	$desired->freq(44100);
51	$desired->format(SDL::Audio::AUDIO_S16SYS);
52	$desired->channels(2);
53	$desired->samples(4096);
54	$desired->callback('main::audio_callback');
55
56	my $obtained = SDL::AudioSpec->new;
57	return SDL::Audio::open( $desired, $obtained );
58}
59
60sub audio_callback {
61
62}
63
641;