Debian Codegraph @

src/base_rendering/castleglimages_sprite.inc

raw
1 {%MainUnit castleglimages.pas}
2 {
3 Copyright 2015-2018 Tomasz Wojtyś, Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Part of CastleGLImages unit: drawing 2D sprites on screen (TSprite class). }
18
19 {$ifdef read_interface}
20 {$ifdef read_interface_type}
21 // type (continue type section from previous include file)
22 { A frame of a custom animation. Part of the TSpriteAnimation. }
23 TSpriteAnimationFrame = record
24 { Frame number from the sprite. }
25 Frame: Cardinal;
26 { This frame individual duration time (in seconds). }
27 Duration: Single;
28 end;
29
30 { Custom animation of a sprite.
31 Used by TSprite.AddAnimation and TSprite.PlayAnimation.
32 Animation is defined as an array of frames. }
33 TSpriteAnimation = class
34 strict private
35 FDuration: TFloatTime;
36 private
37 Frames: array of TSpriteAnimationFrame;
38 public
39 constructor CreateConstantFps(const AFrames: array of Cardinal; const FramesPerSecond: Single);
40 constructor Create(const AFrames: array of TSpriteAnimationFrame);
41 { Duration, in seconds, of this animation. }
42 property Duration: TFloatTime read FDuration;
43 end;
44
45 { Sprite is an animation composed from frames arranged
46 in rows and columns inside a single image.
47 Frames are read from left to right and from top to bottom.
48
49 In the simple case, a sprite represents a single animation,
50 and the frames are just in consecutive order.
51
52 With the help of "custom animations" feature, you can define many
53 animations in a sprite.
54 Each custom animation is an array of frames with
55 corresponding frames duration. Frames can be in any order. It is valid
56 to use some particural frame many times with different
57 duration time.
58
59 @deprecated
60 Instead of this, load sprite sheets to TCastleScene from .castle-sprite-sheet format. }
61 TSprite = class
62 private
63 type
64 TSpriteAnimations = {$ifdef FPC}specialize{$endif} TObjectList<TSpriteAnimation>;
65 var
66 FImage: TDrawableImage;
67 FX, FY: Single;
68 FDrawingWidth, FDrawingHeight: Single;
69 FFrames, FFrameWidth, FFrameHeight, FColumns: Cardinal;
70 FFramesPerSecond: Single;
71 FTime: TFloatTime;
72 FFrame, FCustomFrame: Cardinal;
73 FPlaying: Boolean;
74 FTimeLoop: Boolean;
75 FLeftMargin, FTopMargin: Cardinal;
76 FHorizontalSpacing, FVerticalSpacing: Cardinal;
77 FVerticalSpacingBottom: boolean;
78 FTimeBackwards: Boolean;
79 FCustomAnimations: TSpriteAnimations;
80 FCurrentAnimation: Integer;
81 FCustomAnimTimestamp: TFloatTime;
82 FHorizontalFlip, FVerticalFlip: Boolean;
83 FSmoothScalingSafeBorder, FSmoothScaling: Boolean;
84 FOwnsImage: Boolean;
85 function GetCenterX: Single;
86 function GetCenterY: Single;
87 function GetFrameRect: TFloatRectangle;
88 function GetDrawRect: TFloatRectangle;
89 function GetRotationCenter: TVector2;
90 function GetPosition: TVector2;
91 function GetRotation: Single;
92 function GetColor: TCastleColor;
93 procedure SetTime(const AValue: TFloatTime);
94 procedure SetRotationCenter(const AValue: TVector2);
95 procedure SetCenterX(const AValue: Single);
96 procedure SetCenterY(const AValue: Single);
97 procedure SetDrawRect(const AValue: TFloatRectangle);
98 procedure SetFramesPerSecond(const AValue: Single);
99 procedure SetFrame(const AValue: Cardinal);
100 procedure SetPosition(const APosition: TVector2);
101 procedure SetRotation(const AValue: Single);
102 procedure SetColor(const AValue: TCastleColor);
103 procedure SetSmoothScaling(const Value: Boolean);
104 public
105 { Constructor.
106 @param(Url URL of source image.)
107 @param(AFrames Total numer of animation frames.)
108 @param(AColumns Mumber of columns.)
109 @param(ARows Number of rows.)
110 @param(ASmoothscaling Enables interpolation, see @link(TDrawableImage.SmoothScaling) for details, default @True.)
111 @param(ATimeLoop Should animation be repeated?, default @true.)
112 @param(APlay Is animation playing? if not then current frame will be displayed.) }
113 constructor Create(const Url: String;
114 const AFrames, AColumns, ARows: Cardinal;
115 const ASmoothScaling: Boolean = True;
116 const ATimeLoop: Boolean = True; const APlay: Boolean = False);
117
118 { Constructor that takes explicit frame size, and loaded TDrawableImage instance.
119
120 @param(AImage Source image (will be owned by this sprite).)
121 @param(AFrames Total numer of animation frames.)
122 @param(AColumns Number of columns.)
123 @param(AFrameWidth Width of each frame.)
124 @param(AFrameHeight Height of each frame.)
125 @param(ATimeLoop Should animation be repeated?, default @true.)
126 @param(APlay Is animation playing? if not then current frame will be displayed.) }
127 constructor CreateFrameSize(const AImage: TDrawableImage;
128 const AFrames, AColumns, AFrameWidth, AFrameHeight: Cardinal;
129 const ATimeLoop: Boolean = True; const APlay: Boolean = False); overload;
130
131 { Constructor that takes explicit frame size.
132
133 @param(Url URL of source image.)
134 @param(AFrames Total numer of animation frames.)
135 @param(AColumns Number of columns.)
136 @param(AFrameWidth Width of each frame.)
137 @param(AFrameHeight Height of each frame.)
138 @param(ASmoothscaling Enables interpolation, see @link(TDrawableImage.SmoothScaling) for details, default @True.)
139 @param(ATimeLoop Should animation be repeated?, default True.)
140 @param(APlay Is animation playing? if not then current frame will be displayed.) }
141 constructor CreateFrameSize(const Url: String;
142 const AFrames, AColumns, AFrameWidth, AFrameHeight: Cardinal;
143 const ASmoothScaling: Boolean = True;
144 const ATimeLoop: Boolean = True; const APlay: Boolean = False); overload;
145
146 { By default this class "owns" the underlying image, even when passed explicitly to
147 CreateFrameSize as TDrawableImage instance.
148 That is, the underlying TDrawableImage instance will be automatically freed when
149 the sprite will be freed.
150 You turn it off with this property. }
151 property OwnsImage: Boolean read FOwnsImage write FOwnsImage default true;
152
153 destructor Destroy; override;
154
155 { Update current frame.
156 @param(SecondsPassed Time from previous update.) }
157 procedure Update(const SecondsPassed: TFloatTime);
158
159 { Play playing animation.,
160 When playing, the @link(Time) will move forward when you call @link(Update). }
161 procedure Play;
162
163 { Stop playing animation.
164 When stopped, the @link(Time) will not move forward, regardless
165 if you call @link(Update). }
166 procedure Stop;
167
168 // For Delphi, deprecated on method is not allowed if we want to later deprecate a class
169 procedure Pause; {$ifdef FPC}deprecated 'use Stop';{$endif}
170
171 { Draw the sprite.
172
173 The overloaded version without AX, AY or ScreenRectangle parameters uses
174 the last position set by @link(Move) method. This is the position
175 of the bottom-left frame corner on screen.
176
177 The overloaded version without DrawWidth, DrawHeight or ScreenRectangle
178 parameters uses the last size set by @link(DrawingWidth), @link(DrawingHeight)
179 properties. This is the size of the frame on screen.
180
181 All versions use the rotation set by the last @link(Move) method.
182 This is the rotation of the frame on screen.
183
184 The overloaded versions deliberately look and work
185 similar to @link(TDrawableImage.Draw) versions.
186 @groupBegin }
187 procedure Draw; overload;
188 procedure Draw(const AX, AY: Single); overload;
189 procedure Draw(const AX, AY, DrawWidth, DrawHeight: Single); overload;
190 procedure Draw(const ScreenRectangle: TRectangle); overload;
191 procedure Draw(const ScreenRectangle: TFloatRectangle); overload;
192 { @groupEnd }
193
194 { Draw the sprite, optionally flipped horizontally and/or vertically.
195 These methods ignore the @link(HorizontalFlip), @link(VerticalFlip)
196 -- which axis is flipped (if any) depends only on the parameters to these methods.
197 @groupBegin }
198 procedure DrawFlipped(const ScreenRect: TRectangle;
199 const FlipHorizontal, FlipVertical: Boolean); overload;
200 procedure DrawFlipped(const ScreenRect: TFloatRectangle;
201 const FlipHorizontal, FlipVertical: Boolean); overload;
202 { @groupEnd }
203
204 { Move sprite to position and rotation. }
205 procedure Move(AX, AY: Single; ARot: Single=0);
206 public
207 property X: Single read FX write FX;
208 property Y: Single read FY write FY;
209 property Position: TVector2 read GetPosition write SetPosition;
210
211 {$ifdef FPC}
212 { Center X of rotation. Values between 0 and 1. }
213 property CenterX: Single read GetCenterX write SetCenterX default 0.5;
214 deprecated 'use RotationCenter';
215
216 { Center Y of rotation. Values between 0 and 1. }
217 property CenterY: Single read GetCenterY write SetCenterY default 0.5;
218 deprecated 'use RotationCenter';
219 {$endif}
220
221 { Destination frame width to draw. }
222 property DrawingWidth: Single read FDrawingWidth write FDrawingWidth;
223
224 { Destination frame height to draw. }
225 property DrawingHeight: Single read FDrawingHeight write FDrawingHeight;
226
227 { Drawing rectangle. This is just a shortcut to get / set
228 properties @link(X), @link(Y), DrawingWidth, DrawingHeight. }
229 property DrawRect: TFloatRectangle read GetDrawRect write SetDrawRect;
230
231 { Center of rotation.
232 Expressed as a fraction within the drawn ScreenRectangle,
233 (0,0) means bottom-left corner, (1,1) means top-right corner.
234 Default (0.5,0.5). }
235 property RotationCenter: TVector2 read GetRotationCenter write SetRotationCenter;
236
237 {$ifdef FPC}
238 property Center: TVector2 read GetRotationCenter write SetRotationCenter;
239 deprecated 'use RotationCenter';
240 {$endif}
241
242 { Rotation in degrees. }
243 property Rotation: Single read GetRotation write SetRotation {$ifdef FPC}default 0{$endif};
244
245 { Color multiplier of the sprite, see @link(TDrawableImage.Color) }
246 property Color: TCastleColor read GetColor write SetColor;
247
248 { Total number of frames. Readonly. Defined at creation. }
249 property Frames: Cardinal read FFrames;
250
251 { How many frames per second to play.
252 This is used only for the default sprite animation (spanning all frames).
253 For the custom animations (added by AddAnimation), each frame
254 has an explicit (possibly different) duration. }
255 property FramesPerSecond: Single
256 read FFramesPerSecond write SetFramesPerSecond
257 {$ifdef FPC}default DefaultFramesPerSecond{$endif};
258
259 { Current frame. }
260 property Frame: Cardinal read FFrame write SetFrame;
261
262 { Does the animation proceeds forward when you call @link(Update) method. }
263 property Playing: Boolean read FPlaying write FPlaying default False;
264
265 { Does the animation loop, that is display the same animation over and over
266 when time exceeded the animation duration. }
267 property TimeLoop: Boolean read FTimeLoop write FTimeLoop;
268
269 { Current time within the animation, it determines the current frame.
270 Yoy can set this explicity. Alternatively, you can call the @link(Update)
271 method continuously while the animation is @link(Playing), and then
272 the time will increase automatically. }
273 property Time: TFloatTime read FTime write SetTime;
274
275 { Width of a single frame. Initial value is set by the constructor. }
276 property FrameWidth: Cardinal read FFrameWidth write FFrameWidth;
277
278 { Height of a single frame. Initial value is set by the constructor. }
279 property FrameHeight: Cardinal read FFrameHeight write FFrameHeight;
280
281 { X margin for frame position on the underlying image.
282 Useful if the first frame doesn't start at X=0. }
283 property LeftMargin: Cardinal read FLeftMargin write FLeftMargin default 0;
284 {$ifdef FPC}property LeftMarginOffset: Cardinal read FLeftMargin write FLeftMargin default 0; deprecated 'use LeftMargin';{$endif}
285
286 { Y margin for frame position on the underlying image.
287 Useful if the first frame doesn't start at Y=0. }
288 property TopMargin: Cardinal read FTopMargin write FTopMargin default 0;
289 {$ifdef FPC}property TopMarginOffset: Cardinal read FTopMargin write FTopMargin default 0; deprecated 'use TopMargin';{$endif}
290
291 { Horizontal spacing between frames.
292 Use this if you have an empty space at the @bold(right) of every frame
293 in your spritesheet. This is useful to avoid frames "bleeding"
294 into each other (due to smooth scaling). }
295 property HorizontalSpacing: Cardinal
296 read FHorizontalSpacing write FHorizontalSpacing default 0;
297
298 { Vertical spacing between frames.
299 Use this if you have an empty space at the @bold(top)
300 (or @bold(bottom), if @link(VerticalSpacingBottom))
301 of every frame in your spritesheet.
302 This is useful to avoid frames "bleeding"
303 into each other (due to smooth scaling). }
304 property VerticalSpacing: Cardinal
305 read FVerticalSpacing write FVerticalSpacing default 0;
306
307 { When @true, the @link(VerticalSpacing) is assumed to be at the bottom
308 of every frame, not top. }
309 property VerticalSpacingBottom: boolean
310 read FVerticalSpacingBottom write FVerticalSpacingBottom default false;
311
312 { Should we play the animation backwards after playing it forward.
313 See TVideo.TimeBackwards. }
314 property TimeBackwards: Boolean read FTimeBackwards write FTimeBackwards default False;
315
316 { Currently used animation.
317 Equal to -1 when we're using the default animation
318 spanning all sprite frames.
319 Otherwise, indicates a custom animation index
320 (added by AddAnimation and used by SwitchToAnimation).
321
322 Call SwitchToAnimation to change this. }
323 property CurrentAnimation: Integer read FCurrentAnimation default -1;
324
325 { Add a custom animation (using an explicit sequence of frames),
326 that can be later used by SwitchToAnimation.
327 @param(AAnimation The animation information. The animation instance
328 (TSpriteAnimation) becomes owned by this object, so don't free
329 it yourself.)
330 @param(AnimationFrames The animation information can also be given
331 as a simple list of frame indexes. In this case,
332 all frames are assumed to take the same time: @code(1 / FramesPerSecond).)
333 @returns(The animation index, that can be used with SwitchToAnimation.)
334 @groupBegin }
335 function AddAnimation(const AAnimation: TSpriteAnimation): Integer; overload;
336 function AddAnimation(const AnimationFrames: array of Cardinal): Integer; overload;
337 function AddSpriteAnimation(const AnimationFrames: array of TSpriteAnimationFrame): Integer;
338 // For Delphi, deprecated on method is not allowed if we want to later deprecate a class
339 {$ifdef FPC}deprecated 'use AddAnimation';{$endif}
340 { @groupEnd }
341
342 { Switch to the animation with index AAnimIndex.
343 Use animation index obtained from AddAnimation to use a custom animation,
344 or use -1 to use the default animation (spanning all sprite frames).
345
346 @returns(@true on success, @false if AAnimIndex is out of bounds.) }
347 function SwitchToAnimation(const AAnimIndex: Integer; const ACustomFrame: Cardinal = 0): Boolean;
348
349 { Duration, in seconds, of the currently used animation.
350 See @link(CurrentAnimation) to know what the current animation is. }
351 function Duration: TFloatTime;
352
353 { Is sprite horizontal flipped? }
354 property HorizontalFlip: Boolean read FHorizontalFlip write FHorizontalFlip default False;
355
356 { Is sprite vertical flipped? }
357 property VerticalFlip: Boolean read FVerticalFlip write FVerticalFlip default False;
358
359 { Use smooth "bilinear" filtering when this is scaled. }
360 property SmoothScaling: boolean read FSmoothScaling write SetSmoothScaling;
361
362 { Prevent the color outside of rendered frame from creeping in,
363 by scaling image a bit to the inside.
364 Because of bilinear filtering (used when @link(SmoothScaling)),
365 the colors of neighboring pixels may be visible when you render
366 the sprite frame, and the sprite is significantly scaled.
367 The "neighboring pixels" may mean
368 "pixels from the adjacent sprite frame",
369 or sprite border (in case sprite frame is at the edge of the image),
370 or spacing between sprite frames (if any).
371
372 One way to workaround this is to add a 1-pixel or 2-pixel
373 spacing between each sprite frames, and fill this spacing
374 with the same color (including alpha) as the neighbor pixels.
375
376 Another workaround is to use this property.
377 This renders a slightly smaller sprite frame, such that bilinear
378 filtering doesn't have a chance to introduce colors from outside
379 of the desired sprite frame.
380 This is done only when @link(SmoothScaling), otherwise this property
381 is ignored.
382
383 The downside is that everything is slightly scaled,
384 and doesn't look perfect if you look closely at each pixel.
385 This property is ignored }
386 property SmoothScalingSafeBorder: Boolean
387 read FSmoothScalingSafeBorder write FSmoothScalingSafeBorder
388 default false;
389
390 end deprecated 'load sprite sheets to TCastleScene from .castle-sprite-sheet format';
391
392 { Unfortunately deprecating TSprite makes unavoidable warnings from TSpriteList
393 specialization, and we cannot hide them putting $warnings off/on around it.
394
395 It has been documented as deprecated for a long time now,
396 we can remove it now.
397 }
398 { List of sprites. }
399 //TSpriteList = {$ifdef FPC}specialize{$endif} TObjectList<TSprite>;
400
401 {$endif read_interface_type}
402
403 {$endif read_interface}
404
405 {$ifdef read_implementation}
406
407 { TSpriteAnimation ----------------------------------------------------------- }
408
409 constructor TSpriteAnimation.CreateConstantFps(const AFrames: array of Cardinal; const FramesPerSecond: Single);
410 var
411 I: Integer;
412 FrameDuration: Single;
413 begin
414 inherited Create;
415 FrameDuration := 1 / FramesPerSecond;
416 { calculate Frames }
417 SetLength(Frames, High(AFrames) + 1);
418 for I := 0 to High(AFrames) do
419 begin
420 Frames[I].Frame := AFrames[I];
421 Frames[I].Duration := FrameDuration;
422 end;
423 { calculate FDuration }
424 FDuration := FrameDuration * Length(AFrames);
425 end;
426
427 constructor TSpriteAnimation.Create(const AFrames: array of TSpriteAnimationFrame);
428 var
429 I: Integer;
430 begin
431 inherited Create;
432 { calculate Frames and FDuration }
433 FDuration := 0;
434 SetLength(Frames, High(AFrames) + 1);
435 for I := 0 to High(AFrames) do
436 begin
437 Frames[I] := AFrames[I];
438 FDuration := FDuration + Frames[I].Duration;
439 end;
440 end;
441
442 { TSprite -------------------------------------------------------------------- }
443
444 function TSprite.GetPosition: TVector2;
445 begin
446 Result.X := FX;
447 Result.Y := FY;
448 end;
449
450 function TSprite.GetRotation: Single;
451 begin
452 Result := FImage.Rotation;
453 end;
454
455 function TSprite.GetColor: TCastleColor;
456 begin
457 Result := FImage.Color;
458 end;
459
460 procedure TSprite.SetTime(const AValue: TFloatTime);
461 var
462 Anim: TSpriteAnimation;
463 begin
464 if FTime = AValue then Exit;
465 FTime := AValue;
466 if FCurrentAnimation <> -1 then
467 begin
468 Assert(FCurrentAnimation < FCustomAnimations.Count);
469 Anim := FCustomAnimations.Items[FCurrentAnimation];
470 FFrame := Anim.Frames[FCustomFrame].Frame;
471 while (FTime - FCustomAnimTimestamp) >= Anim.Frames[FCustomFrame].Duration do
472 begin
473 // todo: custom animation loops
474 if FCustomFrame = High(Anim.Frames) then
475 begin
476 if FTimeLoop then
477 FCustomFrame := 0
478 else
479 Stop;
480 end else
481 Inc(FCustomFrame);
482 FCustomAnimTimestamp := FCustomAnimTimestamp + Anim.Frames[FCustomFrame].Duration;
483 FFrame := Anim.Frames[FCustomFrame].Frame;
484 end;
485 end else
486 FFrame := TVideo.FrameIndexFromTime(FTime, FFrames, FFramesPerSecond, FTimeLoop, FTimeBackwards);
487 end;
488
489 function TSprite.GetFrameRect: TFloatRectangle;
490 var
491 FullFrameWidth, FullFrameHeight: Cardinal;
492 begin
493 FullFrameWidth := FFrameWidth + FHorizontalSpacing;
494 FullFrameHeight := FFrameHeight + FVerticalSpacing;
495 Result.Left :=
496 FFrame mod FColumns * FullFrameWidth + FLeftMargin;
497 Result.Bottom := FImage.Height -
498 (FFrame div FColumns + 1) * FullFrameHeight - FTopMargin;
499 if VerticalSpacingBottom then
500 Result.Bottom := Result.Bottom + FVerticalSpacing;
501 Result.Width := FFrameWidth;
502 Result.Height := FFrameHeight;
503 end;
504
505 function TSprite.GetDrawRect: TFloatRectangle;
506 begin
507 Result.Left := FX;
508 Result.Bottom := FY;
509 Result.Width := FDrawingWidth;
510 Result.Height := FDrawingHeight;
511 end;
512
513 procedure TSprite.SetDrawRect(const AValue: TFloatRectangle);
514 begin
515 FX := AValue.Left;
516 FY := AValue.Bottom;
517 FDrawingWidth := AValue.Width;
518 FDrawingHeight := AValue.Height;
519 end;
520
521 procedure TSprite.SetFramesPerSecond(const AValue: Single);
522 begin
523 if FFramesPerSecond = AValue then Exit;
524 FFramesPerSecond := AValue;
525 end;
526
527 procedure TSprite.SetFrame(const AValue: Cardinal);
528 begin
529 if (FFrame = AValue) or (AValue >= FFrames) then Exit;
530 FFrame := AValue;
531 end;
532
533 procedure TSprite.SetPosition(const APosition: TVector2);
534 begin
535 FX := APosition[0];
536 FY := APosition[1];
537 end;
538
539 procedure TSprite.SetRotation(const AValue: Single);
540 begin
541 FImage.Rotation := AValue;
542 end;
543
544 procedure TSprite.SetColor(const AValue: TCastleColor);
545 begin
546 FImage.Color := AValue;
547 end;
548
549 constructor TSprite.CreateFrameSize(const AImage: TDrawableImage;
550 const AFrames, AColumns, AFrameWidth, AFrameHeight: Cardinal;
551 const ATimeLoop: Boolean = True; const APlay: Boolean = False);
552 begin
553 inherited Create;
554 FImage := AImage;
555 FFrames := AFrames;
556 FColumns := AColumns;
557 FFrameWidth := AFrameWidth;
558 FFrameHeight := AFrameHeight;
559 FDrawingWidth := FFrameWidth;
560 FDrawingHeight := FFrameHeight;
561 FPlaying := APlay;
562 FFrame := 0;
563 FTime := 0;
564 SetFramesPerSecond(DefaultFramesPerSecond);
565 FTimeLoop := ATimeLoop;
566 FLeftMargin := 0;
567 FTopMargin := 0;
568 FTimeBackwards := False;
569 FCurrentAnimation := -1;
570 FCustomAnimations := TSpriteAnimations.Create(true);
571 FCustomFrame := 0;
572 FHorizontalFlip := False;
573 FVerticalFlip := False;
574 FOwnsImage := true;
575 end;
576
577 constructor TSprite.CreateFrameSize(const Url: String;
578 const AFrames, AColumns, AFrameWidth, AFrameHeight: Cardinal;
579 const ASmoothScaling: Boolean = True;
580 const ATimeLoop: Boolean = True; const APlay: Boolean = False);
581 var
582 Img: TDrawableImage;
583 begin
584 FSmoothScaling := ASmoothScaling;
585 Img := TDrawableImage.Create(Url, SmoothScaling);
586 CreateFrameSize(Img, AFrames, AColumns, AFrameWidth, AFrameHeight,
587 ATimeLoop, APlay);
588 end;
589
590 constructor TSprite.Create(const Url: String;
591 const AFrames, AColumns, ARows: Cardinal;
592 const ASmoothScaling: Boolean = True;
593 const ATimeLoop: Boolean = True; const APlay: Boolean = False);
594 var
595 Img: TDrawableImage;
596 begin
597 FSmoothScaling := ASmoothScaling;
598 Img := TDrawableImage.Create(Url, SmoothScaling);
599 CreateFrameSize(Img, AFrames, AColumns,
600 { Note that ARows is not remembered in this instance.
601 It's only used to calculate default AFrameHeight below, which can
602 be changed later. }
603 { AFrameWidth } Img.Width div AColumns,
604 { AFrameHeight } Img.Height div ARows,
605 ATimeLoop, APlay);
606 end;
607
608 destructor TSprite.Destroy;
609 begin
610 if OwnsImage then
611 FreeAndNil(FImage);
612 FreeAndNil(FCustomAnimations);
613 inherited;
614 end;
615
616 procedure TSprite.Update(const SecondsPassed: TFloatTime);
617 begin
618 if not FPlaying then Exit;
619 Time := Time + SecondsPassed;
620 end;
621
622 procedure TSprite.Play;
623 begin
624 FPlaying := True;
625 end;
626
627 procedure TSprite.Stop;
628 begin
629 FPlaying := False;
630 end;
631
632 procedure TSprite.Pause;
633 begin
634 Stop;
635 end;
636
637 procedure TSprite.Draw;
638 begin
639 Draw(GetDrawRect);
640 end;
641
642 procedure TSprite.Draw(const AX, AY: Single);
643 begin
644 Draw(FloatRectangle(AX, AY, DrawingWidth, DrawingHeight));
645 end;
646
647 procedure TSprite.Draw(const AX, AY, DrawWidth, DrawHeight: Single);
648 begin
649 Draw(FloatRectangle(AX, AY, DrawWidth, DrawHeight));
650 end;
651
652 procedure TSprite.Draw(const ScreenRectangle: TRectangle);
653 begin
654 Draw(FloatRectangle(ScreenRectangle));
655 end;
656
657 procedure TSprite.Draw(const ScreenRectangle: TFloatRectangle);
658 begin
659 DrawFlipped(ScreenRectangle,
660 HorizontalFlip,
661 VerticalFlip);
662 end;
663
664 procedure TSprite.DrawFlipped(const ScreenRect: TRectangle;
665 const FlipHorizontal, FlipVertical: Boolean);
666 begin
667 DrawFlipped(FloatRectangle(ScreenRect), FlipHorizontal, FlipVertical);
668 end;
669
670 procedure TSprite.DrawFlipped(const ScreenRect: TFloatRectangle;
671 const FlipHorizontal, FlipVertical: Boolean);
672 var
673 F: TFloatRectangle;
674 begin
675 F := GetFrameRect;
676 if SmoothScaling and SmoothScalingSafeBorder then
677 F := F.Grow(-0.51);
678 FImage.DrawFlipped(ScreenRect, F, FlipHorizontal, FlipVertical);
679 end;
680
681 procedure TSprite.Move(AX, AY: Single; ARot: Single);
682 begin
683 FX := AX;
684 FY := AY;
685 Rotation := ARot;
686 end;
687
688 function TSprite.AddAnimation(const AAnimation: TSpriteAnimation): Integer;
689 begin
690 Result := FCustomAnimations.Add(AAnimation);
691 end;
692
693 function TSprite.AddAnimation(
694 const AnimationFrames: array of Cardinal): Integer;
695 begin
696 Result := AddAnimation(
697 TSpriteAnimation.CreateConstantFps(AnimationFrames, FramesPerSecond));
698 end;
699
700 function TSprite.AddSpriteAnimation(
701 const AnimationFrames: array of TSpriteAnimationFrame): Integer;
702 begin
703 Result := AddAnimation(TSpriteAnimation.Create(AnimationFrames));
704 end;
705
706 function TSprite.SwitchToAnimation(const AAnimIndex: Integer;
707 const ACustomFrame: Cardinal): Boolean;
708 begin
709 if (AAnimIndex >= FCustomAnimations.Count) or (AAnimIndex < -1) then
710 begin
711 Result := False;
712 Exit;
713 end;
714 FCurrentAnimation := AAnimIndex;
715 if FCurrentAnimation <> -1 then
716 begin
717 FCustomFrame := Clamped(ACustomFrame, 0, High(FCustomAnimations.Items[AAnimIndex].Frames));
718 end else
719 FCustomFrame := 0; // ACustomFrame, FCustomFrame unused in this case
720 Result := True;
721 end;
722
723 function TSprite.Duration: TFloatTime;
724 begin
725 if FCurrentAnimation <> -1 then
726 Result := FCustomAnimations[FCurrentAnimation].Duration
727 else
728 Result := Frames / FramesPerSecond;
729 end;
730
731 procedure TSprite.SetSmoothScaling(const Value: Boolean);
732 begin
733 if FSmoothScaling <> Value then
734 begin
735 FSmoothScaling := Value;
736 FImage.SmoothScaling := Value;
737 end;
738 end;
739
740 function TSprite.GetRotationCenter: TVector2;
741 begin
742 Result := FImage.RotationCenter;
743 end;
744
745 procedure TSprite.SetRotationCenter(const AValue: TVector2);
746 begin
747 FImage.RotationCenter := AValue;
748 end;
749
750 function TSprite.GetCenterX: Single;
751 begin
752 Result := RotationCenter.X;
753 end;
754
755 function TSprite.GetCenterY: Single;
756 begin
757 Result := RotationCenter.Y;
758 end;
759
760 procedure TSprite.SetCenterX(const AValue: Single);
761 var
762 V: TVector2;
763 begin
764 V := RotationCenter;
765 V.X := AValue;
766 RotationCenter := V;
767 end;
768
769 procedure TSprite.SetCenterY(const AValue: Single);
770 var
771 V: TVector2;
772 begin
773 V := RotationCenter;
774 V.Y := AValue;
775 RotationCenter := V;
776 end;
777
778 {$endif read_implementation}