Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SpriteComponent.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Core.Serialization;
9 using SiliconStudio.Core.Serialization.Converters;
10 using SiliconStudio.Core.Serialization.Serializers;
11 using SiliconStudio.Paradox.EntityModel;
12 using SiliconStudio.Paradox.Graphics;
13 
14 namespace SiliconStudio.Paradox.Engine
15 {
16  /// <summary>
17  /// Add a <see cref="Sprite"/> to an <see cref="Entity"/>. It could be an animated sprite.
18  /// </summary>
19  [DataConverter(AutoGenerate = true)]
20  [DataContract("SpriteComponent")]
21  public sealed class SpriteComponent : EntityComponent
22  {
24 
25  /// <summary>
26  /// The sprites to play.
27  /// </summary>
28  [DataMemberConvert]
29  internal SpriteGroup SpriteGroupInternal;
30 
31  /// <summary>
32  /// The color to apply on the sprite.
33  /// </summary>
34  [DataMemberConvert]
35  public Color Color = Color.White;
36 
37  /// <summary>
38  /// The effect to apply on the sprite.
39  /// </summary>
40  [DataMemberConvert]
42 
43  /// <summary>
44  /// The effect to use to render the sprite.
45  /// </summary>
46  [DataMemberCustomSerializer]
47  public Effect Effect;
48 
49  internal double ElapsedTime;
50 
51  private int currentFrame;
52 
53  /// <summary>
54  /// The group of sprites associated to the component.
55  /// </summary>
56  [DataMemberCustomSerializer]
58  {
59  get { return SpriteGroupInternal; }
60  set
61  {
62  if(SpriteGroupInternal == value)
63  return;
64 
65  SpriteGroupInternal = value;
66  CurrentFrame = 0;
67  }
68  }
69 
70  /// <summary>
71  /// Gets or sets the current frame of the animation.
72  /// </summary>
73  public int CurrentFrame
74  {
75  get { return currentFrame; }
76  set
77  {
78  if (SpriteGroupInternal == null || SpriteGroupInternal.Images == null || SpriteGroupInternal.Images.Count == 0)
79  {
80  currentFrame = 0;
81  return;
82  }
83 
84  currentFrame = Math.Max(0, value % SpriteGroupInternal.Images.Count);
85  }
86  }
87 
88  /// <summary>
89  /// Gets the current sprite.
90  /// </summary>
91  [DataMemberIgnore]
92  public Sprite CurrentSprite
93  {
94  get
95  {
96  if (SpriteGroupInternal == null || SpriteGroupInternal.Images == null)
97  return null;
98 
99  return SpriteGroupInternal.Images[Math.Min(currentFrame, SpriteGroupInternal.Images.Count)];
100  }
101  }
102 
103  private readonly static Queue<List<int>> SpriteIndicesPool = new Queue<List<int>>();
104 
105  internal double AnimationTime;
106 
107  internal int CurrentIndexIndex;
108 
109  internal bool IsPaused;
110 
111  internal struct AnimationInfo
112  {
113  public float FramePerSeconds;
114 
115  public bool ShouldLoop;
116 
117  public List<int> SpriteIndices;
118  }
119 
120  internal Queue<AnimationInfo> Animations = new Queue<AnimationInfo>();
121 
122  internal static List<int> GetNewSpriteIndicesList()
123  {
124  lock (SpriteIndicesPool)
125  {
126  return SpriteIndicesPool.Count > 0 ? SpriteIndicesPool.Dequeue() : new List<int>();
127  }
128  }
129 
130  internal static void RecycleSpriteIndicesList(List<int> indicesList)
131  {
132  lock (SpriteIndicesPool)
133  {
134  indicesList.Clear();
135  SpriteIndicesPool.Enqueue(indicesList);
136  }
137  }
138 
139  internal void ClearAnimations()
140  {
141  lock (SpriteIndicesPool)
142  {
143  while (Animations.Count>0)
144  RecycleSpriteIndicesList(Animations.Dequeue().SpriteIndices);
145  }
146  }
147 
148  internal void RecycleFirstAnimation()
149  {
150  CurrentIndexIndex = 0;
151  if (Animations.Count > 0)
152  {
153  var info = Animations.Dequeue();
154  RecycleSpriteIndicesList(info.SpriteIndices);
155  }
156  }
157 
158  public override PropertyKey DefaultKey
159  {
160  get { return Key; }
161  }
162  }
163 }
Base class for converters to/from a data type.
SpriteEffects
Defines sprite mirroring options.
SpriteEffects SpriteEffect
The effect to apply on the sprite.
Add a Sprite to an Entity. It could be an animated sprite.
Represent of group of Sprite
Definition: SpriteGroup.cs:12
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Definition: Color.cs:16
Effect Effect
The effect to use to render the sprite.
A class that represents a tag propety.
Definition: PropertyKey.cs:17
A sprite represents a series frames in an atlas forming an animation.
Definition: Sprite.cs:14