/// <summary> /// Start the game. This will begin the game loop and no other code past Start() in your entry point will run. Make sure to set the first scene before executing this. /// </summary> public void Start() { if (!windowSet) { SetWindow(Width, Height, WindowFullscreen); } if (LogExceptionsToFile) { // Dump crashes to a file AppDomain.CurrentDomain.UnhandledException += GlobalUnhandledExceptionHandler; } if (!Active) { Active = true; Init(); UpdateScenes(); gameTime.Start(); while (Window.IsOpen) { if (Active) { Window.DispatchEvents(); frameTime = 1000f / TargetFramerate; skipTime = frameTime * 2; if (ShowDebugger) { Window.SetMouseCursorVisible(true); } else { Window.SetMouseCursorVisible(mouseVisible); } if (HasFocus) { ScreenshotButton.UpdateFirst(); if (ScreenshotButton.Pressed) { Surface.SaveToFile(); } } if (HasFocus && EnableQuitButton) { QuitButton.UpdateFirst(); if (QuitButton.Pressed) { Close(); } } if (HasFocus && EnableAltF4) { if (Keyboard.IsKeyPressed((Keyboard.Key)Key.LAlt) || Keyboard.IsKeyPressed((Keyboard.Key)Key.RAlt)) { if (Keyboard.IsKeyPressed((Keyboard.Key)Key.F4)) { Close(); } } } if (!AlwaysUpdate && !HasFocus) { deltaTime = 0; continue; } Instance = this; if (!ShowDebugger) { if (LockMouse && HasFocus) { Vector2i m = Mouse.GetPosition(Window); int mx, my; mx = (int)Util.Clamp(m.X, LockMousePadding, WindowWidth - LockMousePadding); my = (int)Util.Clamp(m.Y, LockMousePadding, WindowHeight - LockMousePadding); Mouse.SetPosition(new Vector2i(mx, my), Window); } else if (LockMouseCenter && HasFocus) { Vector2i m = Mouse.GetPosition(Window); int mx, my; mx = WindowWidth / 2; my = WindowHeight / 2; MouseDeltaX = m.X - mx; MouseDeltaY = m.Y - my; Input.GameMouseUpdate(MouseDeltaX, MouseDeltaY); Mouse.SetPosition(new Vector2i(mx, my), Window); } } deltaTime += gameTime.ElapsedMilliseconds - lastTime; lastTime = gameTime.ElapsedMilliseconds; fpsTime += deltaTime; UpdateAverageFramerate(); RealDeltaTime = deltaTime; if (deltaTime >= skipTime) { deltaTime = skipTime; } if (FixedFramerate) { DeltaTime = MeasureTimeInFrames ? 1 : 1f / TargetFramerate; while (deltaTime >= frameTime + sleepTime) { UpdateDebugger(); if (!ShowDebugger) { UpdateCount = 0; Input.Update(); if (!Paused) { updateTime.Restart(); Update(); OnUpdate?.Invoke(); updateTime.Stop(); } } deltaTime -= frameTime + sleepTime; sleepTime = 0; } } else { DeltaTime = deltaTime * 0.001f; UpdateDebugger(); if (!ShowDebugger) { UpdateCount = 0; Input.Update(); if (!Paused) { updateTime.Restart(); Update(); OnUpdate?.Invoke(); updateTime.Stop(); } } deltaTime = 0; } RenderCount = 0; if (updatedOnce) { renderTime.Restart(); Render(); renderTime.Stop(); } else { Window.Clear(Color.Black.SFMLColor); Window.Display(); } } } } OnEnd(); }
public override void UpdateFirst() { base.UpdateFirst(); AxisLeft.Enabled = Enabled; AxisRight.Enabled = Enabled; AxisDPad.Enabled = Enabled; Up.Enabled = Enabled; Down.Enabled = Enabled; Left.Enabled = Enabled; Right.Enabled = Enabled; A.Enabled = Enabled; B.Enabled = Enabled; X.Enabled = Enabled; Y.Enabled = Enabled; R1.Enabled = Enabled; R2.Enabled = Enabled; L1.Enabled = Enabled; L2.Enabled = Enabled; Start.Enabled = Enabled; Select.Enabled = Enabled; Home.Enabled = Enabled; AxisLeftClick.Enabled = Enabled; AxisRightClick.Enabled = Enabled; Up.UpdateFirst(); Down.UpdateFirst(); Left.UpdateFirst(); Right.UpdateFirst(); A.UpdateFirst(); B.UpdateFirst(); X.UpdateFirst(); Y.UpdateFirst(); R1.UpdateFirst(); R2.UpdateFirst(); L1.UpdateFirst(); L2.UpdateFirst(); Start.UpdateFirst(); Select.UpdateFirst(); Home.UpdateFirst(); AxisLeftClick.UpdateFirst(); AxisRightClick.UpdateFirst(); AxisLeft.UpdateFirst(); AxisRight.UpdateFirst(); AxisDPad.UpdateFirst(); // The recording and playback code is pretty ugly, sorry :I if (Recording) { foreach (var b in buttonNames) { if (b.Value.Pressed || b.Value.Released) { if (!recordedButtonData.ContainsKey(recordingTimer)) { recordedButtonData.Add(recordingTimer, new Dictionary <string, int>()); } } if (b.Value.Pressed) { recordedButtonData[recordingTimer].Add(b.Key, 1); } if (b.Value.Released) { recordedButtonData[recordingTimer].Add(b.Key, 0); } } if (RecordAxes) { if (AxisLeft.HasInput && (AxisLeft.X != AxisLeft.LastX || AxisLeft.Y != AxisLeft.LastY)) { recordedAxisData[0].Add(recordingTimer, new AxisData { X = AxisLeft.X, Y = AxisLeft.Y }); //Console.WriteLine("Time: " + recordingTimer + " X: " + AxisLeft.X + " Y: " + AxisLeft.Y); } if (AxisRight.HasInput && (AxisRight.X != AxisRight.LastX || AxisRight.Y != AxisRight.LastY)) { recordedAxisData[1].Add(recordingTimer, new AxisData { X = AxisRight.X, Y = AxisRight.Y }); } if (AxisDPad.HasInput && (AxisDPad.X != AxisDPad.LastX || AxisDPad.Y != AxisDPad.LastY)) { recordedAxisData[2].Add(recordingTimer, new AxisData { X = AxisDPad.X, Y = AxisDPad.Y }); } if (AxisTriggers.HasInput && (AxisTriggers.X != AxisTriggers.LastX || AxisTriggers.LastY != 0)) { recordedAxisData[3].Add(recordingTimer, new AxisData { X = AxisTriggers.X, Y = AxisTriggers.Y }); } } recordingTimer++; } if (Playing) { if (playingTimer > playbackMax) { Stop(); } if (playbackButtonData.ContainsKey(playingTimer)) { foreach (var act in playbackButtonData[playingTimer]) { if (act.Value == 0) { buttonNames[act.Key].ForceState(false); //Util.Log("Time: " + playingTimer + " " + act.Key + " Released"); } else { buttonNames[act.Key].ForceState(true); //Util.Log("Time: " + playingTimer + " " + act.Key + " Pressed"); } } } var i = 0; foreach (var a in axes) { if (playbackAxisData[i].ContainsKey(playingTimer)) { a.ForceState(playbackAxisData[i][playingTimer].X, playbackAxisData[i][playingTimer].Y); //Util.Log("Time: " + playingTimer + " X: " + playbackAxisData[i][playingTimer].X + " Y: " + playbackAxisData[i][playingTimer].Y); } i++; } playingTimer++; } }