コード例 #1
0
        private void Run()
        {
            bool toggleModeRequest = false;

            canvas.MouseClick += (sender, e) => toggleModeRequest = true;
            var worker = new Thread(() =>
            {
                int frameCount  = 0;
                Stopwatch drawT = new Stopwatch(), applyT = new Stopwatch(), advanceT = Stopwatch.StartNew(), renderT = Stopwatch.StartNew(), infoT = Stopwatch.StartNew();
                while (true)
                {
                    if (advanceT.ElapsedMilliseconds >= 3)
                    {
                        level.Advance(); player.Advance();
                        advanceT.Restart();
                    }
                    if (renderT.ElapsedMilliseconds >= 8)
                    {
                        frameCount++;
                        drawT.Start(); level.Render(); player.Render(); drawT.Stop();
                        applyT.Start(); screen.Render(); applyT.Stop();
                        renderT.Restart();
                    }
                    if (infoT.ElapsedMilliseconds >= 1000)
                    {
                        double drawS = drawT.ElapsedMilliseconds / 1000.0, applyS = applyT.ElapsedMilliseconds / 1000.0, totalS = drawS + applyS;
                        var info     = string.Format("Render using {0} - Frames:{1:n0} FPS:{2:n0} Draw:{3:p2} Apply:{4:p2}",
                                                     screen.mode, frameCount, frameCount / totalS, drawS / totalS, applyS / totalS);
                        form.BeginInvoke(new Action(() => form.Text = info));
                        infoT.Restart();
                    }
                    if (toggleModeRequest)
                    {
                        toggleModeRequest = false;
                        screen.mode       = (RenderMode)(((int)screen.mode + 1) % 3);
                        screen.ApplyMode();
                        frameCount = 0; drawT.Reset(); applyT.Reset();
                    }
                }
            });

            worker.IsBackground = true;
            worker.Start();
            Application.Run(form);
        }