Home Game Development c# – Game loop isn’t performing well enough, so my frame rate is too low (Windows Form + GDI+)

c# – Game loop isn’t performing well enough, so my frame rate is too low (Windows Form + GDI+)

0
c# – Game loop isn’t performing well enough, so my frame rate is too low (Windows Form + GDI+)

[ad_1]

I decided to learn about building games, so I picked up C# to use it along with Windows Form – I already have C# experience, so that was the main reason I did so. That said, I tasked myself to build a simple Windows Form app that renders a noise image targeting hopefully at 60 FPS. I’m using the GDI+ APIs to render bitmaps on screen; however, I noticed the app isn’t even close to render 60 FPS – it renders around 36-39 FPS on a release build.

I should mention I’m a totally newbie in both Windows Form, and image rendering, so I believe my code isn’t optimized at all. I’m sharing below the code snippet the app runs in order to loop infinitely to render the noise images.

    private static Bitmap GetStaticNoise(int width, int height)
    {
        var bitmap = new Bitmap(width, height);
        for (int y = 0; y < bitmap.Height; y++)
        {
            for (int x = 0; x < bitmap.Width; x++)
            {
                Color color = Color.Black;

                var rnd = Random.Shared.Next(0, 2);
                if (rnd % 2 == 0)
                {
                    color = Color.White;
                }

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

        return bitmap;
    }

    private void Form1_Load(object? sender, EventArgs e)
    {
        Task.Factory.StartNew(() =>
        {
            var frameStopwatch = new Stopwatch();

            var screenGraphics = _screen.CreateGraphics();

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

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

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

                    frameStopwatch.Stop();

                    screenGraphics.DrawString(frameStopwatch.ElapsedMilliseconds.ToString(), font, Brushes.Red, new Point(0, 0));

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

        }, TaskCreationOptions.LongRunning);
    }

Any advice about what could be changed here it’s appreciated. Also, any resource that could be useful to learn about using GDI+ for game development with Windows Form is also appreciated (or any resource that could help me understanding for instance this situation I’m running into).

Ah! the Windows Form is on .NET 6.0. I’m also attaching a picture of the bitmap rendered on screen (upper left corner shows the time in MS to render the frame)

app frame rate

[ad_2]

Previous article 10 best dystopian movies of all time, ranked
Next article Best early Black Friday gaming laptop deals 2023
Hello there! My name is YoleeTeam, and I am thrilled to welcome you to AmazonianGames.com. As the premier destination for all things related to Amazon Games' universe, we are dedicated to providing you with the most exciting and immersive gaming experiences out there. From captivating visuals to exhilarating gameplay, our website is packed with comprehensive insights, updates, and reviews to keep you ahead of the game. Whether you're a seasoned gamer or new to the scene, I am here to guide you through this virtual frontier. If you have any questions or suggestions, feel free to reach out to me at john@yoleesolutions.com. Embark on your Amazon gaming journey today with AmazonianGames.com and let the adventure begin!