PageRenderTime 88ms CodeModel.GetById 12ms app.highlight 72ms RepoModel.GetById 1ms app.codeStats 0ms

/t/sdlx_sprite_animated.t

http://github.com/PerlGameDev/SDL
Unknown | 452 lines | 361 code | 91 blank | 0 comment | 0 complexity | 864c40dc3ec7ce0fc511b3f1299d3b51 MD5 | raw file
  1use strict;
  2use warnings;
  3use Test::More;
  4use SDL;
  5use SDL::Config;
  6use SDL::Video;
  7use SDL::Color;
  8use SDLx::Sprite::Animated;
  9use lib 't/lib';
 10use SDL::TestTool;
 11
 12
 13my $videodriver = $ENV{SDL_VIDEODRIVER};
 14$ENV{SDL_VIDEODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};
 15
 16if ( !SDL::TestTool->init(SDL_INIT_VIDEO) ) {
 17	plan( skip_all => 'Failed to init video' );
 18} elsif ( !SDL::Config->has('SDL_image') ) {
 19	plan( skip_all => 'SDL_image support not compiled' );
 20}
 21
 22
 23can_ok(
 24	'SDLx::Sprite::Animated',
 25	qw( new rect clip load surface x y w h draw alpha_key
 26		step_x step_y type max_loops ticks_per_frame current_frame current_loop
 27		set_sequences sequence next previous reset start stop draw)
 28);
 29
 30TODO: {
 31	local $TODO = 'methods not implemented yet';
 32	can_ok( 'SDLx::Sprite', qw( add remove zoom ) );
 33}
 34
 35my $disp = SDL::Video::set_video_mode( 300, 300, 32, SDL_ANYFORMAT );
 36
 37my $sprite = SDLx::Sprite::Animated->new(
 38	width  => 48,
 39	height => 48
 40);
 41
 42isa_ok( $sprite, 'SDLx::Sprite' );
 43isa_ok( $sprite, 'SDLx::Sprite::Animated' );
 44
 45my $clip = $sprite->clip;
 46ok( $clip, 'clip defined upon raw initialization' );
 47isa_ok( $clip, 'SDL::Rect', 'spawned clip isa SDL::Rect' );
 48is( $clip->x, 0,  'clip->x init' );
 49is( $clip->y, 0,  'clip->y init' );
 50is( $clip->w, 48, 'clip->w init' );
 51is( $clip->h, 48, 'clip->h init' );
 52
 53my $rect = $sprite->rect;
 54ok( $rect, 'rect defined upon raw initialization' );
 55isa_ok( $rect, 'SDL::Rect', 'spawned rect isa SDL::Rect' );
 56is( $rect->x, 0,  'rect->x init' );
 57is( $rect->y, 0,  'rect->y init' );
 58is( $rect->w, 48, 'rect->w init' );
 59is( $rect->h, 48, 'rect->h init' );
 60
 61my ( $x, $y ) = ( $sprite->x, $sprite->y );
 62is( $x, 0, 'no x defined upon raw initialization' );
 63is( $y, 0, 'no y defined upon raw initialization' );
 64
 65my ( $w, $h ) = ( $sprite->w, $sprite->h );
 66is( $w, 48, 'w defined upon raw initialization' );
 67is( $h, 48, 'h defined upon raw initialization' );
 68
 69isa_ok(
 70	$sprite->load('test/data/hero.bmp'),
 71	'SDLx::Sprite::Animated', '[load] works'
 72);
 73
 74isa_ok(
 75	$sprite->alpha_key( SDL::Color->new( 0xfc, 0x00, 0xff ) ),
 76	'SDLx::Sprite::Animated', '[alpha_key] works'
 77);
 78
 79isa_ok(
 80	$sprite->alpha(0xcc), 'SDLx::Sprite::Animated',
 81	'[alpha] integer works '
 82);
 83
 84isa_ok(
 85	$sprite->alpha(0.3), 'SDLx::Sprite::Animated',
 86	'[alpha] percentage works'
 87);
 88
 89is( $clip->x, 0,  'clip->x after load' );
 90is( $clip->y, 0,  'clip->y after load' );
 91is( $clip->w, 48, 'clip->w after load' );
 92is( $clip->h, 48, 'clip->h after load' );
 93
 94is( $rect->x, 0,  'rect->x after load' );
 95is( $rect->y, 0,  'rect->y after load' );
 96is( $rect->w, 48, 'rect->w after load' );
 97is( $rect->h, 48, 'rect->h after load' );
 98
 99$sprite->set_sequences( left => [ [ 1, 0 ], [ 1, 1 ], [ 1, 2 ] ], );
