File: sprite.cpp

package info (click to toggle)
clanlib 1.0~svn3827-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 24,696 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,742; ansic: 463; perl: 424; php: 247; sh: 53
file content (1101 lines) | stat: -rw-r--r-- 30,541 bytes parent folder | download | duplicates (6)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#include "Display/display_precomp.h"
#include "API/Display/pixel_format.h"
#include "API/Display/pixel_buffer.h"
#include "API/Display/display.h"
#include "API/Display/display_window.h"
#include "API/Display/sprite.h"
#include "API/Display/sprite_description.h"
#include "API/Display/sprite_packer.h"
#include "API/Display/surface.h"
#include "API/Display/graphic_context.h"
#include "API/Core/System/error.h"
#include "API/Core/System/log.h"
#include "API/Core/Resources/resource_manager.h"
#include "sprite_generic.h"
#include "resourcedata_sprite.h"
#include "display_target.h"
#include "surface_generic.h"
#include "surface_target.h"

#include <math.h>

/////////////////////////////////////////////////////////////////////////////
// CL_Sprite construction:

CL_Sprite::CL_Sprite(const std::string &resource_id, CL_ResourceManager *manager)
: impl(0)
{
	resource = manager->get_resource(resource_id);
	resource.load();

	CL_ResourceData_Sprite *data =
		(CL_ResourceData_Sprite *) resource.get_data("sprite");

	if (!data)
		throw CL_Error("Resource '" + resource_id + "' is not of type 'sprite'");

	impl = new CL_Sprite_Generic;
	if (data->get_sprite().impl)
	{
		*impl = *data->get_sprite().impl;
		restart();
	}
}

