Skip to content

Instantly share code, notes, and snippets.

@baobao
Created November 14, 2018 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baobao/afa2704aeb86f97afea5d72b55182ab2 to your computer and use it in GitHub Desktop.
Save baobao/afa2704aeb86f97afea5d72b55182ab2 to your computer and use it in GitHub Desktop.
背景クリアショートコード
using System.Windows.Forms;
using SlimDX.DXGI;
using SlimDX.Windows;
using SlimDX.Direct3D11;
using Device = SlimDX.Direct3D11.Device;
public class Test:Form
{
RenderForm form;
Device device;
SwapChain swapChain;
RenderTargetView renderTarget;
public void Run()
{
Test.CreateDeviceWithSwapChain(this, out device, out swapChain);
using (var backBuffer = SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(swapChain, 0))
{
renderTarget = new RenderTargetView(device, backBuffer);
}
MessagePump.Run(this, Draw);
}
void Draw()
{
device.ImmediateContext.ClearRenderTargetView(
renderTarget,
new SlimDX.Color4(1, 1, 0, 0)
);
// クリアの反映
swapChain.Present(0, PresentFlags.None);
}
/// <summary>
/// デバイスとSwapChainを作成
/// </summary>
static public SlimDX.Result CreateDeviceWithSwapChain(
Form form,
out SlimDX.Direct3D11.Device device,
out SwapChain swapChain
)
{
return Device.CreateWithSwapChain(
// Direct3Dの機能を持つデバイス(GPU)を使用する
DriverType.Hardware,
// デバイスに特別なふるまいをさせるときは設定するが、基本はNone
DeviceCreationFlags.None,
// SwapChainの詳細設定
new SwapChainDescription()
{
// 2:ダブルバッファでティアリングを回避
BufferCount = 2,
// 表示するウィンドウ
OutputHandle = form.Handle,
// falseにするとフルスクリーン
IsWindowed = true,
// SwapChainのマルチサンプル方法設定
SampleDescription = new SampleDescription()
{
Count = 1,
Quality = 0
},
// ウィンドウの大きさやリフレッシュレートに関する設定
ModeDescription = new ModeDescription()
{
// ウィンドウの幅
Width = form.ClientSize.Width,
// ウィンドウの高さ
Height = form.ClientSize.Height,
// リフレッシュレート60Hz
RefreshRate = new SlimDX.Rational(60, 1),
// ウィンドウのフォーマット
// https://msdn.microsoft.com/ja-jp/library/ee416140(v=vs.85).aspx
Format = Format.R8G8B8A8_UNorm
},
// 描画対象の使用方法
// ウィンドウにただ表示するだけなので、RenderTargetOutputをセット
Usage = Usage.RenderTargetOutput
},
out device,
out swapChain
);
}
}
static class Program
{
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
static void Main()
{
using (var test = new Test())
{
test.Run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment