예제 #1
0
        public void Run()
        {
            form = CreateForm();

            SwapChainDescription desc = new SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(1280, 720, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = DisplayHandle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { FeatureLevelDX.Level_10_0 }, desc, out device, out swapChain);
            FactoryDX factory = swapChain.GetParent <FactoryDX>();

            factory.MakeWindowAssociation(DisplayHandle, WindowAssociationFlags.IgnoreAll);
            backBuffer     = Resource.FromSwapChain <Texture2D>(swapChain, 0);
            backBufferView = new RenderTargetView(device, backBuffer);

            Factory2D = new Factory2D();
            using (Surface surface = backBuffer.QueryInterface <Surface>()) {
                RenderTarget2D = new RenderTarget(Factory2D, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
            }
            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
            FactoryDirectWrite           = new FactoryDW();
            SceneColorBrush = new SolidColorBrush(RenderTarget2D, new RawColor4(1, 1, 1, 1));

            Initialize();
            bool isFormClosed   = false;
            bool formIsResizing = false;

            form.MouseClick  += HandleMouseClick;
            form.KeyDown     += HandleKeyDown;
            form.KeyUp       += HandleKeyUp;
            form.FormClosed  += (o, args) => { isFormClosed = true; };
            form.ResizeBegin += (o, args) => { formIsResizing = true; };
            form.ResizeEnd   += (o, args) => {
                formIsResizing = false;
                HandleResize(o, args);
            };
            form.Resize += (o, args) => {
                if (form.WindowState != currentFormWindowState)
                {
                    HandleResize(o, args);
                }
                currentFormWindowState = form.WindowState;
            };

            LoadContent();
            clock.Start();
            BeginRun();
            RenderLoop.Run(form, () => {
                if (isFormClosed)
                {
                    return;
                }
                OnUpdate();
                if (!formIsResizing)
                {
                    Render();
                }
            });

            UnloadContent();
            EndRun();

            Dispose();
        }
예제 #2
0
        public void Run()
        {
            form      = CreateForm();
            Factory2D = new Factory2D();
            FactoryDW = new FactoryDW();

            HwndRenderTargetProperties props = new HwndRenderTargetProperties();

            props.Hwnd           = DisplayHandle;
            props.PixelSize      = new SharpDX.Size2(1280, 720);
            props.PresentOptions = PresentOptions.Immediately;

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), props);
            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;

            SceneColorBrush = new SolidColorBrush(RenderTarget2D, new SharpDX.Mathematics.Interop.RawColor4(1f, 1f, 1f, 1f));
            Initialize();

            bool isFormClosed = false, formIsResizing = false;

            form.MouseClick += HandleMouseClick;
            form.MouseMove  += HandleMouseMove;
            form.KeyDown    += HandleKeyDown;
            form.KeyUp      += HandleKeyUp;
            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.FormClosed += (o, args) => {
                isFormClosed = true;
            };

            LoadContent();

            clock.Start();
            BeginRun();
            RenderLoop.Run(form, () => {
                if (isFormClosed)
                {
                    return;
                }
                OnUpdate();
                if (!formIsResizing)
                {
                    Render();
                }
            });

            UnloadContent();
            EndRun();
            Dispose();
        }