void CL_Sprite::createFromDescription(const CL_SpriteDescription &spritedescription, bool pack_texture, unsigned int min_width, unsigned int min_height)
{
	impl = new CL_Sprite_Generic;
	
	if (pack_texture && CL_DisplayTarget::current()->enable_packer())
	{
		// Fetch max texture size
		CL_DisplayWindow *window = CL_Display::get_current_window();
		CL_Size max_surface_size = window->get_gc()->get_max_surface_size();

		// Pack frames into textures
		CL_SpritePacker packer(spritedescription);
		if(packer.pack(max_surface_size.width, max_surface_size.height, min_width, min_height) == false)
			throw CL_Error("Couldn't pack all frames into textures");

		// Fetch texture, description-frame and packed-frame lists
		const std::list<CL_SpriteDescription::FramePair> &frames = spritedescription.get_frames();
		std::list<CL_SpriteDescription::FramePair>::const_iterator it_frames;

		// Lock all pixelbuffers to avoid multiple loadings of same pb's:
		for (it_frames = frames.begin(); it_frames != frames.end(); ++it_frames)
		{
			CL_PixelBuffer buffer = (*it_frames).first;
			buffer.lock();
		}
		
		const std::list<CL_SpritePacker::TexturePair> &packed_frames = packer.get_frames();
		std::list<CL_SpritePacker::TexturePair>::const_iterator it_packed_frames;
		
		const std::vector<CL_Size> &texture_sizes = packer.get_texture_sizes();

		std::vector<CL_Surface> surfaces;
			
		// Set up a pixelbuffer format
		CL_PixelFormat format;
		format.set_depth(32);
		format.enable_colorkey(false);
		format.set_alpha_mask(0xFF000000);
		format.set_blue_mask (0x00FF0000);
		format.set_green_mask(0x0000FF00);
		format.set_red_mask  (0x000000FF);

		// Create all the textures needed for packing
//		CL_Log::log("debug", "New sprite:");
		for(unsigned int i = 0; i < texture_sizes.size(); i++)
		{
			int width = texture_sizes[i].width;
			int height = texture_sizes[i].height;
			
			CL_PixelBuffer buffer(width, height, 4 * width, format);
			buffer.lock();

//			CL_Log::log("debug", "- Packing texture %1x%2", width, height);

			// Copy frames into this texture
			for(it_frames = frames.begin(), it_packed_frames = packed_frames.begin();
				it_frames != frames.end();
				++it_frames, ++it_packed_frames)
			{
				if((*it_packed_frames).first == static_cast<int>(i))	// Frame uses this buffer
				{
					CL_PixelBuffer src = (*it_frames).first;
					CL_PixelBuffer dest = buffer;

					CL_Rect src_rect  = (*it_frames).second;
					CL_Rect dest_rect = (*it_packed_frames).second;

					// Remove border around image:
					dest_rect.left++;
					dest_rect.right--;
					dest_rect.top++;
					dest_rect.bottom--;
					
					src.convert(dest.get_data(), format, dest.get_pitch(), dest_rect, src_rect);

					int x, y;
					int dest_pitch = dest.get_pitch();
					unsigned char *data_bytes = (unsigned char*) dest.get_data();

					// Make transparent pixels grey:
					for (y=dest_rect.top; y<dest_rect.bottom; y++)
					{
						unsigned int *line = (unsigned int *) (data_bytes+y*dest_pitch);
						for (x=dest_rect.left; x<dest_rect.right; x++)
						{
							// Test for transparent pixels surrounded by non transparent ones
							if ((line[x] & 0xff000000) == 0)
							{
								int r=0,g=0,b=0,n=0;
                                if(x < dest_rect.right-1) {
									if((line[x+1] & 0xff000000) != 0)
									{
										r+= (line[x+1] & 0x00ff0000) >> 16;
										g+= (line[x+1] & 0x0000ff00) >> 8;
										b+= (line[x+1] & 0x000000ff);
										n++;
									}
								}
								if(x > 0) {
									if((line[x-1] & 0xff000000) != 0)
									{
										r+= (line[x-1] & 0x00ff0000) >> 16;
										g+= (line[x-1] & 0x0000ff00) >> 8;
										b+= (line[x-1] & 0x000000ff);
										n++;
									}
								}
								if(y < dest_rect.bottom-1) {
									line = (unsigned int *) (data_bytes+(y+1)*dest_pitch);
									if((line[x] & 0xff000000) != 0)
									{
										r+= (line[x] & 0x00ff0000) >> 16;
										g+= (line[x] & 0x0000ff00) >> 8;
										b+= (line[x] & 0x000000ff);
										n++;
									}
								}
								if(y > 0) {
									line = (unsigned int *) (data_bytes+(y-1)*dest_pitch);
									if((line[x] & 0xff000000) != 0)
									{
										r+= (line[x] & 0x00ff0000) >> 16;
										g+= (line[x] & 0x0000ff00) >> 8;
										b+= (line[x] & 0x000000ff);
										n++;
									}
								}

								line = (unsigned int *) (data_bytes+y*dest_pitch);
								if(n>0)
								{
									// Set colour to the avarage of the found pixels
									line[x] = ( ((r/n)<<16) | ((g/n)<<8) | (b/n) );
								}
								else line[x] = 0x007f7f7f;
							}
						}
					}

					// Copy pixels in border area:
					unsigned int *top_line = (unsigned int *) (data_bytes+(dest_rect.top-1)*dest_pitch);
					unsigned int *next_line = (unsigned int *) (data_bytes+dest_rect.top*dest_pitch);
					unsigned int *bottom_line = (unsigned int *) (data_bytes+(dest_rect.bottom)*dest_pitch);
					unsigned int *prev_line = (unsigned int *) (data_bytes+(dest_rect.bottom-1)*dest_pitch);
					for (x=dest_rect.left; x<dest_rect.right; x++)
					{
						top_line[x] = next_line[x];
						bottom_line[x] = prev_line[x];
					}
					for (y=dest_rect.top; y<dest_rect.bottom; y++)
					{
						unsigned int *line = (unsigned int *) (data_bytes+y*dest_pitch);
						line[dest_rect.left-1] = line[dest_rect.left];
						line[dest_rect.right] = line[dest_rect.right-1];
					}
					#define dest_pixel(x,y) (*((unsigned int *) (data_bytes+(y)*dest_pitch)+x))
					dest_pixel(dest_rect.left-1, dest_rect.top-1) = dest_pixel(dest_rect.left, dest_rect.top);
					dest_pixel(dest_rect.right, dest_rect.top-1) = dest_pixel(dest_rect.right-1, dest_rect.top);
					dest_pixel(dest_rect.left-1, dest_rect.bottom) = dest_pixel(dest_rect.left, dest_rect.bottom-1);
					dest_pixel(dest_rect.right, dest_rect.bottom) = dest_pixel(dest_rect.right-1, dest_rect.bottom-1);
				}
			}
			
			// Create CL_Surface
			surfaces.push_back(CL_Surface(buffer, spritedescription.get_surface_flag()));
			
			// Clean up
			buffer.unlock();
		}

		// Create SpriteFrames
		for(it_frames = frames.begin(), it_packed_frames = packed_frames.begin();
			it_frames != frames.end();
			++it_frames, ++it_packed_frames)
		{
			CL_Sprite_Generic::SpriteFrame frame;
			
			frame.surface = surfaces[(*it_packed_frames).first];
			frame.position = (*it_packed_frames).second;
			frame.delay = 0.06f;
			frame.offset = CL_Point(0, 0);
			
			// Remove border around image:
			frame.position.left++;
			frame.position.right--;
			frame.position.top++;
			frame.position.bottom--;
			
			impl->frames.push_back(frame);
		}

		// Unlock all pixelbuffers:
		for (it_frames = frames.begin(); it_frames != frames.end(); ++it_frames)
		{
			CL_PixelBuffer buffer = (*it_frames).first;
			buffer.unlock();
		}
	}
	else
	{
		// Create SpriteFrames
		const std::list<CL_SpriteDescription::FramePair> &frames = spritedescription.get_frames();
		std::list<CL_SpriteDescription::FramePair>::const_iterator it_frames;
		std::map<CL_PixelBuffer, CL_Surface> surfaces;

		for(it_frames = frames.begin();
			it_frames != frames.end();
			++it_frames)
		{	
			CL_Sprite_Generic::SpriteFrame frame;

			std::map<CL_PixelBuffer, CL_Surface>::iterator i = surfaces.find((*it_frames).first);
			if (i == surfaces.end())
			{
				frame.surface  = CL_Surface((*it_frames).first);
				surfaces[(*it_frames).first] = frame.surface;
			}
			else
			{
				frame.surface = i->second;
			}

			frame.position = (*it_frames).second;
			frame.delay = 0.06f;
			frame.offset = CL_Point(0, 0);

			impl->frames.push_back(frame);
		}
	}
	
	restart();
}

