private bool render() { if (State.DuringRoomTransition) { return(_roomTransitionWorkflow.Render()); } RenderLoop.Tick(); return(true); }
private void onRenderFrame(object sender, FrameEventArgs e) { if (RenderLoop == null || _renderFrameRetries > 3) { return; } try { _renderMessagePump.PumpMessages(); // render graphics if (_gameCount == 1 || _gameIndex == 2) //if we have 2 games (editor + game) we want the editor layout drawn above the game so only clear screen from the actual game { _graphics.ClearScreen(); } Events.OnBeforeRender.Invoke(); if (RenderLoop.Tick()) { if (_gameIndex == 1) //if we have 2 games (editor + game) editor is game index 1 and should be drawn last, so only the editor should swap buffers { if (_shouldSwapBuffers) { GameWindow.SwapBuffers(); } _shouldSwapBuffers = true; } } else if (_gameIndex != 1) { _shouldSwapBuffers = false; } if (_shouldSetRestart) { _shouldSetRestart = false; SaveLoad.SetRestartPoint(); } } catch (Exception ex) { _renderFrameRetries++; Debug.WriteLine("Exception when rendering:"); Debug.WriteLine(ex.ToString()); throw; } }
public void Start(IGameSettings settings) { GameLoop = _resolver.Container.Resolve <IGameLoop>(new TypedParameter(typeof(AGS.API.Size), settings.VirtualResolution)); TypedParameter settingsParameter = new TypedParameter(typeof(IGameSettings), settings); try { GameWindow = Resolver.Container.Resolve <IGameWindow>(settingsParameter); } catch (Exception ese) { Debug.WriteLine(ese.ToString()); throw; } _updateThread = new AGSUpdateThread(GameWindow); //using (GameWindow) { try { TypedParameter gameWindowParameter = new TypedParameter(typeof(IGameWindow), GameWindow); GameWindow.Load += (sender, e) => { Settings = Resolver.Container.Resolve <IRuntimeSettings>(settingsParameter, gameWindowParameter); _graphics.ClearColor(0f, 0f, 0f, 1f); _graphics.Init(); _glUtils.GenBuffers(); Factory = Resolver.Container.Resolve <IGameFactory>(); TypedParameter sizeParameter = new TypedParameter(typeof(AGS.API.Size), Settings.VirtualResolution); Input = _resolver.Container.Resolve <IInput>(gameWindowParameter, sizeParameter); TypedParameter inputParamater = new TypedParameter(typeof(IInput), Input); TypedParameter gameParameter = new TypedParameter(typeof(IGame), this); RenderLoop = _resolver.Container.Resolve <IRendererLoop>(inputParamater, gameParameter); updateResolver(); HitTest = _resolver.Container.Resolve <IHitTest>(); AudioSettings = _resolver.Container.Resolve <IAudioSettings>(); SaveLoad = _resolver.Container.Resolve <ISaveLoad>(); _glUtils.AdjustResolution(settings.VirtualResolution.Width, settings.VirtualResolution.Height); Events.OnLoad.Invoke(); }; GameWindow.Resize += (sender, e) => { Events.OnScreenResize.Invoke(); }; _updateThread.OnThreadStarted += (sender, e) => { _updateMessagePump.SetSyncContext(); }; _updateThread.UpdateFrame += async(sender, e) => { if (_updateFrameRetries > 3) { return; } try { _updateMessagePump.PumpMessages(); if (State.Paused) { return; } adjustSpeed(); await GameLoop.UpdateAsync(); //Invoking repeatedly execute asynchronously, as if one subscriber is waiting on another subscriber the event will //never get to it (for example: calling ChangeRoom from within RepeatedlyExecute calls StopWalking which //waits for the walk to stop, only the walk also happens on RepeatedlyExecute and we'll hang. //Since we're running asynchronously, the next UpdateFrame will call RepeatedlyExecute for the walk cycle to stop itself and we're good. ///The downside of this approach is that we need to look out for re-entrancy issues. await Events.OnRepeatedlyExecute.InvokeAsync(); } catch (Exception ex) { _updateFrameRetries++; Debug.WriteLine(ex.ToString()); throw ex; } }; GameWindow.RenderFrame += (sender, e) => { if (RenderLoop == null || _renderFrameRetries > 3) { return; } try { _renderMessagePump.PumpMessages(); // render graphics _graphics.ClearScreen(); Events.OnBeforeRender.Invoke(); if (RenderLoop.Tick()) { GameWindow.SwapBuffers(); } if (Repeat.OnceOnly("SetFirstRestart")) { SaveLoad.SetRestartPoint(); } } catch (Exception ex) { _renderFrameRetries++; Debug.WriteLine("Exception when rendering:"); Debug.WriteLine(ex.ToString()); throw; } }; // Run the game at 60 updates per second _updateThread.Run(UPDATE_RATE); GameWindow.Run(UPDATE_RATE); } catch (Exception exx) { Debug.WriteLine(exx.ToString()); throw; } } }