Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
FrameGameSystem.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 using System.Reflection;
6 using SiliconStudio.Core;
7 using SiliconStudio.Paradox.Games;
8 
9 namespace SiliconStudio.Paradox.Graphics.Regression
10 {
12  {
13  #region Private members
14 
15  /// <summary>
16  /// List of methods to call in the Update method.
17  /// </summary>
18  private readonly List<SetupMethodInfo> updateMethods;
19 
20  /// <summary>
21  /// List of methods to call in the Draw method.
22  /// </summary>
23  private readonly List<SetupMethodInfo> drawMethods;
24 
25  /// <summary>
26  /// The frames to take screenshot of.
27  /// </summary>
28  private readonly HashSet<int> screenshotFrames;
29 
30  /// <summary>
31  /// The current frame.
32  /// </summary>
33  private int frameCount;
34 
35  /// <summary>
36  /// The last screenshot frame.
37  /// </summary>
38  private int lastFrame;
39 
40  #endregion
41 
42  #region Public properties
43 
44  /// <summary>
45  /// Flag stating that all the tests have been rendered.
46  /// </summary>
47  public bool AllTestsCompleted
48  {
49  get
50  {
51  return frameCount > lastFrame;
52  }
53  }
54 
55  /// <summary>
56  /// Flag stating that a screenshot should be taken.
57  /// </summary>
58  public bool TakeSnapshot
59  {
60  get
61  {
62  return screenshotFrames.Contains(frameCount);
63  }
64  }
65 
66  /// <summary>
67  /// The current frame.
68  /// </summary>
69  public int CurrentFrame
70  {
71  get
72  {
73  return frameCount;
74  }
75  }
76 
77  /// <summary>
78  /// The number of frames to render.
79  /// </summary>
80  public int TestCount
81  {
82  get
83  {
84  return screenshotFrames.Count;
85  }
86  }
87 
88  #endregion
89 
90  #region Constructor
91 
93  : base(registry)
94  {
95  updateMethods = new List<SetupMethodInfo>();
96  drawMethods = new List<SetupMethodInfo>();
97  screenshotFrames = new HashSet<int>();
98  lastFrame = -1;
99  }
100 
101  #endregion
102 
103  #region Public methods
104 
105  /// <summary>
106  /// Add a method to call in the update function.
107  /// </summary>
108  /// <param name="frameIndex">The index of the frame.</param>
109  /// <param name="method">The method to call.</param>
110  /// <returns>this.</returns>
111  public FrameGameSystem Update(int frameIndex, Action method)
112  {
113  AddTestMethods(method, frameIndex, updateMethods);
114  return this;
115  }
116 
117  /// <summary>
118  /// Add a method to call in the update function.
119  /// </summary>
120  /// <param name="method">The method to call.</param>
121  /// <returns>this.</returns>
122  public FrameGameSystem Update(Action method)
123  {
124  AddTestMethods(method, lastFrame + 1, updateMethods);
125  return this;
126  }
127 
128  /// <summary>
129  /// Add a method to call in the draw function.
130  /// </summary>
131  /// <param name="frameIndex">The index of the frame.</param>
132  /// <param name="method">The method to call.</param>
133  /// <returns>this.</returns>
134  public FrameGameSystem Draw(int frameIndex, Action method)
135  {
136  AddTestMethods(method, frameIndex, drawMethods);
137  return this;
138  }
139 
140  /// <summary>
141  /// Add a method to call in the draw function.
142  /// </summary>
143  /// <param name="method">The method to call.</param>
144  /// <returns>this.</returns>
145  public FrameGameSystem Draw(Action method)
146  {
147  AddTestMethods(method, lastFrame + 1, drawMethods);
148  return this;
149  }
150 
151  /// <summary>
152  /// Take a screenshot at the desired frame.
153  /// </summary>
154  /// <param name="frameIndex">the frame index.</param>
155  /// <returns>this</returns>
156  public FrameGameSystem TakeScreenshot(int frameIndex)
157  {
158  screenshotFrames.Add(frameIndex);
159  if (frameIndex > lastFrame)
160  lastFrame = frameIndex;
161  return this;
162  }
163 
164  /// <summary>
165  /// Take a screenshot at the desired frame.
166  /// </summary>
167  /// <returns>this</returns>
169  {
170  return TakeScreenshot(lastFrame + 1);
171  }
172 
173  public override void Draw(GameTime gameTime)
174  {
175  base.Draw(gameTime);
176  ExecuteFrameMethod(drawMethods);
177  }
178 
179  public override void Update(GameTime gameTime)
180  {
181  // Update is called twice before the first draw
182  frameCount = gameTime.FrameCount - 1;
183  base.Update(gameTime);
184  ExecuteFrameMethod(updateMethods);
185  }
186 
187  #endregion
188 
189  #region Private methods
190 
191  /// <summary>
192  /// Add method to the list.
193  /// </summary>
194  /// <param name="action">The action to add.</param>
195  /// <param name="frameIndex">The index of the frame.</param>
196  /// <param name="targetList">The list where to add the method.</param>
197  private void AddTestMethods(Action action, int frameIndex, List<SetupMethodInfo> targetList)
198  {
199  SetupMethodInfo smi;
200  smi.Action = action;
201  smi.FrameIndex = frameIndex;
202  targetList.Add(smi);
203  }
204 
205  /// <summary>
206  /// Execute the test method for the current frame.
207  /// </summary>
208  /// <param name="targetList">List of methods.</param>
209  private void ExecuteFrameMethod(List<SetupMethodInfo> targetList)
210  {
211  var methodsToRemove = new Stack<int>();
212  for (var i = 0; i < targetList.Count; ++i)
213  {
214  var method = targetList[i];
215  if (method.FrameIndex == frameCount)
216  {
217  if (method.Action != null)
218  {
219  TestGameBase.TestGameLogger.Debug(@"Executing method in Draw/Update for frame " + frameCount + @": " + method.Action.GetMethodInfo().Name);
220  method.Action.Invoke();
221  }
222  methodsToRemove.Push(i);
223  }
224  }
225 
226  // remove methods so that they are only executed once per frame.
227  while (methodsToRemove.Count > 0)
228  targetList.RemoveAt(methodsToRemove.Pop());
229  }
230 
231  #endregion
232 
233  #region Helper structures
234 
235  /// <summary>
236  /// Structure to store the information of the method to run before any test.
237  /// </summary>
238  private struct SetupMethodInfo
239  {
240  public Action Action;
241 
242  public int FrameIndex;
243  }
244 
245  #endregion
246  }
247 }
override void Draw(GameTime gameTime)
Draws this instance.
FrameGameSystem TakeScreenshot()
Take a screenshot at the desired frame.
A service registry is a IServiceProvider that provides methods to register and unregister services...
FrameGameSystem Draw(int frameIndex, Action method)
Add a method to call in the draw function.
FrameGameSystem Draw(Action method)
Add a method to call in the draw function.
Base class for a GameSystemBase component.
Current timing used for variable-step (real time) or fixed-step (game time) games.
Definition: GameTime.cs:31
FrameGameSystem Update(Action method)
Add a method to call in the update function.
FrameGameSystem TakeScreenshot(int frameIndex)
Take a screenshot at the desired frame.
FrameGameSystem Update(int frameIndex, Action method)
Add a method to call in the update function.
override void Update(GameTime gameTime)
This method is called when this game component is updated.