CL_Sprite::CL_Sprite(const CL_SpriteDescription &spritedescription, bool pack_texture, unsigned int min_width, unsigned int min_height)
{
	createFromDescription(spritedescription, pack_texture, min_width, min_height);
}

CL_Sprite::CL_Sprite(const CL_SpriteDescription &spritedescription, bool pack_texture)
{
	createFromDescription(spritedescription, pack_texture, 16, 16);
}


CL_Sprite::CL_Sprite(const CL_Sprite &sprite)
: impl(0)
{
	if (sprite.impl)
	{
		impl = new CL_Sprite_Generic;
		*impl = *sprite.impl;
	
		resource = sprite.resource;
		resource.load();
	}
}

CL_Sprite::CL_Sprite()
: impl(0)
{
}

CL_Sprite::~CL_Sprite()
{
	delete impl;
	resource.unload();
}

/////////////////////////////////////////////////////////////////////////////
// CL_Sprite attributes:

float CL_Sprite::get_angle() const
{
	return impl->angle;
}

float CL_Sprite::get_angle_pitch() const
{
	return impl->angle_pitch;
}

float CL_Sprite::get_angle_yaw() const
{
	return impl->angle_yaw;
}

float CL_Sprite::get_base_angle() const
{
	return impl->base_angle;
}

void CL_Sprite::get_scale(float &x, float &y) const
{
	x = impl->scale_x;
	y = impl->scale_y;
}

float CL_Sprite::get_alpha() const
{
	return impl->alpha;
}

void CL_Sprite::get_color(float &red, float &green, float &blue, float &alpha) const
{
	red = impl->red;
	green = impl->green;
	blue = impl->blue;
	alpha = impl->alpha;
}

void CL_Sprite::get_blend_func(CL_BlendFunc &src, CL_BlendFunc &dest) const
{
	src  = impl->blend_src;
	dest = impl->blend_dest;
}

void CL_Sprite::get_alignment(CL_Origin &origin, int &x, int &y) const
{
	origin = impl->translation_origin;
	x = impl->translation_hotspot.x;
	y = impl->translation_hotspot.y;
}

void CL_Sprite::get_rotation_hotspot(CL_Origin &origin, int &x, int &y) const
{
	origin = impl->rotation_origin;
	x = impl->rotation_hotspot.x;
	y = impl->rotation_hotspot.y;
}

int CL_Sprite::get_current_frame() const
{
	return impl->current_frame;
}

int CL_Sprite::get_frame_count() const
{
	if (!impl) return 0;
	return impl->frames.size();
}

float CL_Sprite::get_frame_delay(int frameno) const
{
	CL_Sprite_Generic::SpriteFrame *frame = impl->get_frame(frameno);
	if(frame)
		return frame->delay;
	else
		return 0;
}

