25 using System.Collections.Generic;
27 using SiliconStudio.Paradox.Graphics;
28 using SiliconStudio.Core;
30 namespace SiliconStudio.
Paradox.Games
32 internal abstract class GamePlatform :
ReferenceBase, IGraphicsDeviceFactory, IGamePlatform
34 private bool hasExitRan =
false;
36 protected readonly GameBase game;
40 protected GameWindow gameWindow;
42 protected GamePlatform(GameBase game)
45 Services = game.Services;
46 Services.AddService(typeof(IGraphicsDeviceFactory),
this);
49 public static GamePlatform
Create(GameBase game)
51 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
52 return new GamePlatformWindowsRuntime(game);
53 #elif SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP && SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
54 return new GamePlatformOpenTK(game);
55 #elif SILICONSTUDIO_PLATFORM_ANDROID
56 return new GamePlatformAndroid(game);
57 #elif SILICONSTUDIO_PLATFORM_IOS
58 return new GamePlatformiOS(game);
60 return new GamePlatformDesktop(game);
64 public abstract string DefaultAppDirectory {
get; }
66 public object WindowContext {
get; set; }
68 public event EventHandler<EventArgs> Activated;
70 public event EventHandler<EventArgs> Deactivated;
72 public event EventHandler<EventArgs> Exiting;
74 public event EventHandler<EventArgs> Idle;
76 public event EventHandler<EventArgs> Resume;
78 public event EventHandler<EventArgs> Suspend;
80 public event EventHandler<EventArgs> WindowCreated;
82 public GameWindow MainWindow
90 internal abstract GameWindow[] GetSupportedGameWindows();
92 public virtual GameWindow CreateWindow(GameContext gameContext)
94 gameContext = gameContext ??
new GameContext();
96 var windows = GetSupportedGameWindows();
98 foreach (var gameWindowToTest
in windows)
100 if (gameWindowToTest.CanHandle(gameContext))
102 gameWindowToTest.Services = Services;
103 gameWindowToTest.Initialize(gameContext);
104 return gameWindowToTest;
108 throw new ArgumentException(
"Game Window context not supported on this platform");
111 public bool IsBlockingRun {
get;
protected set; }
113 public void Run(GameContext gameContext)
115 gameWindow = CreateWindow(gameContext);
118 gameWindow.GameContext = gameContext;
119 gameWindow.Activated += OnActivated;
120 gameWindow.Deactivated += OnDeactivated;
121 gameWindow.InitCallback = OnInitCallback;
122 gameWindow.RunCallback = OnRunCallback;
124 var windowCreated = WindowCreated;
125 if (windowCreated != null)
133 private void OnRunCallback()
136 var unhandledException = game.UnhandledExceptionInternal;
137 if (unhandledException != null)
147 unhandledException(
this,
new GameUnhandledExceptionEventArgs(e,
false));
157 private void OnInitCallback()
160 var unhandledException = game.UnhandledExceptionInternal;
161 if (unhandledException != null)
166 game.InitializeBeforeRun();
171 unhandledException(
this,
new GameUnhandledExceptionEventArgs(e,
false));
177 game.InitializeBeforeRun();
185 if (!IsBlockingRun && game.IsExiting() && !hasExitRan)
192 public virtual void Exit()
195 gameWindow.Exiting =
true;
198 protected void OnActivated(
object source,
EventArgs e)
200 EventHandler<EventArgs> handler = Activated;
201 if (handler != null) handler(
this, e);
204 protected void OnDeactivated(
object source,
EventArgs e)
206 EventHandler<EventArgs> handler = Deactivated;
207 if (handler != null) handler(
this, e);
210 protected void OnExiting(
object source,
EventArgs e)
212 EventHandler<EventArgs> handler = Exiting;
213 if (handler != null) handler(
this, e);
216 protected void OnIdle(
object source,
EventArgs e)
218 EventHandler<EventArgs> handler = Idle;
219 if (handler != null) handler(
this, e);
222 protected void OnResume(
object source,
EventArgs e)
224 EventHandler<EventArgs> handler = Resume;
225 if (handler != null) handler(
this, e);
228 protected void OnSuspend(
object source,
EventArgs e)
230 EventHandler<EventArgs> handler = Suspend;
231 if (handler != null) handler(
this, e);
234 protected void AddDevice(
DisplayMode mode, GraphicsDeviceInformation deviceBaseInfo, GameGraphicsParameters preferredParameters, List<GraphicsDeviceInformation> graphicsDeviceInfos)
240 var deviceInfo = deviceBaseInfo.Clone();
242 deviceInfo.PresentationParameters.RefreshRate = mode.RefreshRate;
244 if (preferredParameters.IsFullScreen)
246 deviceInfo.PresentationParameters.BackBufferFormat = mode.Format;
247 deviceInfo.PresentationParameters.BackBufferWidth = mode.Width;
248 deviceInfo.PresentationParameters.BackBufferHeight = mode.Height;
252 deviceInfo.PresentationParameters.BackBufferFormat = preferredParameters.PreferredBackBufferFormat;
253 deviceInfo.PresentationParameters.BackBufferWidth = preferredParameters.PreferredBackBufferWidth;
254 deviceInfo.PresentationParameters.BackBufferHeight = preferredParameters.PreferredBackBufferHeight;
258 deviceInfo.PresentationParameters.DepthStencilFormat = preferredParameters.PreferredDepthStencilFormat;
259 deviceInfo.PresentationParameters.MultiSampleCount = MSAALevel.None;
261 if (!graphicsDeviceInfos.Contains(deviceInfo))
263 graphicsDeviceInfos.Add(deviceInfo);
267 public virtual List<GraphicsDeviceInformation> FindBestDevices(GameGraphicsParameters preferredParameters)
269 var graphicsDeviceInfos =
new List<GraphicsDeviceInformation>();
275 if (graphicsAdapter.Outputs.Length == 0)
280 var preferredGraphicsProfiles = preferredParameters.PreferredGraphicsProfile;
283 if (graphicsAdapter.VendorId == 0x8086)
284 preferredGraphicsProfiles = preferredGraphicsProfiles.Select(x => x < GraphicsProfile.Level_10_0 ? GraphicsProfile.Level_10_0 : x).ToArray();
287 foreach (var featureLevel
in preferredGraphicsProfiles)
290 if (graphicsAdapter.IsProfileSupported(featureLevel))
292 var deviceInfo =
new GraphicsDeviceInformation
294 Adapter = graphicsAdapter,
298 MultiSampleCount = MSAALevel.None,
299 IsFullScreen = preferredParameters.IsFullScreen,
300 PreferredFullScreenOutputIndex = preferredParameters.PreferredFullScreenOutputIndex,
301 PresentationInterval = preferredParameters.SynchronizeWithVerticalRetrace ? PresentInterval.One : PresentInterval.Immediate,
302 DeviceWindowHandle = MainWindow.NativeWindow,
306 var preferredMode =
new DisplayMode(preferredParameters.PreferredBackBufferFormat,
307 preferredParameters.PreferredBackBufferWidth,
308 preferredParameters.PreferredBackBufferHeight,
309 preferredParameters.PreferredRefreshRate);
312 if (preferredParameters.IsFullScreen)
314 if (preferredParameters.PreferredFullScreenOutputIndex < graphicsAdapter.Outputs.Length)
316 var output = graphicsAdapter.Outputs[preferredParameters.PreferredFullScreenOutputIndex];
317 var displayMode = output.FindClosestMatchingDisplayMode(preferredGraphicsProfiles, preferredMode);
318 AddDevice(displayMode, deviceInfo, preferredParameters, graphicsDeviceInfos);
323 AddDevice(preferredMode, deviceInfo, preferredParameters, graphicsDeviceInfos);
332 return graphicsDeviceInfos;
335 public virtual GraphicsDevice CreateDevice(GraphicsDeviceInformation deviceInformation)
338 graphicsDevice.Presenter =
new SwapChainGraphicsPresenter(graphicsDevice, deviceInformation.PresentationParameters);
340 return graphicsDevice;
343 public virtual void RecreateDevice(
GraphicsDevice currentDevice, GraphicsDeviceInformation deviceInformation)
345 currentDevice.Recreate(deviceInformation.Adapter ?? GraphicsAdapterFactory.Default,
new[] { deviceInformation.GraphicsProfile },
deviceInformation.DeviceCreationFlags, gameWindow.NativeWindow);
348 public virtual void DeviceChanged(
GraphicsDevice currentDevice, GraphicsDeviceInformation deviceInformation)
351 gameWindow.Resize(deviceInformation.PresentationParameters.BackBufferWidth, deviceInformation.PresentationParameters.BackBufferHeight);
356 if (currentDevice == null)
358 currentDevice = CreateDevice(deviceInformation);
362 RecreateDevice(currentDevice, deviceInformation);
365 DeviceChanged(currentDevice, deviceInformation);
367 return currentDevice;
370 protected override void Destroy()
372 if (gameWindow != null)
374 gameWindow.Dispose();
ComponentBase.Destroy() event.
static GraphicsAdapter[] Adapters
Collection of available adapters on the system.
Factory for GraphicsAdapter.
A service registry is a IServiceProvider that provides methods to register and unregister services...
Describes the display mode.
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 IReferencable class.
Creates a new file, always. If a file exists, the function overwrites the file, clears the existing a...
GraphicsProfile
Identifies the set of supported devices for the demo based on device capabilities.
PixelFormat
Defines various types of pixel formats.
Describess how data will be displayed to the screen.