Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[Toolkit.Game] Added resize support for WPF
  • Loading branch information
ArtiomCiumac committed Jun 18, 2013
1 parent 6086a2d commit 7160838
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
Expand Up @@ -38,6 +38,7 @@ internal class GameWindowDesktopWpf : GameWindow
private bool isMouseCurrentlyHidden;
private bool isVisible;
private Cursor oldCursor;
private RenderTargetGraphicsPresenter presenter;

public override bool AllowUserResizing { get { return false; } set { /* ignore, WPF will resize everything itself */ } }
public override Rectangle ClientBounds { get { return new Rectangle(0, 0, (int)element.ActualWidth, (int)element.ActualHeight); } }
Expand Down Expand Up @@ -73,12 +74,16 @@ public override bool Visible

public override void BeginScreenDeviceChange(bool willBeFullScreen)
{
// TODO
}

public override void EndScreenDeviceChange(int clientWidth, int clientHeight)
{
// TODO
if (element != null)
{
element.Width = clientWidth;
element.Height = clientHeight;
element.SetBackbuffer(presenter.BackBuffer);
}
}

internal override bool CanHandle(GameContext gameContext)
Expand All @@ -93,17 +98,17 @@ internal override GraphicsPresenter CreateGraphicsPresenter(GraphicsDevice devic
var backbufferDesc = RenderTarget2D.CreateDescription(device,
parameters.BackBufferWidth,
parameters.BackBufferHeight,
Graphics.PixelFormat.B8G8R8A8.UNorm // TODO: take the format from presentation parameters
);
Graphics.PixelFormat.B8G8R8A8.UNorm);

// mandatory to share the surface with D3D9
backbufferDesc.OptionFlags |= ResourceOptionFlags.Shared;

var backbuffer = RenderTarget2D.New(device, backbufferDesc);

element.SetBackbuffer(backbuffer);
if (presenter != null)
RemoveAndDispose(ref presenter);

return new RenderTargetGraphicsPresenter(device, backbuffer, DepthFormat.None, true);
presenter = ToDispose(new RenderTargetGraphicsPresenter(device, backbufferDesc, DepthFormat.None, false, true));
element.SetBackbuffer(presenter.BackBuffer);
return presenter;
}

internal override void Initialize(GameContext gameContext)
Expand Down
2 changes: 1 addition & 1 deletion Source/Toolkit/SharpDX.Toolkit.Game/GamePlatform.cs
Expand Up @@ -231,7 +231,7 @@ public virtual GraphicsDevice CreateDevice(GraphicsDeviceInformation deviceInfor
device.Presenter = presenter;

// Force to resize the gameWindow
gameWindow.Resize(deviceInformation.PresentationParameters.BackBufferWidth, deviceInformation.PresentationParameters.BackBufferHeight);
gameWindow.Resize(parameters.BackBufferWidth, parameters.BackBufferHeight);

return device;
}
Expand Down
Expand Up @@ -22,34 +22,41 @@

namespace SharpDX.Toolkit.Graphics
{
using Direct3D11;

/// <summary>
/// Graphics presenter for SwapChain.
/// </summary>
public class RenderTargetGraphicsPresenter : GraphicsPresenter
{
private Texture2DDescription renderTargetDescription;
private readonly bool allowFormatChange;
private RenderTarget2D backBuffer;

public RenderTargetGraphicsPresenter(GraphicsDevice device, RenderTarget2D renderTarget, DepthFormat depthFormat = DepthFormat.None, bool disposeRenderTarget = false)
: base(device, CreatePresentationParameters(renderTarget, depthFormat))
public RenderTargetGraphicsPresenter(GraphicsDevice device, Texture2DDescription renderTargetDescription, DepthFormat depthFormat = DepthFormat.None, bool allowFormatChange = true, bool disposeRenderTarget = false)
: base(device, CreatePresentationParameters(renderTargetDescription, depthFormat))
{
PresentInterval = Description.PresentationInterval;

if (disposeRenderTarget)
ToDispose(renderTarget);

// Initialize the swap chain
backBuffer = renderTarget;
this.renderTargetDescription = renderTargetDescription;
this.allowFormatChange = allowFormatChange;

backBuffer = RenderTarget2D.New(device, renderTargetDescription);

if (disposeRenderTarget)
ToDispose(backBuffer);
}

private static PresentationParameters CreatePresentationParameters(RenderTarget2D renderTarget2D, DepthFormat depthFormat)
private static PresentationParameters CreatePresentationParameters(Texture2DDescription renderTargetDescription, DepthFormat depthFormat)
{
return new PresentationParameters()
{
BackBufferWidth = renderTarget2D.Width,
BackBufferHeight = renderTarget2D.Height,
BackBufferFormat = renderTarget2D.Description.Format,
BackBufferWidth = renderTargetDescription.Width,
BackBufferHeight = renderTargetDescription.Height,
BackBufferFormat = renderTargetDescription.Format,
DepthStencilFormat = depthFormat,
DeviceWindowHandle = renderTarget2D,
DeviceWindowHandle = renderTargetDescription,
Flags = SwapChainFlags.None,
IsFullScreen = true,
MultiSampleCount = MSAALevel.None,
Expand All @@ -67,11 +74,6 @@ public override RenderTarget2D BackBuffer
}
}

public void SetBackBuffer(RenderTarget2D backBuffer)
{
this.backBuffer = backBuffer;
}

public override object NativePresenter
{
get
Expand All @@ -97,9 +99,21 @@ public override void Present()
GraphicsDevice.Flush();
}

protected override void Dispose(bool disposeManagedResources)
public override void Resize(int width, int height, Format format)
{
base.Dispose(disposeManagedResources);
base.Resize(width, height, format);

renderTargetDescription.Width = width;
renderTargetDescription.Height = height;

if (allowFormatChange)
renderTargetDescription.Format = format;

if (backBuffer != null)
{
RemoveAndDispose(ref backBuffer);
backBuffer = RenderTarget2D.New(GraphicsDevice, renderTargetDescription);
}
}
}
}

0 comments on commit 7160838

Please sign in to comment.