CL_Point CL_Sprite::get_frame_offset(int frameno) const
{
	CL_Sprite_Generic::SpriteFrame *frame = impl->get_frame(frameno);
	if(frame)
		return frame->offset;
	else
		return CL_Point(0, 0);
}

CL_Size CL_Sprite::get_frame_size(int frameno) const
{
	CL_Sprite_Generic::SpriteFrame *frame = impl->get_frame(frameno);
	if(frame)
		return CL_Size(frame->position.get_width(), frame->position.get_height());
	else
		return CL_Size(0, 0);
}

int CL_Sprite::get_width() const
{
	return impl->get_frame(impl->current_frame)->position.get_width();
}

int CL_Sprite::get_height() const
{
	return impl->get_frame(impl->current_frame)->position.get_height();
}

CL_Surface CL_Sprite::get_frame_surface(int frameno) const
{
	CL_Sprite_Generic::SpriteFrame *frame = impl->get_frame(frameno);
	if (frame)
		return frame->surface;
	else
		return CL_Surface();
}

CL_Surface CL_Sprite::get_frame_surface(int frameno, CL_Rect &surface_rect) const
{
	CL_Sprite_Generic::SpriteFrame *frame = impl->get_frame(frameno);
	if (frame)
	{
		surface_rect = frame->position;
		return frame->surface;
	}
	else
		return CL_Surface();
}

CL_PixelBuffer CL_Sprite::get_frame_pixeldata(int frameno) const
{
	CL_Sprite_Generic::SpriteFrame *frame = impl->get_frame(frameno);
	if (frame)
	{
		CL_PixelBuffer pb = frame->surface.get_pixeldata();
		int w = get_width();
		int h = get_height();
		// Make sure dimensions are the same as this sprite.
		// pb will always be larger than, or equal to the sprite's size
		
		if (w != pb.get_width() || h != pb.get_height())
		{
			int p = pb.get_pitch();
			int bytes_per_pixel = (pb.get_format().get_depth() + 7)/8;
			CL_PixelBuffer new_pb(w, h, p, pb.get_format());
			unsigned char *src_bytes = (unsigned char*) pb.get_data();
			unsigned char *dest_bytes = (unsigned char*) new_pb.get_data();

			src_bytes += frame->position.top * p; //move down to where it really starts if applicable

			for (int y = 0; y < h; y++)
				for (int x = 0; x < w * bytes_per_pixel; x++)
					dest_bytes[y * p + x] = src_bytes[y*p + x + (frame->position.left * bytes_per_pixel)];
			return new_pb;
		}
		else
			return pb;
	}
	else
		return CL_PixelBuffer();
}

int CL_Sprite::get_id() const
{
	return impl->id;
}

bool CL_Sprite::is_null() const
{
	return (impl == 0);
}

bool CL_Sprite::is_play_loop() const
{
	return impl->play_loop;
}

bool CL_Sprite::is_play_backward() const
{
	return impl->play_backward;
}

bool CL_Sprite::is_play_pingpong() const
{
	return impl->play_pingpong;
}

CL_Sprite::ShowOnFinish CL_Sprite::get_show_on_finish() const
{
	return impl->show_on_finish;
}

bool CL_Sprite::is_finished() const
{
	return impl->finished;
}

bool CL_Sprite::is_looping() const
{
	return impl->looping;
}

/////////////////////////////////////////////////////////////////////////////
// CL_Sprite operations:

CL_Sprite &CL_Sprite::operator =(const CL_Sprite &copy)
{
	resource.unload();
	if (impl) delete impl;
	impl = 0;
	if (copy.impl)
	{
		impl = new CL_Sprite_Generic;
		*impl = *copy.impl;
	}
	resource = copy.resource;
	resource.load();
	return *this;
}

CL_Sprite::operator bool() const
{
  return (impl != 0);
}

void CL_Sprite::set_image_data(const CL_Sprite &image_source)
{
	if(!impl)
		impl = new CL_Sprite_Generic;

	impl->frames = image_source.impl->frames;

	impl->id = image_source.get_id();
	impl->play_loop = image_source.is_play_loop();
	impl->play_backward = image_source.is_play_backward();
	impl->play_pingpong = image_source.is_play_pingpong();
	impl->base_angle = image_source.get_base_angle();

	restart();
}
	


