Ejemplo n.º 1
0
        void MainLoop()
        {
            renderThreadTime.FrameBegan();
            renderThreadTime.TargetFps = TargetFps;
            Debug.AddValue("rendering / frames rendered", renderThreadTime.FrameCounter);

            EventSystem.Raise(new MyEngine.Events.FrameStarted());

            try
            {
                gameWindow.ProcessEvents();
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            this.WindowTitle = defaultWindowTitle + " - " + renderThreadTime;
            Debug.Tick("rendering / main render");

            // if window is not focused we dont want to have our character and camera responding to keyboard and mouse inputs
            if (this.Focused)
            {
                Input.Update();
                Debug.InputUpdate();
            }

            Debug.LogicUpdate();

            EventSystem.Raise(new MyEngine.Events.InputUpdate(renderThreadTime));
            EventSystem.Raise(new MyEngine.Events.EventThreadUpdate(renderThreadTime));

            if (ShouldContinueRunning == false)
            {
                return;
            }


            UpdateGPUMemoryInfo();

            if (Debug.GetCVar("rendering / debug / reload all shaders").EatBoolIfTrue())
            {
                Factory.ReloadAllShaders();
            }

            ubo.engine.totalElapsedSecondsSinceEngineStart = (float)stopwatchSinceStart.Elapsed.TotalSeconds;
            ubo.engine.gammaCorrectionTextureRead          = Debug.GetCVar("rendering / gamma correction", 2.2f);
            ubo.engine.gammaCorrectionFinalColor           = 1 / ubo.engine.gammaCorrectionTextureRead;

            //GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); My.Check();
            EventSystem.Raise(new MyEngine.Events.PreRenderUpdate(renderThreadTime));

            if (PauseRenderPrepare == false)
            {
                if (RenderPrepareInOwnThread)
                {
                    renderManagerBackReady.Wait();
                    renderManagerBackReady.Reset();
                }
                else
                {
                    RenderPrepareMain();
                }
                var tmp = renderManagerFront;
                renderManagerFront = renderManagerBack;
                renderManagerBack  = tmp;
                if (RenderPrepareInOwnThread)
                {
                    renderManagerPrepareNext.Set();
                }
            }

            {
                var scene        = scenes[0];
                var camera       = scene.MainCamera;
                var dataToRender = scene.DataToRender;

                //if (renderThreadTime.FpsPer1Sec > 30)
                renderManagerFront.SkyboxCubeMap = scene.skyBox;
                //else renderManagerFront.SkyboxCubeMap = null;
                renderManagerFront.RenderAll(camera, ubo, dataToRender.Lights, camera.postProcessEffects);
            }

            if (ShouldContinueRunning == false)
            {
                return;
            }

            gameWindow.SwapBuffers();

            EventSystem.Raise(new MyEngine.Events.PostRenderUpdate(renderThreadTime));


            //GC.Collect();
            Mesh.ProcessFinalizerQueue();

            EventSystem.Raise(new MyEngine.Events.FrameEnded(renderThreadTime));

            if (Debug.GetCVar("rendering / fps / automatically adjust target fps", false))
            {
                if (renderThreadTime.FrameCounter > TargetFps.Number * 10 && renderThreadTime.FpsPer10Sec < TargetFps)
                {
                    TargetFps.Number = (float)renderThreadTime.FpsPer1Sec;
                    Debug.AddValue("rendering / fps / adjusted target fps", TargetFps.Number);
                }
            }

            if (Debug.GetCVar("rendering / fps / fps throttling enabled", false))
            {
                var targetFps         = TargetFps * 1.2;
                var secondsWeCanSleep = 1 / targetFps - renderThreadTime.CurrentFrameElapsedSeconds;
                if (secondsWeCanSleep > 0)
                {
                    Debug.AddValue("rendering /fps / theoretical unthrottled fps", renderThreadTime.CurrentFrameElapsedTimeFps + " fps");
                    Thread.Sleep((1000 * secondsWeCanSleep).FloorToInt());
                }
            }
        }
Ejemplo n.º 2
0
        void OnStart()
        {
            ubo = new UniformBlock();
            //new PhysicsUsage.PhysicsManager();

            stopwatchSinceStart.Restart();

            renderManagerFront = new RenderManager(gameWindow.Width, gameWindow.Height);
            renderManagerBack  = new RenderManager(gameWindow.Width, gameWindow.Height);


            /*Debug.CommonCVars.VSync().ToogledByKey(OpenTK.Input.Key.V).OnChanged += (cvar) =>
             * {
             *      if (cvar.Bool) VSync = VSyncMode.On;
             *      else VSync = VSyncMode.Off;
             * };
             * Debug.CommonCVars.VSync().InitializeWith(false);*/

            Debug.GetCVar("rendering / fullscreen").OnChangedAndNow((cvar) =>
            {
                if (cvar.Bool && WindowState != WindowState.Fullscreen)
                {
                    WindowState = WindowState.Fullscreen;
                }
                else
                {
                    WindowState = WindowState.Normal;
                }
            });


            WindowTitle = defaultWindowTitle;



            foreach (StringName r in System.Enum.GetValues(typeof(StringName)))
            {
                if (r == StringName.Extensions)
                {
                    break;
                }
                var str = GL.GetString(r); MyGL.Check();
                Log.Info(r.ToString() + ": " + str);
            }

            // Other state
            //GL.Enable(EnableCap.Texture2D); My.Check();
            //GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); My.Check();
            //GL.Enable(EnableCap.Multisample); My.Check();

            gameWindow.Resize += (sender, e) => OnResize();
            OnResize();

            gameWindow.Visible = true;

            StartOtherThreads();

            while (ShouldContinueRunning)
            {
                MainLoop();
            }

            gameWindow.Exit();
        }