Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameSystemBase.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 //
4 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 
24 using System;
25 
26 using SiliconStudio.Paradox.Graphics;
27 using SiliconStudio.Core;
28 using SiliconStudio.Core.Serialization.Assets;
29 
30 namespace SiliconStudio.Paradox.Games
31 {
32  /// <summary>
33  /// Base class for a <see cref="GameSystemBase"/> component.
34  /// </summary>
35  /// <remarks>
36  /// A <see cref="GameSystemBase"/> component can be used to
37  /// </remarks>
39  {
40  private readonly IServiceRegistry registry;
41  private int drawOrder;
42  private bool enabled;
43  private readonly GameBase game;
44  private int updateOrder;
45  private bool visible;
46  private readonly IAssetManager assetManager;
47  private IGraphicsDeviceService graphicsDeviceService;
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="GameSystemBase" /> class.
51  /// </summary>
52  /// <param name="registry">The registry.</param>
53  /// <remarks>
54  /// The GameSystem is expecting the following services to be registered: <see cref="IGame"/> and <see cref="IAssetManager"/>.
55  /// </remarks>
56  protected GameSystemBase(IServiceRegistry registry)
57  {
58  if (registry == null) throw new ArgumentNullException("registry");
59  this.registry = registry;
60  game = (GameBase)Services.GetSafeServiceAs<IGame>();
61  assetManager = Services.GetSafeServiceAs<IAssetManager>();
62  }
63 
64  /// <summary>
65  /// Gets the <see cref="Game"/> associated with this <see cref="GameSystemBase"/>. This value can be null in a mock environment.
66  /// </summary>
67  /// <value>The game.</value>
68  public GameBase Game
69  {
70  get { return game; }
71  }
72 
73  /// <summary>
74  /// Gets the services registry.
75  /// </summary>
76  /// <value>The services registry.</value>
77  public IServiceRegistry Services
78  {
79  get
80  {
81  return registry;
82  }
83  }
84 
85  /// <summary>
86  /// Gets the content manager.
87  /// </summary>
88  /// <value>The content.</value>
89  protected IAssetManager Asset
90  {
91  get
92  {
93  return assetManager;
94  }
95  }
96 
97  /// <summary>
98  /// Gets the graphics device.
99  /// </summary>
100  /// <value>The graphics device.</value>
102  {
103  get
104  {
105  return graphicsDeviceService != null ? graphicsDeviceService.GraphicsDevice : null;
106  }
107  }
108 
109  #region IDrawable Members
110 
111  public event EventHandler<EventArgs> DrawOrderChanged;
112 
113  public event EventHandler<EventArgs> VisibleChanged;
114 
115  public virtual bool BeginDraw()
116  {
117  return true;
118  }
119 
120  public virtual void Draw(GameTime gameTime)
121  {
122  }
123 
124  public virtual void EndDraw()
125  {
126  }
127 
128  public bool Visible
129  {
130  get { return visible; }
131  set
132  {
133  if (visible != value)
134  {
135  visible = value;
136  OnVisibleChanged(EventArgs.Empty);
137  }
138  }
139  }
140 
141  public int DrawOrder
142  {
143  get { return drawOrder; }
144  set
145  {
146  if (drawOrder != value)
147  {
148  drawOrder = value;
149  OnDrawOrderChanged(this, EventArgs.Empty);
150  }
151  }
152  }
153 
154  #endregion
155 
156  #region IGameSystemBase Members
157 
158  public virtual void Initialize()
159  {
160  // Gets the graphics device service
161  graphicsDeviceService = (IGraphicsDeviceService)registry.GetService(typeof(IGraphicsDeviceService));
162  }
163 
164  #endregion
165 
166  #region IUpdateable Members
167 
168  public event EventHandler<EventArgs> EnabledChanged;
169 
170  public event EventHandler<EventArgs> UpdateOrderChanged;
171 
172  public virtual void Update(GameTime gameTime)
173  {
174  }
175 
176  public bool Enabled
177  {
178  get { return enabled; }
179  set
180  {
181  if (enabled != value)
182  {
183  enabled = value;
184  OnEnabledChanged(EventArgs.Empty);
185  }
186  }
187  }
188 
189  public int UpdateOrder
190  {
191  get { return updateOrder; }
192  set
193  {
194  if (updateOrder != value)
195  {
196  updateOrder = value;
197  OnUpdateOrderChanged(this, EventArgs.Empty);
198  }
199  }
200  }
201 
202  #endregion
203 
204  protected virtual void OnDrawOrderChanged(object source, EventArgs e)
205  {
206  EventHandler<EventArgs> handler = DrawOrderChanged;
207  if (handler != null) handler(source, e);
208  }
209 
210  private void OnVisibleChanged(EventArgs e)
211  {
212  EventHandler<EventArgs> handler = VisibleChanged;
213  if (handler != null) handler(this, e);
214  }
215 
216  private void OnEnabledChanged(EventArgs e)
217  {
218  EventHandler<EventArgs> handler = EnabledChanged;
219  if (handler != null) handler(this, e);
220  }
221 
222  protected virtual void OnUpdateOrderChanged(object source, EventArgs e)
223  {
224  EventHandler<EventArgs> handler = UpdateOrderChanged;
225  if (handler != null) handler(source, e);
226  }
227 
228  #region Implementation of IContentable
229 
230  void IContentable.LoadContent()
231  {
232  LoadContent();
233  }
234 
235  void IContentable.UnloadContent()
236  {
237  UnloadContent();
238  }
239 
240  protected virtual void LoadContent()
241  {
242  }
243 
244  protected virtual void UnloadContent()
245  {
246  }
247 
248  #endregion
249  }
250 }
251 
GameSystemBase(IServiceRegistry registry)
Initializes a new instance of the GameSystemBase class.
Service providing method to access GraphicsDevice life-cycle.
An interface that is called by GameBase.Update.
Definition: IUpdateable.cs:30
virtual void Draw(GameTime gameTime)
Draws this instance.
An interface to load and unload asset.
Definition: IContentable.cs:29
EventHandler< EventArgs > DrawOrderChanged
virtual bool BeginDraw()
Starts the drawing of a frame. This method is followed by calls to Draw and EndDraw.
A service registry is a IServiceProvider that provides methods to register and unregister services...
Base class for a framework component.
Main Game class system.
Definition: Game.cs:32
Defines a generic game system.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
Base class for a GameSystemBase component.
virtual void Initialize()
This method is called when the component is added to the game.
virtual void Update(GameTime gameTime)
This method is called when this game component is updated.
Current timing used for variable-step (real time) or fixed-step (game time) games.
Definition: GameTime.cs:31
virtual void OnUpdateOrderChanged(object source, EventArgs e)
virtual void UnloadContent()
Called when graphics resources need to be unloaded. Override this method to unload any game-specific ...
virtual void EndDraw()
Ends the drawing of a frame. This method is preceeded by calls to Draw and BeginDraw.
An interface for a drawable game component that is called by the GameBase class.
Definition: IDrawable.cs:30
virtual void LoadContent()
Loads the assets.
EventHandler< EventArgs > VisibleChanged
virtual void OnDrawOrderChanged(object source, EventArgs e)
EventHandler< EventArgs > EnabledChanged
EventHandler< EventArgs > UpdateOrderChanged