bool CL_Sprite::setup_draw_params(float x, float y, CL_Surface_DrawParams1 & params1, bool sub_pixel_accuracy)
{
	if (impl->finished && impl->show_on_finish == show_blank)
	{
		return false; //nothing to draw, signal invalid data
	}

	CL_Sprite_Generic::SpriteFrame &frame = impl->frames[impl->current_frame];

	static CL_Surface_DrawParams2 params2;
	
	params2.destX = x;
	params2.destY = y;
	params2.srcX = frame.position.left;
	params2.srcY = frame.position.top;
	params2.srcWidth = frame.position.get_width();
	params2.srcHeight = frame.position.get_height();
	params2.destZ = 0.0;
	params2.red = impl->red;
	params2.green = impl->green;
	params2.blue = impl->blue;
	params2.alpha = impl->alpha;
	params2.blend_src = impl->blend_src;
	params2.blend_dest = impl->blend_dest;
	params2.blendfunc_src_alpha  = impl->blendfunc_src_alpha;
	params2.blendfunc_dest_alpha = impl->blendfunc_dest_alpha;
	params2.scale_x = impl->scale_x;
	params2.scale_y = impl->scale_y;
	params2.translate_origin = impl->translation_origin;
	params2.translate_x = impl->translation_hotspot.x + frame.offset.x;
	params2.translate_y = impl->translation_hotspot.y + frame.offset.y;
	params2.rotate_angle = impl->angle - impl->base_angle;
	params2.rotate_pitch = impl->angle_pitch;
	params2.rotate_yaw = impl->angle_yaw;
	params2.rotate_origin = impl->rotation_origin;
	params2.rotate_x = impl->rotation_hotspot.x + frame.offset.x;
	params2.rotate_y = impl->rotation_hotspot.y + frame.offset.y;
	params2.sub_pixel_accuracy = sub_pixel_accuracy;

	static CL_Surface_TargetDrawParams1 t_params1;

	frame.surface.setup_target_params(params2, t_params1);
	frame.surface.setup_draw_params(params2, &t_params1, params1);
	
	return true;
}


void CL_Sprite::draw(const CL_Surface_DrawParams1 & params1, CL_GraphicContext *gc /* = 0 */)
{
	if(impl->finished == false || impl->show_on_finish != show_blank)
	{
		CL_Sprite_Generic::SpriteFrame &frame = impl->frames[impl->current_frame];
		if (gc == 0) gc = CL_Display::get_current_window()->get_gc();

		frame.surface.draw(params1, gc);
	}
}

void CL_Sprite::draw(
	float x,
	float y,
	CL_GraphicContext *gc)
{
	if(impl->finished == false || impl->show_on_finish != show_blank)
	{
	
		CL_Sprite_Generic::SpriteFrame &frame = impl->frames[impl->current_frame];

		if (gc == 0) gc = CL_Display::get_current_window()->get_gc();

		static CL_Surface_DrawParams2 params2;
		
		params2.destX = x;
		params2.destY = y;
		params2.srcX = frame.position.left;
		params2.srcY = frame.position.top;
		params2.srcWidth = frame.position.get_width();
		params2.srcHeight = frame.position.get_height();
		params2.destZ = 0.0;
		params2.red = impl->red;
		params2.green = impl->green;
		params2.blue = impl->blue;
		params2.alpha = impl->alpha;
		params2.blend_src = impl->blend_src;
		params2.blend_dest = impl->blend_dest;
		params2.blendfunc_src_alpha  = impl->blendfunc_src_alpha;
		params2.blendfunc_dest_alpha = impl->blendfunc_dest_alpha;
		params2.scale_x = impl->scale_x;
		params2.scale_y = impl->scale_y;
		params2.translate_origin = impl->translation_origin;
		params2.translate_x = impl->translation_hotspot.x + frame.offset.x;
		params2.translate_y = impl->translation_hotspot.y + frame.offset.y;
		params2.rotate_angle = impl->angle - impl->base_angle;
		params2.rotate_pitch = impl->angle_pitch;
		params2.rotate_yaw = impl->angle_yaw;
		params2.rotate_origin = impl->rotation_origin;
		params2.rotate_x = impl->rotation_hotspot.x + frame.offset.x;
		params2.rotate_y = impl->rotation_hotspot.y + frame.offset.y;

		frame.surface.draw(params2,
                                   gc);
	}
}


