3DWorld/Actor/SpriteOct.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran n/a Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 97 CC 1 SCAR 6.9
CC
1
Cov
0%
new
2.0
init
2.0
display
2.0
CRAP 2.0
1 package SDL::Tutorial::3DWorld::Actor::SpriteOct;
2  
3 # A SpriteOct is a set of eight sprites that represent
4 # a single character facing in a particular direction.
5 # Each of the sprites displays the character at a particular
6 # 45 degree offset.
7 # Combined together, the sprites give the illusion of being
8 # a full 3D model.
9  
10 use 5.008;
11 use strict;
12 use warnings;
13 use OpenGL::List ();
14 use SDL::Tutorial::3DWorld ();
15 use SDL::Tutorial::3DWorld::OpenGL ();
16 use SDL::Tutorial::3DWorld::Texture ();
17 use SDL::Tutorial::3DWorld::Material ();
18 use SDL::Tutorial::3DWorld::Actor ();
19  
20 our $VERSION = '0.33';
21 our @ISA = 'SDL::Tutorial::3DWorld::Actor';
22  
23  
24  
25  
26  
27 ######################################################################
28 # Constructor and Accessors
29  
30 sub new {
31 my $self = shift->SUPER::new(
32 blending => 1,
33 @_,
34 );
35  
36 # Even though the sprite has a notional direction in
37 # which it is facing, this does not impact on the OpenGL
38 # "orientation" implemented in the base actor class.
39 # As such, we use a totally different variable to hold the
40 # direction of the character.
41 $self->{angle} ||= 0;
42 $self->{velocity} ||= [ 0, 0, 0 ];
43  
44 # Convert the eight sprite texture files to full materials
45 $self->{material} = [
46 map {
47 SDL::Tutorial::3DWorld::Material->new(
48 ambient => [ 1, 1, 1, 0.5 ],
49 diffuse => [ 1, 1, 1, 0.5 ],
50 texture => SDL::Tutorial::3DWorld::Texture->new(
51 file => $_,
52 tile => 0,
53 mag_filter => OpenGL::GL_NEAREST,
54 ),
55 ),
56 } ( @{$self->{texture}} )
57 ];
58  
59 return $self;
60 }
61  
62  
63  
64  
65  
66 ######################################################################
67 # Engine Interface Methods
68  
69 sub init {
70 my $self = shift;
71  
72 # Load the sprites
73 foreach my $material ( @{$self->{material}} ) {
74 $material->init;
75 }
76  
77 # Compile the common drawing code
78 $self->{draw} = OpenGL::List::glpList {
79 $self->compile;
80 };
81  
82 return 1;
83 }
84  
85 sub display {
86 my $self = shift;
87 $self->SUPER::display(@_);
88  
89 # Rotate towards the camera.
90 # This merely serves to ensure the sprite is oriented towards
91 # the camera and has no relationship to the direction the
92 # actor will APPEAR to be facing relative to the camera.
93 OpenGL::glRotatef(
94 -SDL::Tutorial::3DWorld->current->camera->{angle},
95 0, 1, 0,
96 );
97  
98 # Select which sprite to display
99 my $i = (($self->{angle} + 22.5) / 45) % 8;
100  
101 # Switch to the sprite
102 $self->{material}->[$i]->display;
103  
104 # Draw the sprite quad.
105 OpenGL::glCallList( $self->{draw} );
106 }
107  
108 sub move {
109 my $self = shift;
110 my $step = shift;
111 $self->{angle} += 1 * $step;
112 return 1;
113 }
114  
115 sub compile {
116 my $self = shift;
117  
118 # Draw the sprite quad.
119 # The texture seems to wrap a little unless we use the 0.01 here.
120 OpenGL::glDisable( OpenGL::GL_LIGHTING );
121 OpenGL::glBegin( OpenGL::GL_QUADS );
122 OpenGL::glTexCoord2f( 0, 0 ); OpenGL::glVertex3f( -0.5, 1, 0 ); # Top Left
123 OpenGL::glTexCoord2f( 0, 1 ); OpenGL::glVertex3f( -0.5, 0, 0 ); # Bottom Left
124 OpenGL::glTexCoord2f( 1, 1 ); OpenGL::glVertex3f( 0.5, 0, 0 ); # Bottom Right
125 OpenGL::glTexCoord2f( 1, 0 ); OpenGL::glVertex3f( 0.5, 1, 0 ); # Top Right
126 OpenGL::glEnd();
127 OpenGL::glEnable( OpenGL::GL_LIGHTING );
128  
129 return 1;
130 }
131  
132 1;