【发布时间】:2017-07-14 01:09:43
【问题描述】:
嗨,就像标题所说的那样,我正在尝试为一个项目制作一个图像查看器(有点像 Windows 图像查看器)。我看到很多代码将他们的 RenderForm 显示到 RenderLoop 中,但我不喜欢这种解决方案,因为我不想在 RenderLoop 中无限刷新图像。我只想在需要重绘时调用 Draw 方法(例如在缩放时)。问题是,现在我尝试使用 renderLoop.Show() 但它并没有留在屏幕上并且毕竟关闭代码已经执行...
using SharpDX;
using SharpDX.Windows;
using SharpDX.D3DCompiler;
using SharpDX.Direct2D1;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Mathematics;
using System.Drawing;
using System.Windows.Forms;
using Device = SharpDX.Direct3D11.Device;
using Color = SharpDX.Color;
namespace SharpDXWic
{
public class SharpDXDisplay : IDisposable
{
private const int WIDTH = 1500;
private const int HEIGHT = 800;
private Device device;
private SwapChain swapChain;
private RenderForm renderForm;
private RenderTargetView targetView;
public SharpDXDisplay(string display_title)
{
renderForm = new RenderForm(display_title);
renderForm.Width = WIDTH;
renderForm.Height = HEIGHT;
Texture2D target;
SwapChainDescription scd = new SwapChainDescription()
{
BufferCount = 1,
Flags = SwapChainFlags.None,
IsWindowed = true,
ModeDescription = new ModeDescription(WIDTH,HEIGHT, new Rational(60, 1),Format.R8G8B8A8_UNorm),
OutputHandle = renderForm.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain( SharpDX.Direct3D.DriverType.Hardware,DeviceCreationFlags.Debug, scd, out device, out swapChain);
target = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
targetView = new RenderTargetView(device, target);
device.ImmediateContext.OutputMerger.SetRenderTargets(targetView);
renderForm.Show();
device.ImmediateContext.ClearRenderTargetView(targetView, Color.CornflowerBlue);
swapChain.Present(0, PresentFlags.None);
}
private void OnClosing()
{
Dispose();
}
public void Dispose()
{
device.Dispose();
swapChain.Dispose();
renderForm.Dispose();
targetView.Dispose();
}
}
}
我的目标是在不进入 RenderLoop 的情况下绘制()一个表单。我只想按需刷新图像,而不是不断刷新。
【问题讨论】:
-
所有窗口都由循环驱动。游戏不断地在每一帧中绘制所有内容。诀窍是将实际计算排除在渲染代码之外。
-
您知道是否有可能拥有一个仅按需绘制的查看器。通过使用窗口形式可能吗?问题是 Graphics 。 DrawImage 对于我需要显示的图像大小来说太慢了,这就是我打开sharpDX的原因。
-
它不应该太慢,除非您每次绘制都重新创建图像。一次绘制所有静态的东西,缓存它,并且每帧只渲染缓存的图像。任何动态的,叠加在它上面。如果您每帧都创建一个新的图像/纹理,那么您做错了。对于游戏,您只更新必须更新的内容,并且有很多技巧可以优化它。