void CL_Sprite::draw_subpixel(
					 float x,
					 float y,
					 CL_GraphicContext *gc)
{
	if(impl->finished == false || impl->show_on_finish != show_blank)
	{
	
		CL_Sprite_Generic::SpriteFrame &frame = impl->frames[impl->current_frame];

		if (gc == 0) gc = CL_Display::get_current_window()->get_gc();

		static CL_Surface_DrawParams2 params2;
	
		params2.destX = x;
		params2.destY = y;
		params2.srcX = frame.position.left;
		params2.srcY = frame.position.top;
		params2.srcWidth = frame.position.get_width();
		params2.srcHeight = frame.position.get_height();
		params2.destZ = 0.0;
		params2.red = impl->red;
		params2.green = impl->green;
		params2.blue = impl->blue;
		params2.alpha = impl->alpha;
		params2.blend_src = impl->blend_src;
		params2.blend_dest = impl->blend_dest;
		params2.blendfunc_src_alpha  = impl->blendfunc_src_alpha;
		params2.blendfunc_dest_alpha = impl->blendfunc_dest_alpha;
		params2.scale_x = impl->scale_x;
		params2.scale_y = impl->scale_y;
		params2.translate_origin = impl->translation_origin;
		params2.translate_x = impl->translation_hotspot.x + frame.offset.x;
		params2.translate_y = impl->translation_hotspot.y + frame.offset.y;
		params2.rotate_angle = impl->angle - impl->base_angle;
		params2.rotate_pitch = impl->angle_pitch;
		params2.rotate_yaw = impl->angle_yaw;
		params2.rotate_origin = impl->rotation_origin;
		params2.rotate_x = impl->rotation_hotspot.x + frame.offset.x;
		params2.rotate_y = impl->rotation_hotspot.y + frame.offset.y;
		params2.sub_pixel_accuracy = true;

		frame.surface.draw(params2,
			gc);
	}
}

void CL_Sprite::draw(
	const CL_Rect &dest,
	CL_GraphicContext *gc)
{
	if(impl->finished == false || impl->show_on_finish != show_blank)
	{
		CL_Sprite_Generic::SpriteFrame &frame = impl->frames[impl->current_frame];
		
		if (gc == 0) gc = CL_Display::get_current_window()->get_gc();

		static CL_Surface_DrawParams2 params2;
		params2.srcX = frame.position.left;
		params2.srcY = frame.position.top;
		params2.srcWidth = frame.position.get_width();
		params2.srcHeight = frame.position.get_height();
		params2.destX = dest.left;
		params2.destY = dest.top;
		params2.destZ = 0.0;
		params2.red = impl->red;
		params2.green = impl->green;
		params2.blue = impl->blue;
		params2.alpha = impl->alpha;
		params2.blend_src = impl->blend_src;
		params2.blend_dest = impl->blend_dest;
		params2.blendfunc_src_alpha  = impl->blendfunc_src_alpha;
		params2.blendfunc_dest_alpha = impl->blendfunc_dest_alpha;
		params2.scale_x = dest.get_width()/float(frame.position.get_width());
		params2.scale_y = dest.get_height()/float(frame.position.get_height());
		params2.translate_origin = impl->translation_origin;
		params2.translate_x = impl->translation_hotspot.x + frame.offset.x;
		params2.translate_y = impl->translation_hotspot.y + frame.offset.y;
		params2.rotate_angle = impl->angle - impl->base_angle;
		params2.rotate_pitch = impl->angle_pitch;
		params2.rotate_yaw = impl->angle_yaw;
		params2.rotate_origin = impl->rotation_origin;
		params2.rotate_x = impl->rotation_hotspot.x + frame.offset.x;
		params2.rotate_y = impl->rotation_hotspot.y + frame.offset.y;

		frame.surface.draw(
			params2,
			gc);
	}
}

void CL_Sprite::draw(
					 const CL_Rectf &dest,
					 CL_GraphicContext *gc)
{
	if(impl->finished == false || impl->show_on_finish != show_blank)
	{
		CL_Sprite_Generic::SpriteFrame &frame = impl->frames[impl->current_frame];

		if (gc == 0) gc = CL_Display::get_current_window()->get_gc();

		static CL_Surface_DrawParams2 params2;
		params2.srcX = frame.position.left;
		params2.srcY = frame.position.top;
		params2.srcWidth = frame.position.get_width();
		params2.srcHeight = frame.position.get_height();
		params2.destX = dest.left;
		params2.destY = dest.top;
		params2.destZ = 0.0;
		params2.red = impl->red;
		params2.green = impl->green;
		params2.blue = impl->blue;
		params2.alpha = impl->alpha;
		params2.blend_src = impl->blend_src;
		params2.blend_dest = impl->blend_dest;
		params2.blendfunc_src_alpha  = impl->blendfunc_src_alpha;
		params2.blendfunc_dest_alpha = impl->blendfunc_dest_alpha;
		params2.scale_x = dest.get_width()/float(frame.position.get_width());
		params2.scale_y = dest.get_height()/float(frame.position.get_height());
		params2.translate_origin = impl->translation_origin;
		params2.translate_x = impl->translation_hotspot.x + frame.offset.x;
		params2.translate_y = impl->translation_hotspot.y + frame.offset.y;
		params2.rotate_angle = impl->angle - impl->base_angle;
		params2.rotate_pitch = impl->angle_pitch;
		params2.rotate_yaw = impl->angle_yaw;
		params2.rotate_origin = impl->rotation_origin;
		params2.rotate_x = impl->rotation_hotspot.x + frame.offset.x;
		params2.rotate_y = impl->rotation_hotspot.y + frame.offset.y;
		params2.sub_pixel_accuracy = true;

		frame.surface.draw(
			params2,
			gc);
	}
}