100
101my ( $clip_w, $clip_h ) = ( $sprite->clip->w, $sprite->clip->h );
102$sprite->alpha_key( SDL::Color->new( 0xfc, 0x00, 0xff ) );
103is( $sprite->clip->w, $clip_w, 'alpha_key() does not change clip width' );
104is( $sprite->clip->h, $clip_h, 'alpha_key() does not change clip height' );
105
106$sprite->sequence('left');
107
108is( $sprite->current_frame, 1, 'sprite->current_frame after sequence' );
109is( $sprite->current_loop,  1, 'sprite->current_loop after sequence' );
110
111is( $clip->x, 48, 'clip->x after sequence' );
112is( $clip->y, 0,  'clip->y after sequence' );
113is( $clip->w, 48, 'clip->w after sequence' );
114is( $clip->h, 48, 'clip->h after sequence' );
115
116is( $rect->x, 0,  'rect->x after sequence' );
117is( $rect->y, 0,  'rect->y after sequence' );
118is( $rect->w, 48, 'rect->w after sequence' );
119is( $rect->h, 48, 'rect->h after sequence' );
120
121$sprite->next;
122is( $sprite->current_frame, 2, 'sprite->current_frame after next' );
123is( $sprite->current_loop,  1, 'sprite->current_loop after next' );
124
125is( $clip->x, 48, 'clip->x after next' );
126is( $clip->y, 48, 'clip->y after next' );
127is( $clip->w, 48, 'clip->w after next' );
128is( $clip->h, 48, 'clip->h after next' );
129
130is( $rect->x, 0,  'rect->x after next' );
131is( $rect->y, 0,  'rect->y after next' );
132is( $rect->w, 48, 'rect->w after next' );
133is( $rect->h, 48, 'rect->h after next' );
134
135$sprite->next;
136is( $sprite->current_frame, 3, 'sprite->current_frame after second next' );
137is( $sprite->current_loop,  1, 'sprite->current_loop after second next' );
138
139is( $clip->x, 48, 'clip->x after second next' );
140is( $clip->y, 96, 'clip->y after second next' );
141is( $clip->w, 48, 'clip->w after second next' );
142is( $clip->h, 48, 'clip->h after second next' );
143
144is( $rect->x, 0,  'rect->x after second next' );
145is( $rect->y, 0,  'rect->y after second next' );
146is( $rect->w, 48, 'rect->w after second next' );
147is( $rect->h, 48, 'rect->h after second next' );
148
149$sprite->next;
150is( $sprite->current_frame, 1, 'sprite->current_frame after third next' );
151is( $sprite->current_loop,  2, 'sprite->current_loop after second next' );
152
153is( $clip->x, 48, 'clip->x after third next' );
154is( $clip->y, 0,  'clip->y after third next' );
155is( $clip->w, 48, 'clip->w after third next' );
156is( $clip->h, 48, 'clip->h after third next' );
157
158is( $rect->x, 0,  'rect->x after third next' );
159is( $rect->y, 0,  'rect->y after third next' );
160is( $rect->w, 48, 'rect->w after third next' );
161is( $rect->h, 48, 'rect->h after third next' );
162
163is( $sprite->next, $sprite, 'next() returns the object' );
164
165is( $sprite->current_frame, 2, 'sprite->current_frame after next' );
166
167is( $sprite->previous, $sprite, 'previous() returns the object' );
168
169is( $sprite->current_frame, 1, 'sprite->current_frame after previous' );
170
171$sprite->next;
172is( $sprite->current_frame, 2, 'sprite->current_frame before reset' );
173
174is( $clip->x, 48, 'clip->x before reset' );
175is( $clip->y, 48, 'clip->y before reset' );
176is( $clip->w, 48, 'clip->w before reset' );
177is( $clip->h, 48, 'clip->h before reset' );
178
179is( $sprite->reset, $sprite, 'reset() returns the object' );
180
181is( $sprite->current_frame, 1, 'sprite->current_frame after reset' );
182
183is( $clip->x, 48, 'clip->x after reset' );
184is( $clip->y, 0,  'clip->y after reset' );
185is( $clip->w, 48, 'clip->w after reset' );
186is( $clip->h, 48, 'clip->h after reset' );
187
188$sprite = SDLx::Sprite::Animated->new(
189	image => 'test/data/hero.bmp',
190	rect  => SDL::Rect->new( 40, 50, 48, 48 ),
191);
192
193$clip = $sprite->clip;
194is( $clip->x, 0,  'clip->x after new with image and rect' );
195is( $clip->y, 0,  'clip->y after new with image and rect' );
196is( $clip->w, 48, 'clip->w after new with image and rect' );
197is( $clip->h, 48, 'clip->h after new with image and rect' );
198
199$rect = $sprite->rect;
200is( $rect->x, 40, 'rect->x after new with image and rect' );
201is( $rect->y, 50, 'rect->y after new with image and rect' );
202is( $rect->w, 48, 'rect->w after new with image and rect' );
203is( $rect->h, 48, 'rect->h after new with image and rect' );
204
205$sprite = SDLx::Sprite::Animated->new(
206	image => 'test/data/hero.bmp',
207	clip  => SDL::Rect->new( 0, 0, 48, 48 ),
208);
209
210$clip = $sprite->clip;
211is( $clip->x, 0,  'clip->x after new with image and clip' );
212is( $clip->y, 0,  'clip->y after new with image and clip' );
213is( $clip->w, 48, 'clip->w after new with image and clip' );
214is( $clip->h, 48, 'clip->h after new with image and clip' );
215
216$rect = $sprite->rect;
217is( $rect->x, 0,  'rect->x after new with image and clip' );
218is( $rect->y, 0,  'rect->y after new with image and clip' );
219is( $rect->w, 48, 'rect->w after new with image and clip' );
220is( $rect->h, 48, 'rect->h after new with image and clip' );
221
222$sprite = SDLx::Sprite::Animated->new(
223	image  => 'test/data/hero.bmp',
224	rect   => SDL::Rect->new( 40, 50, 48, 48 ),
225	step_x => 50,
226	step_y => 50,
227);
228$sprite->set_sequences(
229	left  => [ [ 1, 0 ], [ 1, 1 ], ],
230	right => [ [ 3, 0 ], [ 3, 1 ], ],
231);
232$sprite->sequence('left');
233
234$clip = $sprite->clip;
235is( $clip->x, 50, 'clip->x after new with step_x, step_y' );
236is( $clip->y, 0,  'clip->y after new with step_x, step_y' );
237is( $clip->w, 48, 'clip->w after new with step_x, step_y' );
238is( $clip->h, 48, 'clip->h after new with step_x, step_y' );
239
240$sprite->next;
241$clip = $sprite->clip;
242is( $clip->x, 50, 'clip->x after first next' );
243is( $clip->y, 50, 'clip->y after first next' );
244is( $clip->w, 48, 'clip->w after first next' );
245is( $clip->h, 48, 'clip->h after first next' );
246
247$sprite->next;
248$clip = $sprite->clip;
249is( $clip->x, 50, 'clip->x after second next' );
250is( $clip->y, 0,  'clip->y after second next' );
251is( $clip->w, 48, 'clip->w after second next' );
252is( $clip->h, 48, 'clip->h after second next' );
253
254$sprite->sequence('right');
255$clip = $sprite->clip;
256is( $clip->x, 150, 'clip->x after sequence change' );
257is( $clip->y, 0,   'clip->y after sequence change' );
258is( $clip->w, 48,  'clip->w after sequece change' );
259is( $clip->h, 48,  'clip->h after sequence change' );
260
261$sprite->next;
262$clip = $sprite->clip;
263is( $clip->x, 150, 'clip->x after first next' );
264is( $clip->y, 50,  'clip->y after first next' );
265is( $clip->w, 48,  'clip->w after first next' );
266is( $clip->h, 48,  'clip->h after first next' );
267
268$sprite->next;
269$clip = $sprite->clip;
270is( $clip->x, 150, 'clip->x after second next' );
271is( $clip->y, 0,   'clip->y after second next' );
272is( $clip->w, 48,  'clip->w after second next' );
273is( $clip->h, 48,  'clip->h after second next' );
274
275$sprite = SDLx::Sprite::Animated->new(
276	image     => 'test/data/hero.bmp',
277	rect      => SDL::Rect->new( 40, 50, 48, 48 ),
278	max_loops => 2,
279);
280$sprite->set_sequences( up => [ [ 0, 0 ], [ 0, 1 ], ], );
281$sprite->sequence('up');
282$clip = $sprite->clip;
283is( $clip->y, 0, 'clip->y after new with max_loops' );
284
285$sprite->next;
286is( $clip->y, 48, 'clip->y after first next' );
287
288$sprite->next;
289is( $clip->y, 0, 'clip->y after second next' );
290
291$sprite->next;
292is( $clip->y, 48, 'clip->y after third next' );
293
294$sprite->next;
295is( $clip->y, 0, 'clip->y after fourth next' );
296
297$sprite->next;
298is( $clip->y, 0, 'clip->y after fifth next' );
299
300$sprite = SDLx::Sprite::Animated->new(
301	image => 'test/data/hero.bmp',
302	rect  => SDL::Rect->new( 40, 50, 48, 48 ),
303	type  => 'reverse'
304);
305$sprite->set_sequences( up => [ [ 0, 0 ], [ 0, 1 ], [ 0, 2 ], ], );
306$sprite->sequence('up');
307$clip = $sprite->clip;
308is( $clip->y, 0, 'clip->y after new with type = reverse' );
309is( $sprite->current_loop, 1,
310	'sprite->current_loop after new with type = reverse'
311);
312
313$sprite->next;
314is( $clip->y,               48, 'clip->y after first next' );
315is( $sprite->current_frame, 2,  'sprite->current_frame after first next' );
316is( $sprite->current_loop,  1,  'sprite->current_loop after first next' );
317
318$sprite->next;
319is( $clip->y,               96, 'clip->y after second next' );
320is( $sprite->current_frame, 3,  'sprite->current_frame after second next' );
321is( $sprite->current_loop,  1,  'sprite->current_loop after second next' );
322
323$sprite->next;
324is( $clip->y,               48, 'clip->y after third next' );
325is( $sprite->current_frame, 2,  'sprite->current_frame after third next' );
326is( $sprite->current_loop,  1,  'sprite->current_loop after third next' );
327
328$sprite->next;
329is( $clip->y,               0, 'clip->y after fourth next' );
330is( $sprite->current_frame, 1, 'sprite->current_frame after fourth next' );
331is( $sprite->current_loop,  2, 'sprite->current_loop after fourth next' );
332
333$sprite->next;
334is( $clip->y,               48, 'clip->y after fifth next' );
335is( $sprite->current_frame, 2,  'sprite->current_frame after fifth next' );
336is( $sprite->current_loop,  2,  'sprite->current_loop after fifth next' );
337
338$sprite->next;
339is( $clip->y,               96, 'clip->y after sixth next' );
340is( $sprite->current_frame, 3,  'sprite->current_frame after sixth next' );
341is( $sprite->current_loop,  2,  'sprite->current_loop after sixth next' );
342
343$sprite->next;
344is( $clip->y,               48, 'clip->y after seventh next' );
345is( $sprite->current_frame, 2,  'sprite->current_frame after seventh next' );
346is( $sprite->current_loop,  2,  'sprite->current_loop after seventh next' );
347
348$sprite = SDLx::Sprite::Animated->new(
349	image => 'test/data/hero.bmp',
350	rect  => SDL::Rect->new( 40, 50, 48, 48 ),
351);
352$sprite->set_sequences( up => [ [ 0, 0 ], [ 0, 1 ], ], );
353$sprite->sequence('up');
354$clip = $sprite->clip;
355is( $clip->y, 0, 'clip->y after new' );
356
357$sprite->previous;
358is( $clip->y, 48, 'clip->y after first previous' );
359
360$sprite->previous;
361is( $clip->y, 0, 'clip->y after second previous' );
362
363$sprite->previous;
364is( $clip->y, 48, 'clip->y after third previous' );
365
366$sprite = SDLx::Sprite::Animated->new(
367	image => 'test/data/hero.bmp',
368	rect  => SDL::Rect->new( 40, 50, 48, 48 ),
369	type  => 'reverse'
370);
371$sprite->set_sequences( up => [ [ 0, 0 ], [ 0, 1 ], [ 0, 2 ], ], );
372$sprite->sequence('up');
373$clip = $sprite->clip;
374is( $clip->y, 0, 'clip->y after new with type = reverse' );
375
376$sprite->previous;
377is( $clip->y,               96, 'clip->y after first previous' );
378is( $sprite->current_frame, 3,  'sprite->current_frame after first previous' );
379
380$sprite->previous;
381is( $clip->y,               48, 'clip->y after second previous' );
382is( $sprite->current_frame, 2,  'sprite->current_frame after second previous' );
383
384$sprite->previous;
385is( $clip->y,               0, 'clip->y after third previous' );
386is( $sprite->current_frame, 1, 'sprite->current_frame after third previous' );
387
388$sprite->previous;
389is( $clip->y,               48, 'clip->y after fourth previous' );
390is( $sprite->current_frame, 2,  'sprite->current_frame after fourth previous' );
391
392$sprite->previous;
393is( $clip->y,               96, 'clip->y after fifth previous' );
394is( $sprite->current_frame, 3,  'sprite->current_frame after fifth previous' );
395
396$sprite->previous;
397is( $clip->y,               48, 'clip->y after sixth previous' );
398is( $sprite->current_frame, 2,  'sprite->current_frame after sixth previous' );
399
400$sprite->previous;
401is( $clip->y,               0, 'clip->y after seventh previous' );
402is( $sprite->current_frame, 1, 'sprite->current_frame after seventh previous' );
403
404$sprite = SDLx::Sprite::Animated->new(
405	image     => 'test/data/hero.bmp',
406	rect      => SDL::Rect->new( 40, 50, 48, 48 ),
407	clip      => SDL::Rect->new( 48, 48, 48, 48 ),
408	sequences => { up => [ [ 0, 0 ], [ 0, 1 ] ] },
409	sequence  => 'up',
410);
411$clip = $sprite->clip;
412is( $clip->x, 48, 'clip->x after new with clip' );
413is( $clip->y, 48, 'clip->y after new with clip' );
414
415$sprite->next();
416is( $clip->x, 48, 'clip->x after first next' );
417is( $clip->y, 96, 'clip->y after first next' );
418
419$sprite->next();
420is( $clip->x, 48, 'clip->x after second next' );
421is( $clip->y, 48, 'clip->y after second next' );
422
423$sprite = SDLx::Sprite::Animated->new(
424	image => 'test/data/hero.bmp',
425	rect  => SDL::Rect->new( 40, 50, 48, 48 ),
426);
427$clip = $sprite->clip;
428is( $clip->x, 0, 'clip->x after new with no sequences' );
429is( $clip->y, 0, 'clip->y after new with no sequences' );
430
431my $sequences = [
432	[ 0, 0 ],  [ 48, 0 ],  [ 96, 0 ],  [ 144, 0 ],  [ 192, 0 ],
433	[ 0, 48 ], [ 48, 48 ], [ 96, 48 ], [ 144, 48 ], [ 192, 48 ],
434	[ 0, 96 ], [ 48, 96 ], [ 96, 96 ], [ 144, 96 ], [ 192, 96 ],
435];
436
437foreach my $count ( 1 .. 20 ) {
438	$sprite->next;
439	my $s = $sequences->[ $count % @$sequences ];
440	is( $clip->x, $s->[0], 'clip->x after ' . $count . '-th next' );
441	is( $clip->y, $s->[1], 'clip->y after ' . $count . '-th next' );
442}
443
444done_testing;
445
446#reset the old video driver
447if ($videodriver) {
448	$ENV{SDL_VIDEODRIVER} = $videodriver;
449} else {
450	delete $ENV{SDL_VIDEODRIVER};
451}
452