Ejemplo n.º 1
0
        protected override void OnDraw(ID2D1DeviceContext renderTarget)
        {
            base.OnDraw(renderTarget);

            renderTarget.Clear(ClearColor);
            renderTarget.Transform = GlobalTransform;
            foreach (var sprite in Sprites.Values)
            {
                sprite.Draw(renderTarget);
            }

            renderTarget.Transform = Matrix3x2.Identity;
            if (ShowFPS)
            {
                renderTarget.DrawText($"FPS: {RenderTimer.FramesPerSecond:F1}",
                                      XResource.TextFormats[12.0f],
                                      new RectangleF(0, 0, renderTarget.Size.Width, renderTarget.Size.Height),
                                      XResource.GetColor(Color4.DimGray));
            }
        }
Ejemplo n.º 2
0
        public void Draw(IntPtr hWnd)
        {
            if (m_disposed)
            {
                return;
            }
            EnsureDevice(hWnd);

            using (var backbuffer = new IDXGISurface2())
            {
                // setup backbuffer
                m_swapchain.GetBuffer(0, ref IDXGISurface2.IID, out backbuffer.PtrForNew).ThrowIfFailed();
                backbuffer.GetDesc(out DXGI_SURFACE_DESC desc);


                var prop = new D2D1_BITMAP_PROPERTIES1
                {
                    bitmapOptions = D2D1_BITMAP_OPTIONS._TARGET | D2D1_BITMAP_OPTIONS._CANNOT_DRAW,
                    pixelFormat   = new D2D1_PIXEL_FORMAT
                    {
                        format    = DXGI_FORMAT._B8G8R8A8_UNORM,
                        alphaMode = D2D1_ALPHA_MODE._IGNORE
                    }
                };
                m_d2dContext.CreateBitmapFromDxgiSurface(backbuffer, ref prop, out ID2D1Bitmap1 bitmap).ThrowIfFailed();
                using (bitmap)
                {
                    m_d2dContext.SetTarget(bitmap);

                    // draw
                    m_d2dContext.BeginDraw();
                    var color = new Vector4(0, 0.1f, 0, 0);
                    m_d2dContext.Clear(ref color);

                    var brushColor = new Vector4(1, 0, 0, 1);
                    var brushProp  = new D2D1_BRUSH_PROPERTIES
                    {
                        opacity   = 1.0f,
                        transform = new Matrix3x2
                        {
                            M11 = 1.0f,
                            M22 = 1.0f,
                        }
                    };
                    m_d2dContext.CreateSolidColorBrush(ref brushColor, ref brushProp, out ID2D1SolidColorBrush brush).ThrowIfFailed();
                    using (brush)
                    {
                        var ellipse = new D2D1_ELLIPSE
                        {
                            radiusX = 50.0f,
                            radiusY = 50.0f,
                        };
                        m_d2dContext.DrawEllipse(ref ellipse, brush, 10.0f, null);
                    }
                }

                // dwrite
                var font   = new WSTR("Consolas");
                var text   = new WSTR("A0漢字B");
                var locale = new WSTR("ja-jp");

                m_dwriteFactory.CreateTextFormat(
                    ref font.Data,
                    null,
                    DWRITE_FONT_WEIGHT._REGULAR,
                    DWRITE_FONT_STYLE._NORMAL,
                    DWRITE_FONT_STRETCH._NORMAL,
                    144.0f,
                    ref locale.Data,
                    out IDWriteTextFormat pTextFormat
                    ).ThrowIfFailed();
                using (pTextFormat)
                {
                    pTextFormat.SetTextAlignment(DWRITE_TEXT_ALIGNMENT._CENTER).ThrowIfFailed();
                    pTextFormat.SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT._CENTER).ThrowIfFailed();

                    var brushColor = new Vector4(0.7f, 0.7f, 1, 1);
                    var brushProp  = new D2D1_BRUSH_PROPERTIES
                    {
                        opacity   = 1.0f,
                        transform = new Matrix3x2
                        {
                            M11 = 1.0f,
                            M22 = 1.0f,
                        }
                    };
                    m_d2dContext.CreateSolidColorBrush(ref brushColor, ref brushProp, out ID2D1SolidColorBrush brush).ThrowIfFailed();
                    using (brush)
                    {
                        var rect = new Vector4
                        {
                            X = 0,
                            Y = 0,
                            Z = m_width,
                            W = m_height,
                        };

                        m_d2dContext.DrawText(
                            ref text.Data,
                            (uint)text.Length, // The string's length.
                            pTextFormat,       // The text format.
                            ref rect,          // The region of the window where the text will be rendered.
                            brush,             // The brush used to draw the text.
                            D2D1_DRAW_TEXT_OPTIONS._NONE,
                            DWRITE_MEASURING_MODE._NATURAL
                            );
                    }
                }
                m_d2dContext.EndDraw(out ulong tag1, out ulong tag2);
                // }

                m_context.Flush();
                m_swapchain.Present(0, 0);
                m_d2dContext.SetTarget(null);
            }
        }