float CL_Sprite::update(float time_elapsed)
{
	impl->looping = false;
	
	if(time_elapsed == 0)
		time_elapsed = impl->calc_time_elapsed();

	int total_frames = impl->frames.size();
	
	//we still want to know when a 1 frame 'anim' loops, based on the timer -mrfun
	//if(total_frames < 2 || impl->finished)
	//	return time_elapsed; 
	// but we need to stop when animation is finished -gpmfuchs
	if (impl->finished) return time_elapsed;

	CL_Sprite_Generic::SpriteFrame *frame = &impl->frames[impl->current_frame];

	impl->update_time += time_elapsed;
	while(impl->update_time > frame->delay)
	{
		impl->update_time -= frame->delay;
		impl->current_frame += impl->delta_frame;

		// Beginning or end of loop ?
		if(impl->current_frame >= total_frames || impl->current_frame < 0)
		{
			if(impl->play_loop == false)
			{
				int delta_frame = impl->play_backward ? -1 : 1;
				if(delta_frame != impl->delta_frame || impl->play_pingpong == false)
				{
					finish();
					return time_elapsed;
				}
			}
				
			if(impl->play_pingpong == true)
			{
				impl->delta_frame = -impl->delta_frame;	// Change direction
				if(impl->delta_frame > 0)
				{
					impl->current_frame = 1;
					//during a ping pong, we only count when we re-start in the forward direction as a loop
					impl->looping = true;
				}
				else
					impl->current_frame = total_frames - 2;
			}
			else // Restart
			{
				impl->current_frame = impl->play_backward ? total_frames - 1 : 0;
				impl->looping = true;
			}
		}
	}

	return time_elapsed;
}

void CL_Sprite::set_angle(float angle)
{
	impl->angle = angle;

	if (impl->angle >= 0)
		impl->angle = (float)fmod(impl->angle, 360.0f);
	else
		impl->angle = (float)fmod (impl->angle, 360.0f) + 360.0f;
}

void CL_Sprite::set_angle_pitch(float angle)
{
	impl->angle_pitch = angle;

	if (impl->angle_pitch >= 0)
		impl->angle_pitch = (float)fmod(impl->angle_pitch, 360.0f);
	else
		impl->angle_pitch = (float)fmod (impl->angle_pitch, 360.0f) + 360.0f;
}

void CL_Sprite::set_angle_yaw(float angle)
{
	impl->angle_yaw = angle;

	if (impl->angle_yaw >= 0)
		impl->angle_yaw = (float)fmod(impl->angle_yaw, 360.0f);
	else
		impl->angle_yaw = (float)fmod (impl->angle_yaw, 360.0f) + 360.0f;
}

void CL_Sprite::rotate(float angle)
{
	impl->angle += angle;

	if (impl->angle >= 0)
		impl->angle = (float)fmod(impl->angle, 360.0f);
	else
		impl->angle = (float)fmod(impl->angle, 360.0f) + 360.0f;
}

void CL_Sprite::rotate_pitch(float angle)
{
	impl->angle_pitch += angle;

	if (impl->angle_pitch >= 0)
		impl->angle_pitch = (float)fmod(impl->angle_pitch, 360.0f);
	else
		impl->angle_pitch = (float)fmod(impl->angle_pitch, 360.0f) + 360.0f;
}

void CL_Sprite::rotate_yaw(float angle)
{
	impl->angle_yaw += angle;

	if (impl->angle_yaw >= 0)
		impl->angle_yaw = (float)fmod(impl->angle_yaw, 360.0f);
	else
		impl->angle_yaw = (float)fmod(impl->angle_yaw, 360.0f) + 360.0f;
}

