/// <summary> /// This method runs on every frame /// </summary> private void GameLoop() { // Close program with esc if (InputDevice.Keyboard.IsKeyDown(VirtualKeyCode.ESCAPE)) { renderForm.Dispose(); } stopwatch.Restart(); // Execute all Update methods StaticUpdater.ExecuteUpdateActions(lastDeltaTime); // Render on each frame renderDevice.Draw(); foreach (GameObject gameObject in Scene.CurrentScene.GameObjects) { // Execute updates per-object foreach (IComponent component in gameObject.Components) { component.Update(lastDeltaTime); } } // TODO create separate physics loop PhysicsHandler.Simulation.Timestep(lastDeltaTime); stopwatch.Stop(); lastDeltaTime = (float)stopwatch.Elapsed.TotalSeconds; elapsedTime += lastDeltaTime; }
/// <summary> /// /// </summary> public Engine(AutoUpdateable gameLogic) { { // Init window renderForm = new RenderForm("RaymarchEngine"); renderForm.AutoSize = false; renderForm.ClientSize = new Size( Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height ); renderForm.AllowUserResizing = false; renderForm.IsFullscreen = isFullscreen; renderForm.StartPosition = FormStartPosition.Manual; renderForm.Location = new Point(0, 0); renderForm.WindowState = FormWindowState.Maximized; renderForm.MinimizeBox = false; renderForm.Show(); } this.gameLogic = gameLogic; RaymarchRenderer.Init(); // Create main scene Scene.CurrentScene = new Scene(); // Start physics library physics = new PhysicsHandler(PhysicsReady); // Init input device InputDevice.Init(renderForm); int unixTime = Util.ConvertToUnixTimestamp(DateTime.Now); // Execute all start methods StaticUpdater.ExecuteStartActions(unixTime); // Execute each scene object's components' Start method foreach (GameObject gameObject in Scene.CurrentScene.GameObjects) { foreach (IComponent component in gameObject.Components) { component.Start(unixTime); } } // It's important that render device is created after scene and game logic start renderDevice = new RenderDevice(renderForm, new Resolution(2560, 1440)); // Start stopwatch for deltaTime stopwatch = new Stopwatch(); stopwatch.Start(); }
/// <summary> /// Called on program close /// </summary> public void Dispose() { int unixTime = Util.ConvertToUnixTimestamp(DateTime.Now); // Execute all end methods StaticUpdater.ExecuteEndActions(unixTime); // Execute each scene GameObject's end methods foreach (GameObject gameObject in Scene.CurrentScene.GameObjects) { foreach (IComponent component in gameObject.Components) { component.End(unixTime); } } renderForm.Dispose(); }