c# – Recreation loop is not performing nicely sufficient, so my body price is simply too low (Home windows Type + GDI+)

c# – Recreation loop is not performing nicely sufficient, so my body price is simply too low (Home windows Type + GDI+)

[ad_1]

I made a decision to study constructing video games, so I picked up C# to make use of it together with Home windows Type – I have already got C# expertise, in order that was the primary cause I did so. That mentioned, I tasked myself to construct a easy Home windows Type app that renders a noise picture focusing on hopefully at 60 FPS. I am utilizing the GDI+ APIs to render bitmaps on display screen; nonetheless, I observed the app is not even near render 60 FPS – it renders round 36-39 FPS on a launch construct.

I ought to point out I am a completely beginner in each Home windows Type, and picture rendering, so I imagine my code is not optimized in any respect. I am sharing beneath the code snippet the app runs to be able to loop infinitely to render the noise pictures.

    personal static Bitmap GetStaticNoise(int width, int top)
    {
        var bitmap = new Bitmap(width, top);
        for (int y = 0; y < bitmap.Peak; y++)
        {
            for (int x = 0; x < bitmap.Width; x++)
            {
                Colour coloration = Colour.Black;

                var rnd = Random.Shared.Subsequent(0, 2);
                if (rnd % 2 == 0)
                {
                    coloration = Colour.White;
                }

                bitmap.SetPixel(x, y, coloration);
            }
        }

        return bitmap;
    }

    personal void Form1_Load(object? sender, EventArgs e)
    {
        Job.Manufacturing unit.StartNew(() =>
        {
            var frameStopwatch = new Stopwatch();

            var screenGraphics = _screen.CreateGraphics();

            var font = new Font(FontFamily.GenericMonospace, 30);

            var ct = cts.Token;
            whereas (!ct.IsCancellationRequested)
            {
                for (int body = 0; body < 60; body++)
                {
                    frameStopwatch.Restart();

                    // generates "noise"
                    Bitmap bitmap = GetStaticNoise(_screen.ClientSize.Width, _screen.ClientSize.Peak);
                    screenGraphics.DrawImage(bitmap, new Level(0, 0));
                    
                    bitmap.Dispose();

                    frameStopwatch.Cease();

                    screenGraphics.DrawString(frameStopwatch.ElapsedMilliseconds.ToString(), font, Brushes.Crimson, new Level(0, 0));

                    //Thread.Sleep(Math.Max(0, _frameCooldown.Milliseconds - (int)frameStopwatch.ElapsedMilliseconds));
                }
            }

        }, TaskCreationOptions.LongRunning);
    }

Any recommendation about what could possibly be modified right here it is appreciated. Additionally, any useful resource that could possibly be helpful to study utilizing GDI+ for recreation growth with Home windows Type can be appreciated (or any useful resource that would assist me understanding as an example this case I am working into).

Ah! the Home windows Type is on .NET 6.0. I am additionally attaching an image of the bitmap rendered on display screen (higher left nook exhibits the time in MS to render the body)

app frame rate

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply