如何在sharpDX中绘图?

问题描述 投票:0回答:2

这是我第一次使用sharpdx在屏幕上绘制椭圆,我这里有这段代码(使用c#.net框架windows窗体),我不知道为什么,有两个问题:

  1. 调整窗口大小时,椭圆被拉伸,我不知道为什么
  2. 未向我显示 fps 和 GPU 信息

我也想尽可能优化我的代码!

这是我的代码:

    private WindowRenderTarget _renderTarget;
    private Ellipse el;
    private Stopwatch _stopwatch = new Stopwatch();
    private int _frameCount;
    private string _gpuDescription;
    public Form1()
    {
        InitializeComponent();
        InitializeDirect2D();

        el = new Ellipse
        {
            RadiusX = 50,
            RadiusY = 50,
            Point = new RawVector2(ClientSize.Width / 2.0f, ClientSize.Height / 2.0f)
        };

        using (var factory = new SharpDX.DXGI.Factory1())
        {
            using (var adapter = factory.GetAdapter1(0))
            {
                _gpuDescription = adapter.Description.Description;
            }
        }

        this.Text = _gpuDescription;
    }

    private void InitializeDirect2D()
    {
        var dpi = this.CreateGraphics().DpiX;
        var properties = new HwndRenderTargetProperties
        {
            Hwnd = this.Handle,
            PixelSize = new Size2(this.ClientSize.Width, this.ClientSize.Height),
            PresentOptions = PresentOptions.None
        };
        var renderTargetProperties = new RenderTargetProperties
        {
            DpiX = dpi,
            DpiY = dpi,
            Type = RenderTargetType.Hardware,
            Usage = RenderTargetUsage.GdiCompatible,
            PixelFormat = new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)
        };
        _renderTarget = new WindowRenderTarget(new SharpDX.Direct2D1.Factory(), renderTargetProperties, properties);
    }



    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        SolidColorBrush brush = new SolidColorBrush(_renderTarget, new RawColor4(1.0f, 0.0f, 0.0f, 1.0f));

        _renderTarget.BeginDraw();
        _renderTarget.Clear(new RawColor4(0, 0, 0, 1));

        _renderTarget.FillEllipse(el, brush);

        _renderTarget.EndDraw();

        
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        _frameCount++;
        if (_stopwatch.ElapsedMilliseconds >= 1000)
        {
            this.Text = $"FPS: {_frameCount}, GPU: {_gpuDescription}";
            _frameCount = 0;
            _stopwatch.Restart();
        }
    }

我希望有人能帮助我解决这些问题,我真的很感激!

c# winforms directx sharpdx
2个回答
0
投票

你的秒表还没有启动,所以你可以在构造函数中添加这样的东西:

public Form1()
{
    InitializeComponent();
    InitializeDirect2D();

    _stopwatch.Start();
    ...
}
 

如果调整窗口大小,您还必须调整渲染目标的大小,就像在表单的调整大小事件上一样:

private void Form1_Resize(object sender, EventArgs e)
{
    _renderTarget.Resize(new Size2(ClientSize.Width, ClientSize.Height));
}

对于测量 FPS,您必须使用一个计时器,其滴答间隔低于或等于您的显示器频率(这远非完美,因为它还取决于其他因素,但至少它应该对您更有用),因此将其添加到构造函数也是:

timer1.Interval = 10; // ~16ms or lower if your monitor is 60hz

0
投票

我有另一个代码,它基本上移动椭圆,但它不起作用。当然,坐标正在改变,但椭圆没有显示出来

代码:

    private WindowRenderTarget _renderTarget;
    private Ellipse el;
    private Stopwatch _stopwatch = new Stopwatch();
    private int _frameCount;
    private string _gpuDescription;
    private float ellipseXPosition = 100; // Starting x-coordinate
    private const float MovementSpeed = 2f;
    public Form1()
    {
        InitializeComponent();
        InitializeDirect2D();


        el = new Ellipse
        {
            RadiusX = 50,
            RadiusY = 50,
            Point = new RawVector2(100, 100)
        };


        using (var factory = new SharpDX.DXGI.Factory1())
        {
            using (var adapter = factory.GetAdapter1(0))
            {
                _gpuDescription = adapter.Description.Description;
            }
        }

        _stopwatch.Start();
        // Force a repaint
        this.Invalidate();
    }


    private void InitializeDirect2D()
    {
        var dpi = this.CreateGraphics().DpiX;
        var properties = new HwndRenderTargetProperties
        {
            Hwnd = this.Handle,
            PixelSize = new Size2(this.ClientSize.Width, this.ClientSize.Height),
            PresentOptions = PresentOptions.None
        };
        var renderTargetProperties = new RenderTargetProperties
        {
            DpiX = dpi,
            DpiY = dpi,
            Type = RenderTargetType.Hardware,
            Usage = RenderTargetUsage.GdiCompatible,
            PixelFormat = new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)
        };
        _renderTarget = new WindowRenderTarget(new SharpDX.Direct2D1.Factory(), renderTargetProperties, properties);
    }



    private void Form1_Load(object sender, EventArgs e)
    {
        
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Console.WriteLine("Form1_Paint method called");
        SolidColorBrush brush = new SolidColorBrush(_renderTarget, new RawColor4(1.0f, 0.0f, 0.0f, 1.0f));

        _renderTarget.BeginDraw();
        _renderTarget.Clear(new RawColor4(0, 0, 0, 1));

        // Update the position of the ellipse
        el.Point = new RawVector2(ellipseXPosition, 100); // Y-coordinate remains the same

        // Rysowanie elipsy
        _renderTarget.FillEllipse(el, brush);

        _renderTarget.EndDraw();

        // Move the ellipse to the right for the next frame
        ellipseXPosition += MovementSpeed;

        // Check if the ellipse reaches the right boundary and reset its position
        if (ellipseXPosition > this.ClientSize.Width)
        {
            ellipseXPosition = -el.RadiusX; // Reset to the left side of the window
        }
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (_renderTarget != null)
        {
            _renderTarget.Resize(new Size2(ClientSize.Width, ClientSize.Height));
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        _frameCount++;
        if (_stopwatch.ElapsedMilliseconds >= 1000)
        {
            this.Text = $"FPS: {_frameCount}, GPU: {_gpuDescription}";
            _frameCount = 0;
            _stopwatch.Restart();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.