3DWorld/Actor/GridPlane.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 199 CC 12 SCAR 31.7
CC
12
Cov
0%
compile
56.0
new
20.0
init
12.0
CRAP 23.8
1 package SDL::Tutorial::3DWorld::Actor::GridPlane;
2  
3 # This implements a static psuedo-3d horizontal plane of cubes (actually
4 # just the sides of the cube).
5 #
6 # When bounded by two flat planes at the top and bottom, the GridPlane
7 # object should allow you to implement a cube-based grid map as used in
8 # old games like Wolfenstein 3D.
9 #
10 # In the initial implementation this map is completely static, without
11 # any moving walls or none-perfect cube elements such as doors.
12 #
13 # When rendering, we rely on the power of modern graphics cards to brute
14 # force render the entire map instead of relying on optimisation.
15  
16 use 5.008;
17 use strict;
18 use warnings;
19 use SDL::Tutorial::3DWorld::OpenGL ();
20 use SDL::Tutorial::3DWorld::Actor ();
21 use SDL::Tutorial::3DWorld::Bound ();
22 use SDL::Tutorial::3DWorld::Texture ();
23  
24 our $VERSION = '0.33';
25 our @ISA = 'SDL::Tutorial::3DWorld::Actor';
26  
27  
28  
29  
30  
31 ######################################################################
32 # Constructor and Accessors
33  
34 sub new {
35 my $self = shift->SUPER::new(@_);
36  
37 # Do we have a texture set
38 unless ( $self->{size} ) {
39 die "Did not provide a map size";
40 }
41 unless ( $self->{wall} ) {
42 die "Did not provide a wall texture set";
43 }
44 unless ( $self->{map} ) {
45 die "Did not provide a the map";
46 }
47  
48 return $self;
49 }
50  
51  
52  
53  
54  
55 ######################################################################
56 # Engine Methods
57  
58 sub init {
59 my $self = shift;
60 my $wall = $self->{wall};
61 my $scale = $self->{scale} || [ 1, 1, 1 ];
62  
63 # Set up the bounding box
64 $self->{bound} = SDL::Tutorial::3DWorld::Bound->box(
65 0, 0, 0,
66 $scale->[0] * $self->{size},
67 $scale->[1] * 1,
68 $scale->[2] * $self->{size},
69 );
70  
71 # Initialise the textures
72 foreach my $i ( 0 .. $#$wall ) {
73 # Turn off mag filtering for that retro pixel look
74 $wall->[$i] = SDL::Tutorial::3DWorld::Texture->new(
75 file => $wall->[$i],
76 tile => 0,
77 mag_filter => OpenGL::GL_NEAREST,
78 );
79 $wall->[$i]->init;
80 }
81  
82 # Make sure the array position lines up with the map id.
83 # That is, the first wall texture is at position 1.
84 unshift @$wall, undef;
85  
86 # Compile the map into one big display list
87 $self->{list} = OpenGL::List::glpList {
88 OpenGL::glTranslatef( @{$self->{position}} );
89 OpenGL::glScalef( @{$self->{scale}} ) if $self->{scale};
90 OpenGL::glRotatef( @{$self->{orient}} ) if $self->{orient};
91 $self->compile;
92 };
93  
94 return 1;
95 }
96  
97 # Generate the drawing instructions
98 sub compile {
99 my $self = shift;
100 my $size = $self->{size};
101 my $n = $size - 1;
102 my $wall = $self->{wall};
103 my $debug = $self->{debug};
104  
105 # Generate a two-dimensional array from the map string
106 my @map = map {
107 [ split //, $_ ]
108 } split /\n/, $self->{map};
109  
110 # To be sufficiently old school, we don't do shadows
111 OpenGL::glDisable( OpenGL::GL_LIGHTING );
112  
113 # Draw the floor of the map
114 OpenGL::glDisable( OpenGL::GL_TEXTURE_2D );
115 OpenGL::glColor3f( @{$self->{floor}} );
116 OpenGL::glBegin( OpenGL::GL_QUADS );
117 OpenGL::glVertex3f( 0, 0.001, 0 );
118 OpenGL::glVertex3f( 0, 0.001, $size );
119 OpenGL::glVertex3f( $size, 0.001, $size );
120 OpenGL::glVertex3f( $size, 0.001, 0 );
121 OpenGL::glEnd();
122  
123 # Draw the roof of the map
124 OpenGL::glColor3f( @{$self->{ceiling}} );
125 OpenGL::glBegin( OpenGL::GL_QUADS );
126 OpenGL::glVertex3f( 0, 1, 0 );
127 OpenGL::glVertex3f( $size, 1, 0 );
128 OpenGL::glVertex3f( $size, 1, $size );
129 OpenGL::glVertex3f( 0, 1, $size );
130 OpenGL::glEnd();
131  
132 # Prepare to show the textured walls
133 OpenGL::glEnable( OpenGL::GL_TEXTURE_2D );
134 OpenGL::glColor3f( 1, 1, 1 );
135  
136 # Track the active wall texture
137 my $active = 0;
138  
139 # Iterate through the map
140 foreach my $x ( 0 .. $n ) {
141 my $X = $x + 1;
142 foreach my $z ( 0 .. $n ) {
143 my $Z = $z + 1;
144  
145 # Ignore open space
146 my $w = $map[$x]->[$z] or next;
147  
148 # Switch textures if needed
149 if ( $w != $active ) {
150 OpenGL::glEnd() if $active;
151 $wall->[$w]->display;
152 $active = $w;
153 OpenGL::glBegin( OpenGL::GL_QUADS );
154 }
155  
156 # Draw the north face unless hidden
157 if ( $debug or not $z or not $map[$x]->[$z-1] ) {
158 # Top Left
159 OpenGL::glTexCoord2f( 0, 0 );
160 OpenGL::glVertex3f( $X, 1, $z );
161  
162 # Bottom Left
163 OpenGL::glTexCoord2f( 0, 1 );
164 OpenGL::glVertex3f( $X, 0, $z );
165  
166 # Bottom Right
167 OpenGL::glTexCoord2f( 1, 1 );
168 OpenGL::glVertex3f( $x, 0, $z );
169  
170 # Top Right
171 OpenGL::glTexCoord2f( 1, 0 );
172 OpenGL::glVertex3f( $x, 1, $z );
173 }
174  
175 # Draw the east face unless hidden
176 if ( $debug or not $map[$x+1]->[$z] ) {
177 # Top Left
178 OpenGL::glTexCoord2f( 0, 0 );
179 OpenGL::glVertex3f( $X, 1, $Z );
180  
181 # Bottom Left
182 OpenGL::glTexCoord2f( 0, 1 );
183 OpenGL::glVertex3f( $X, 0, $Z );
184  
185 # Bottom Right
186 OpenGL::glTexCoord2f( 1, 1 );
187 OpenGL::glVertex3f( $X, 0, $z );
188  
189 # Top Right
190 OpenGL::glTexCoord2f( 1, 0 );
191 OpenGL::glVertex3f( $X, 1, $z );
192 }
193  
194 # Draw the south face unless hidden
195 if ( $debug or not $map[$x]->[$z+1] ) {
196 # Top Left
197 OpenGL::glTexCoord2f( 0, 0 );
198 OpenGL::glVertex3f( $x, 1, $Z );
199  
200 # Bottom Left
201 OpenGL::glTexCoord2f( 0, 1 );
202 OpenGL::glVertex3f( $x, 0, $Z );
203  
204 # Bottom Right
205 OpenGL::glTexCoord2f( 1, 1 );
206 OpenGL::glVertex3f( $X, 0, $Z );
207  
208 # Top Right
209 OpenGL::glTexCoord2f( 1, 0 );
210 OpenGL::glVertex3f( $X, 1, $Z );
211 }
212  
213 # Draw the west face unless hidden
214 if ( $debug or not $x or not $map[$x-1]->[$z] ) {
215 # Top Left
216 OpenGL::glTexCoord2f( 0, 0 );
217 OpenGL::glVertex3f( $x, 1, $z );
218  
219 # Bottom Left
220 OpenGL::glTexCoord2f( 0, 1 );
221 OpenGL::glVertex3f( $x, 0, $z );
222  
223 # Bottom Right
224 OpenGL::glTexCoord2f( 1, 1 );
225 OpenGL::glVertex3f( $x, 0, $Z );
226  
227 # Top Right
228 OpenGL::glTexCoord2f( 1, 0 );
229 OpenGL::glVertex3f( $x, 1, $Z );
230 }
231 }
232 }
233  
234 # Reenable lighting
235 OpenGL::glEnd();
236 OpenGL::glEnable( OpenGL::GL_LIGHTING );
237  
238 return 1;
239 }
240  
241 # This should never need to be called
242 sub display {
243 OpenGL::glCallList( $_[0]->{list} );
244 }
245  
246 1;