PageRenderTime 24ms CodeModel.GetById 8ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/SDLx/Controller/Interface.pm

http://github.com/PerlGameDev/SDL
Perl | 61 lines | 44 code | 17 blank | 0 comment | 9 complexity | 3eeb1a636b315e435b054f2470b90c6d MD5 | raw file
 1package SDLx::Controller::Interface;
 2use strict;
 3use warnings;
 4use Carp qw/confess/;
 5use Scalar::Util 'refaddr';
 6
 7our @ISA = qw(Exporter DynaLoader);
 8
 9use SDL::Internal::Loader;
10
11our $VERSION = 2.548;
12
13my %_controller;
14
15sub new {
16	shift;
17	my %foo = @_;
18
19	my @args;
20	push @args, ( $foo{x}     || 0 );
21	push @args, ( $foo{y}     || 0 );
22	push @args, ( $foo{v_x}   || 0 );
23	push @args, ( $foo{v_y}   || 0 );
24	push @args, ( $foo{rot}   || 0 );
25	push @args, ( $foo{ang_v} || 0 );
26
27	return SDLx::Controller::Interface->make(@args);
28}
29
30
31sub attach {
32	my ( $self, $controller, $render, @params ) = @_;
33
34	Carp::confess "An SDLx::Controller is needed" unless $controller && $controller->isa('SDLx::Controller');
35
36        $_controller{ refaddr $self } = [ $controller ];
37	my $move = sub { $self->update( $_[2], $_[1]->dt )};
38	$_controller{ refaddr $self }->[1] = $controller->add_move_handler($move);
39
40	if ($render) {
41		my $show = sub { my $state = $self->interpolate( $_[0] ); $render->( $state, @params ); };
42		$_controller{ refaddr $self }->[2] = $controller->add_show_handler($show);
43	} else {
44		Carp::confess "Render callback not provided";
45
46	}
47}
48
49sub detach {
50	my ( $self) = @_;
51        my $controller = $_controller{ refaddr $self }; 
52	return unless $controller;
53	$controller->[0]->remove_move_handler($controller->[1]);
54	$controller->[0]->remove_show_handler($controller->[2]);
55}
56
57internal_load_dlls(__PACKAGE__);
58bootstrap SDLx::Controller::Interface;
59
60
611;