ちょっとだけDirectX11

表題の書籍でDirectX11の勉強をはじめる。

https://www.kohgakusha.co.jp/books/detail/978-4-7775-1501-1

ひっかかったのはDXSDK_Jun10のサンプルをビルドする必要があると言う事。

C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Samples\C++\Effects11

ここで作成されるEffects11.libをリネームして配置する必要がありました。

C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include\d3dx11effect.h
C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86\d3dx11effects.lib

サンプルプログラムをSharpDXで書いてみました。

using System;
using System.Windows.Forms;
using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.DirectWrite;
using SharpDX.DXGI;
using SharpDX.Windows;
using TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode;
using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using Factory = SharpDX.Direct2D1.Factory;

namespace HelloApp
{

    /// <summary>
    /// Shows how to use DirectWrite to render simple text.
    /// Port of DirectWrite sample SimpleHelloWorld from Windows 7 SDK samples
    /// http://msdn.microsoft.com/en-us/library/dd742738%28v=VS.85%29.aspx
    /// </summary>
    public class Program
    {
        private FormWindowState _currentFormWindowState;
        private Form _form;
        public Factory Factory2D { get; private set; }
        public SharpDX.DirectWrite.Factory FactoryDWrite { get; private set; }
        public WindowRenderTarget RenderTarget2D { get; private set; }
        public SolidColorBrush SceneColorBrush { get; private set; }
        public TextFormat TextFormat { get; private set; }
        public RectangleF ClientRectangle { get; private set; }
        protected IntPtr DisplayHandle
        {
            get
            {
                return _form.Handle;
            }
        }
        protected void Initialize()
        {
            Factory2D = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();
            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
            properties.Hwnd = DisplayHandle;
            properties.PixelSize = new SharpDX.Size2(640, 480);
            properties.PresentOptions = PresentOptions.None;
            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);
            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
            SceneColorBrush = new SolidColorBrush(RenderTarget2D, Color.White);
            TextFormat = new TextFormat(FactoryDWrite, "メイリオ", 50) { TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center };
            RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype;
            ClientRectangle = new RectangleF(0, 0, 640, 480);
            SceneColorBrush.Color = Color.Black;               
        }
        protected void Draw()
        {
            RenderTarget2D.Clear(Color.White);
            RenderTarget2D.DrawText("Hello, World!", TextFormat, ClientRectangle, SceneColorBrush);
        }

        private void HandleResize(object sender, EventArgs e)
        {
            if (_form.WindowState == FormWindowState.Minimized)
            {
                return;
            }

        }
        protected virtual Form CreateForm()
        {
            return new RenderForm("Direct2D Hello Application")
            {
                ClientSize = new System.Drawing.Size(640, 480)
            };
        }

        public void Run()
        {
            _form = CreateForm();
            Initialize();

            bool isFormClosed = false;
            bool formIsResizing = false;

            _form.Resize += (o, args) =>
            {
                if (_form.WindowState != _currentFormWindowState)
                {
                    HandleResize(o, args);
                }

                _currentFormWindowState = _form.WindowState;
            };

            _form.ResizeBegin += (o, args) => { formIsResizing = true; };
            _form.ResizeEnd += (o, args) =>
            {
                formIsResizing = false;
                HandleResize(o, args);
            };

            _form.Closed += (o, args) => { isFormClosed = true; };

            RenderLoop.Run(_form, () =>
            {
                if (isFormClosed)
                {
                    return;
                }
                RenderTarget2D.BeginDraw();
                Draw();
                RenderTarget2D.EndDraw();
            });
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Program program = new Program();
            program.Run();
        }
    }
}