Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BackgroundRenderer.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 using System;
5 
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Core.Serialization.Assets;
9 using SiliconStudio.Paradox.Graphics;
10 
11 namespace SiliconStudio.Paradox.Effects
12 {
13  /// <summary>
14  /// This renderer draws a full-screen image as background.
15  /// The ratio or the texture used is preserved. The texture is centered and cropped along X or Y axis depending on the screen ratio.
16  /// </summary>
17  /// <remarks>This renderer does not write into the depth buffer</remarks>
19  {
20  private readonly SpriteBatch spriteBatch;
21 
22  /// <summary>
23  /// Gets or sets the texture displayed as background.
24  /// </summary>
25  public Texture2D BackgroundTexture { get; set; }
26 
27  /// <summary>
28  /// Initializes a new instance of the <see cref="BackgroundRenderer"/> with null texture.
29  /// </summary>
30  /// <param name="services">The services.</param>
32  : this(services, null)
33  {
34  }
35 
36  /// <summary>
37  /// Initializes a new instance of the <see cref="BackgroundRenderer"/> using the provided file as background texture.
38  /// </summary>
39  /// <param name="services">The services.</param>
40  /// <param name="backgroundTexturePath">The path to the background texture to use</param>
41  public BackgroundRenderer(IServiceRegistry services, string backgroundTexturePath)
42  : base(services)
43  {
44  // load the background texture
45  if (!string.IsNullOrEmpty(backgroundTexturePath))
46  {
47  var assetManager = (IAssetManager)Services.GetService(typeof(IAssetManager));
48  BackgroundTexture = assetManager.Load<Texture2D>(backgroundTexturePath);
49  }
50 
51  spriteBatch = new SpriteBatch(GraphicsDevice);
52  }
53 
54  protected override void OnRendering(RenderContext context)
55  {
56  if(BackgroundTexture == null)
57  return;
58 
60 
61  var imageBufferMinRatio = Math.Min(BackgroundTexture.Width / (float)GraphicsDevice.BackBuffer.Width, BackgroundTexture.Height / (float)GraphicsDevice.BackBuffer.Height);
62  var sourceSize = new Int2((int)(GraphicsDevice.BackBuffer.Width * imageBufferMinRatio), (int)(GraphicsDevice.BackBuffer.Height * imageBufferMinRatio));
63  var source = new Rectangle((BackgroundTexture.Width - sourceSize.X) / 2, (BackgroundTexture.Height - sourceSize.Y) / 2, sourceSize.X, sourceSize.Y);
64 
65  spriteBatch.Begin(SpriteSortMode.FrontToBack, GraphicsDevice.BlendStates.Opaque, GraphicsDevice.SamplerStates.LinearClamp, GraphicsDevice.DepthStencilStates.None);
66  spriteBatch.Draw(BackgroundTexture, destination, source, Color.White, 0, Vector2.Zero, SpriteEffects.None, ImageOrientation.AsIs, 0);
67  spriteBatch.End();
68 
69  // reset the states to default
70  GraphicsDevice.SetBlendState(null);
71  GraphicsDevice.SetRasterizerState(null);
72  GraphicsDevice.SetDepthStencilState(null);
73  }
74  }
75 }
RenderTarget BackBuffer
Gets the back buffer sets by the current Presenter setup on this device.
Performs render pipeline transformations attached to a specific RenderPass.
Definition: Renderer.cs:13
This renderer draws a full-screen image as background. The ratio or the texture used is preserved...
A service registry is a IServiceProvider that provides methods to register and unregister services...
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.
int Width
Gets the width in texel.
Definition: RenderTarget.cs:23
Thread-local storage context used during rendering.
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
int Height
Gets the height in texel.
Definition: RenderTarget.cs:29
System.Windows.Shapes.Rectangle Rectangle
Definition: ColorPicker.cs:16
Represents a three dimensional mathematical vector.
Definition: Int2.cs:41
BackgroundRenderer(IServiceRegistry services)
Initializes a new instance of the BackgroundRenderer with null texture.
SiliconStudio.Core.Mathematics.RectangleF RectangleF
Definition: SpriteFont.cs:17
override void OnRendering(RenderContext context)
BackgroundRenderer(IServiceRegistry services, string backgroundTexturePath)
Initializes a new instance of the BackgroundRenderer using the provided file as background texture...