void CL_Sprite::set_base_angle(float angle)
{
	impl->base_angle = angle;

	while (impl->base_angle < 0.0f)
		impl->base_angle += 360.0f;
	while (impl->base_angle > 360.0f)
		impl->base_angle -= 360.0f;
}

void CL_Sprite::set_scale(float x, float y)
{
	impl->scale_x = x;
	impl->scale_y = y;
}

void CL_Sprite::set_alpha(float alpha)
{
	impl->alpha = alpha;
}

void CL_Sprite::set_color(float r, float g, float b, float a)
{
	impl->red = r;
	impl->green = g;
	impl->blue = b;
	impl->alpha = a;
}

void CL_Sprite::set_blend_func(CL_BlendFunc src, CL_BlendFunc dest)
{
	impl->blend_src        = src;
	impl->blend_dest       = dest;
	impl->blendfunc_src_alpha  = src;
	impl->blendfunc_dest_alpha = dest;
}

void CL_Sprite::set_blend_func_separate(CL_BlendFunc src, CL_BlendFunc dest, 
                                        CL_BlendFunc src_alpha, CL_BlendFunc dest_alpha)
{
	impl->blend_src        = src;
	impl->blend_dest       = dest;
	impl->blendfunc_src_alpha  = src_alpha;
	impl->blendfunc_dest_alpha = dest_alpha;
}

void CL_Sprite::set_alignment(CL_Origin origin, int x, int y)
{
	impl->translation_origin = origin;
	impl->translation_hotspot.x = x;
	impl->translation_hotspot.y = y;
}

void CL_Sprite::set_rotation_hotspot(CL_Origin origin, int x, int y)
{
	impl->rotation_origin = origin;
	impl->rotation_hotspot.x = x;
	impl->rotation_hotspot.y = y;
}

void CL_Sprite::set_frame(unsigned int frame)
{
	if(frame < 0)
		impl->current_frame = 0;
	else if(frame >= impl->frames.size())
		impl->current_frame = impl->frames.size() - 1;
	else
		impl->current_frame = frame;
}

void CL_Sprite::set_frame_delay(int frameno, float delay)
{
	CL_Sprite_Generic::SpriteFrame *frame = impl->get_frame(frameno);
	if(frame)
		frame->delay = delay;
}

void CL_Sprite::set_frame_offset(int frameno, CL_Point offset)
{
	CL_Sprite_Generic::SpriteFrame *frame = impl->get_frame(frameno);
	if(frame)
		frame->offset = offset;
}

void CL_Sprite::set_id(int id)
{
	impl->id = id;
}

void CL_Sprite::finish()
{
	impl->finished = true;
	if(impl->show_on_finish == CL_Sprite::show_first_frame)
		impl->current_frame = 0;
	else
		impl->current_frame = impl->frames.size() - 1;

	impl->sig_animation_finished();
}

void CL_Sprite::restart()
{
	impl->update_time = 0;
	impl->last_time = 0;
	impl->finished = false;
	impl->current_frame = impl->play_backward ? impl->frames.size() - 1 : 0;
	impl->delta_frame = impl->play_backward ? -1 : 1;
}

void CL_Sprite::set_play_loop(bool loop)
{
	impl->play_loop = loop;
}

void CL_Sprite::set_play_pingpong(bool pingpong)
{
	impl->play_pingpong = pingpong;
}

void CL_Sprite::set_play_backward(bool backward)
{
	impl->play_backward = backward;
}

void CL_Sprite::set_show_on_finish(CL_Sprite::ShowOnFinish show_on_finish)
{
	impl->show_on_finish = show_on_finish;
}

void CL_Sprite::add_frame(CL_Surface surface, const CL_Rect& rect, float delay, CL_Point offset)
{
	if (!impl)
		impl = new CL_Sprite_Generic;
	
	CL_Sprite_Generic::SpriteFrame frame;
			
	frame.surface  = surface;
        if (rect == CL_Rect(0,0,0,0))
          frame.position = CL_Rect(CL_Point(0, 0), CL_Size(surface.get_width(), surface.get_height()));
        else
          frame.position = rect;
	frame.delay    = delay;
	frame.offset   = offset;
			
	impl->frames.push_back(frame);
}

/////////////////////////////////////////////////////////////////////////////
// CL_Sprite signals:

CL_Signal_v0 &CL_Sprite::sig_animation_finished()
{
	return impl->sig_animation_finished;
}