File: Interface.pm

package info (click to toggle)
libsdl-perl 2.548-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,972 kB
  • sloc: perl: 13,985; ansic: 583; makefile: 35
file content (61 lines) | stat: -rw-r--r-- 1,455 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package SDLx::Controller::Interface;
use strict;
use warnings;
use Carp qw/confess/;
use Scalar::Util 'refaddr';

our @ISA = qw(Exporter DynaLoader);

use SDL::Internal::Loader;

our $VERSION = 2.548;

my %_controller;

sub new {
	shift;
	my %foo = @_;

	my @args;
	push @args, ( $foo{x}     || 0 );
	push @args, ( $foo{y}     || 0 );
	push @args, ( $foo{v_x}   || 0 );
	push @args, ( $foo{v_y}   || 0 );
	push @args, ( $foo{rot}   || 0 );
	push @args, ( $foo{ang_v} || 0 );

	return SDLx::Controller::Interface->make(@args);
}


sub attach {
	my ( $self, $controller, $render, @params ) = @_;

	Carp::confess "An SDLx::Controller is needed" unless $controller && $controller->isa('SDLx::Controller');

        $_controller{ refaddr $self } = [ $controller ];
	my $move = sub { $self->update( $_[2], $_[1]->dt )};
	$_controller{ refaddr $self }->[1] = $controller->add_move_handler($move);

	if ($render) {
		my $show = sub { my $state = $self->interpolate( $_[0] ); $render->( $state, @params ); };
		$_controller{ refaddr $self }->[2] = $controller->add_show_handler($show);
	} else {
		Carp::confess "Render callback not provided";

	}
}

sub detach {
	my ( $self) = @_;
        my $controller = $_controller{ refaddr $self }; 
	return unless $controller;
	$controller->[0]->remove_move_handler($controller->[1]);
	$controller->[0]->remove_show_handler($controller->[2]);
}

internal_load_dlls(__PACKAGE__);
bootstrap SDLx::Controller::Interface;


1;