简体   繁体   中英

SlimDX: limit the frame rate for lower cpu usage

I am making a 2D game engine using SlimDX (DirectX 9), however Im facing two issues that are linked together, fluctuating frame rates (quite high frame rate though) and high cpu usage. The frame rate is essentially the game timer that all animations run off. The CPU usage is also through the roof(20%), I would like to limit how fast SlimDX draws frames for lower CPU usage, and to stabilize animation and game speed, how could i do this?

ps Im sure there is a better way to make a game "timer" that is separate from the frame rate, any info would be appreciated.

loop and inizialization:

            var form = new RenderForm(World.WindowTitle);
        form.ClientSize = new Size(World.WindowWidth, World.WindowHeight);
        var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters()
        {
            BackBufferWidth = form.ClientSize.Width,
            BackBufferHeight = form.ClientSize.Height
        });
        //Some stuff here, irrelevant to DirectX
        MessagePump.Run(form, () =>
        {
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            device.BeginScene();
            sprite.Begin(SlimDX.Direct3D9.SpriteFlags.AlphaBlend);
            //Stuff here
            sprite.End();
            device.EndScene();
            device.Present();
            //Thread.Sleep(1); <-- this didnt work using 0 or 1, it actually increased cpu ussage
        });

NVM i had an idea that worked, because i want 60 fps that would mean about 16ms between each frame, so i measure the time it takes to render a frame then Thread.Sleep for 16 - render time (if above 0), now CPU usage is zero and if the engine takes longer than 16ms to draw a frame then it will draw the next frame directly after.

            var form = new RenderForm(World.WindowTitle);
    form.ClientSize = new Size(World.WindowWidth, World.WindowHeight);
    var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters()
    {
        BackBufferWidth = form.ClientSize.Width,
        BackBufferHeight = form.ClientSize.Height
    });
    //Some stuff here, irrelevant to DirectX
    MessagePump.Run(form, () =>
    {
        Stopwatch stopwatch = Stopwatch.StartNew();
        device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
        device.BeginScene();
        sprite.Begin(SlimDX.Direct3D9.SpriteFlags.AlphaBlend);
        //Stuff here
        sprite.End();
        device.EndScene();
        device.Present();
        stopwatch.Stop();
        int ms = 16 - (int)(stopwatch.ElapsedMilliseconds);
        if (ms > 0)
        {
            Thread.Sleep(ms);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM