File: Animation.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 (122 lines) | stat: -rw-r--r-- 3,164 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env perl
#
# Animation.pm
#
# Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
#
# ------------------------------------------------------------------------------
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# ------------------------------------------------------------------------------
#
# Please feel free to send questions, suggestions or improvements to:
#
#	David J. Goehrig
#	dgoehrig@cpan.org
#

package SDL::Tutorial::Animation;

use strict;
use warnings;

use SDL;
use SDLx::App;
use SDL::Rect;
use SDL::Color;
use SDL::Video;

our $VERSION = 2.548;

# change these values as necessary
my $title = 'My SDL Animation';
my ( $width,      $height,      $depth )  = ( 640,  480,  16 );
my ( $bg_r,       $bg_g,        $bg_b )   = ( 0x00, 0x00, 0x00 );
my ( $rect_r,     $rect_g,      $rect_b ) = ( 0x00, 0x00, 0xff );
my ( $rect_width, $rect_height, $rect_y ) = ( 100,  100,  190 );

my $app = SDLx::App->new(
	width  => $width,
	height => $height,
	depth  => $depth,
);

my $color = SDL::Video::map_RGB( $app->format, $rect_r, $rect_g, $rect_b, );

my $bg_color = SDL::Video::map_RGB( $app->format, $bg_r, $bg_g, $bg_b, );

my $background = SDL::Rect->new( 0, 0, $width, $height, );

my $rect = create_rect();

# your code here, perhaps
for my $x ( 0 .. 640 ) {
	$rect->x($x);
	draw_frame(
		$app,
		bg         => $background,
		bg_color   => $bg_color,
		rect       => $rect,
		rect_color => $color,
	);
}

# remove this line
sleep 2;

# XXX - if you know why I need to create a new rect here, please tell me!
$rect = create_rect();
my $old_rect = create_rect();

# your code also here, perhaps
for my $x ( 0 .. 640 ) {
	$rect->x($x);
	draw_undraw_rect(
		$app,
		rect       => $rect,
		old_rect   => $old_rect,
		rect_color => $color,
		bg_color   => $bg_color,
	);
	$old_rect->x($x);
}

# your code almost certainly follows; remove this line
sleep 2;

sub create_rect {
	return SDL::Rect->new( 0, $rect_y, $rect_width, $rect_height, );
}

sub draw_frame {
	my ( $app, %args ) = @_;

	SDL::Video::fill_rect( $app, $args{bg},   $args{bg_color} );
	SDL::Video::fill_rect( $app, $args{rect}, $args{rect_color} );
	SDL::Video::update_rects( $app, $args{bg} );
}

sub draw_undraw_rect {
	my ( $app, %args ) = @_;

	SDL::Video::fill_rect( $app, $args{old_rect}, $args{bg_color} );
	SDL::Video::fill_rect( $app, $args{rect},     $args{rect_color} );
	SDL::Video::update_rects( $app, $args{old_rect} );
	SDL::Video::update_rects( $app, $args{rect} );
}

1;