Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameWindowRenderer.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.Core;
27 using SiliconStudio.Paradox.Graphics;
28 using SiliconStudio.Core.Mathematics;
29 
30 namespace SiliconStudio.Paradox.Games
31 {
32  /// <summary>
33  /// A GameSystem that allows to draw to another window or control. Currently only valid on desktop with Windows.Forms.
34  /// </summary>
36  {
37  private PixelFormat preferredBackBufferFormat;
38  private int preferredBackBufferHeight;
39  private int preferredBackBufferWidth;
40  private PixelFormat preferredDepthStencilFormat;
41  private bool isBackBufferToResize;
42  private GraphicsPresenter savedPresenter;
43  private Viewport savedViewport;
44  private bool beginDrawOk;
45  private bool windowUserResized;
46 
47  /// <summary>
48  /// Initializes a new instance of the <see cref="GameWindowRenderer" /> class.
49  /// </summary>
50  /// <param name="registry">The registry.</param>
51  /// <param name="gameContext">The window context.</param>
52  public GameWindowRenderer(IServiceRegistry registry, GameContext gameContext = null)
53  : base(registry)
54  {
55  GameContext = gameContext ?? new GameContext();
56  }
57 
58  /// <summary>
59  /// Gets the underlying native window.
60  /// </summary>
61  /// <value>The underlying native window.</value>
62  public GameContext GameContext { get; private set; }
63 
64  /// <summary>
65  /// Gets the window.
66  /// </summary>
67  /// <value>The window.</value>
68  public GameWindow Window { get; private set; }
69 
70  /// <summary>
71  /// Gets or sets the presenter.
72  /// </summary>
73  /// <value>The presenter.</value>
74  public GraphicsPresenter Presenter { get; protected set; }
75 
76  /// <summary>
77  /// Gets or sets the preferred back buffer format.
78  /// </summary>
79  /// <value>The preferred back buffer format.</value>
80  public PixelFormat PreferredBackBufferFormat
81  {
82  get
83  {
84  return preferredBackBufferFormat;
85  }
86 
87  set
88  {
89  if (preferredBackBufferFormat != value)
90  {
91  preferredBackBufferFormat = value;
92  isBackBufferToResize = true;
93  }
94  }
95  }
96 
97  /// <summary>
98  /// Gets or sets the height of the preferred back buffer.
99  /// </summary>
100  /// <value>The height of the preferred back buffer.</value>
101  public int PreferredBackBufferHeight
102  {
103  get
104  {
105  return preferredBackBufferHeight;
106  }
107 
108  set
109  {
110  if (preferredBackBufferHeight != value)
111  {
112  preferredBackBufferHeight = value;
113  isBackBufferToResize = true;
114  }
115  }
116  }
117 
118  /// <summary>
119  /// Gets or sets the width of the preferred back buffer.
120  /// </summary>
121  /// <value>The width of the preferred back buffer.</value>
122  public int PreferredBackBufferWidth
123  {
124  get
125  {
126  return preferredBackBufferWidth;
127  }
128 
129  set
130  {
131  if (preferredBackBufferWidth != value)
132  {
133  preferredBackBufferWidth = value;
134  isBackBufferToResize = true;
135  }
136  }
137  }
138 
139  /// <summary>
140  /// Gets or sets the preferred depth stencil format.
141  /// </summary>
142  /// <value>The preferred depth stencil format.</value>
143  public PixelFormat PreferredDepthStencilFormat
144  {
145  get
146  {
147  return preferredDepthStencilFormat;
148  }
149 
150  set
151  {
152  preferredDepthStencilFormat = value;
153  }
154  }
155 
156  public override void Initialize()
157  {
158  var gamePlatform = (IGamePlatform)this.Services.GetService(typeof(IGamePlatform));
159  GameContext.RequestedWidth = PreferredBackBufferWidth;
160  GameContext.RequestedHeight = PreferredBackBufferHeight;
161  Window = gamePlatform.CreateWindow(GameContext);
162  Window.Visible = true;
163 
164  Window.ClientSizeChanged += WindowOnClientSizeChanged;
165 
166  base.Initialize();
167  }
168 
169  private Vector2 GetRequestedSize(out PixelFormat format)
170  {
171  var bounds = Window.ClientBounds;
172  format = PreferredBackBufferFormat == PixelFormat.None ? PixelFormat.R8G8B8A8_UNorm : PreferredBackBufferFormat;
173  return new Vector2(
174  PreferredBackBufferWidth == 0 || windowUserResized ? bounds.Width : PreferredBackBufferWidth,
175  PreferredBackBufferHeight == 0 || windowUserResized ? bounds.Height : PreferredBackBufferHeight);
176  }
177 
178  protected virtual void CreateOrUpdatePresenter()
179  {
180  if (Presenter == null)
181  {
182  PixelFormat resizeFormat;
183  var size = GetRequestedSize(out resizeFormat);
184  var presentationParameters = new PresentationParameters((int)size.X, (int)size.Y, Window.NativeWindow, resizeFormat) { DepthStencilFormat = PreferredDepthStencilFormat };
185  presentationParameters.PresentationInterval = PresentInterval.Immediate;
186  Presenter = new SwapChainGraphicsPresenter(GraphicsDevice, presentationParameters);
187  isBackBufferToResize = false;
188  }
189  }
190 
191  public override bool BeginDraw()
192  {
193  if (GraphicsDevice != null && Window.Visible)
194  {
195  savedPresenter = GraphicsDevice.Presenter;
196  savedViewport = GraphicsDevice.Viewport;
197 
198  CreateOrUpdatePresenter();
199 
200  if (isBackBufferToResize || windowUserResized)
201  {
202  PixelFormat resizeFormat;
203  var size = GetRequestedSize(out resizeFormat);
204  Presenter.Resize((int)size.X, (int)size.Y, resizeFormat);
205 
206  isBackBufferToResize = false;
207  windowUserResized = false;
208  }
209 
210  GraphicsDevice.Presenter = Presenter;
211 
212  beginDrawOk = true;
213  return true;
214  }
215 
216  beginDrawOk = false;
217  return false;
218  }
219 
220  public override void EndDraw()
221  {
222  if (beginDrawOk && GraphicsDevice != null)
223  {
224  try
225  {
226  Presenter.Present();
227  }
228  catch (GraphicsException ex)
229  {
230  if (ex.Status != GraphicsDeviceStatus.Removed && ex.Status != GraphicsDeviceStatus.Reset)
231  {
232  throw;
233  }
234  }
235 
236  if (savedPresenter != null)
237  {
238  GraphicsDevice.Presenter = savedPresenter;
239  }
240  }
241  }
242 
243  private void WindowOnClientSizeChanged(object sender, EventArgs eventArgs)
244  {
245  windowUserResized = true;
246  }
247  }
248 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
GraphicsDeviceStatus
Describes the current status of a GraphicsDevice.
Interface for a game platform (OS, machine dependent).
Definition: IGamePlatform.cs:8
A service registry is a IServiceProvider that provides methods to register and unregister services...
A GameSystem that allows to draw to another window or control. Currently only valid on desktop with W...
override void Initialize()
This method is called when the component is added to the game.
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.
This class is a frontend to SwapChain and SwapChain1.
Defines the window dimensions of a render-target surface onto which a 3D volume projects.
Definition: Viewport.cs:14
GameWindowRenderer(IServiceRegistry registry, GameContext gameContext=null)
Initializes a new instance of the GameWindowRenderer class.
override bool BeginDraw()
Starts the drawing of a frame. This method is followed by calls to Draw and EndDraw.
Contains context used to render the game (Control for WinForm, a DrawingSurface for WP8...
Definition: GameContext.cs:31
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
override void EndDraw()
Ends the drawing of a frame. This method is preceeded by calls to Draw and BeginDraw.
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32
Describess how data will be displayed to the screen.