From 92c0d43169281d5f0b78734289c912cdc2d5ed20 Mon Sep 17 00:00:00 2001 From: Lucas Vulpius Date: Sat, 23 Jan 2021 08:34:38 +0000 Subject: [PATCH] Implemented #896 - Added option for Unlimited FPS --- voxygen/src/hud/mod.rs | 3 ++- voxygen/src/hud/settings_window.rs | 22 ++++++++++++++++++---- voxygen/src/main.rs | 4 ++-- voxygen/src/run.rs | 3 ++- voxygen/src/settings.rs | 28 +++++++++++++++++++++++++--- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 75be629adb..524cecdeb7 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -51,6 +51,7 @@ use crate::{ i18n::{LanguageMetadata, Localization}, render::{Consts, Globals, RenderMode, Renderer}, scene::camera::{self, Camera}, + settings::Fps, ui::{fonts::Fonts, img_ids::Rotations, slot, Graphic, Ingameable, ScaleMode, Ui}, window::{Event as WinEvent, FullScreenSettings, GameInput}, GlobalState, @@ -349,7 +350,7 @@ pub enum Event { AdjustMusicVolume(f32), AdjustSfxVolume(f32), //ChangeAudioDevice(String), - ChangeMaxFPS(u32), + ChangeMaxFPS(Fps), ChangeFOV(u16), ChangeGamma(f32), ChangeExposure(f32), diff --git a/voxygen/src/hud/settings_window.rs b/voxygen/src/hud/settings_window.rs index ecb267623c..3ec8fd7d9c 100644 --- a/voxygen/src/hud/settings_window.rs +++ b/voxygen/src/hud/settings_window.rs @@ -10,6 +10,7 @@ use crate::{ AaMode, CloudMode, FluidMode, LightingMode, RenderMode, ShadowMapMode, ShadowMode, UpscaleMode, }, + settings::Fps, ui::{fonts::Fonts, ImageSlider, ScaleMode, ToggleButton}, window::{FullScreenSettings, FullscreenMode, GameInput}, GlobalState, @@ -27,7 +28,20 @@ use itertools::Itertools; use std::iter::once; use winit::monitor::VideoMode; -const FPS_CHOICES: [u32; 11] = [15, 30, 40, 50, 60, 90, 120, 144, 240, 300, 500]; +const FPS_CHOICES: [Fps; 12] = [ + Fps::Max(15), + Fps::Max(30), + Fps::Max(40), + Fps::Max(50), + Fps::Max(60), + Fps::Max(90), + Fps::Max(120), + Fps::Max(144), + Fps::Max(240), + Fps::Max(300), + Fps::Max(500), + Fps::Unlimited, +]; widget_ids! { struct Ids { @@ -298,7 +312,7 @@ pub enum Event { AdjustMusicVolume(f32), AdjustSfxVolume(f32), //ChangeAudioDevice(String), - MaximumFPS(u32), + MaximumFPS(Fps), CrosshairTransp(f32), CrosshairType(CrosshairType), UiScale(ScaleChange), @@ -1794,7 +1808,7 @@ impl<'a> Widget for SettingsWindow<'a> { .position(|&x| x == self.global_state.settings.graphics.max_fps) .unwrap_or(5), 0, - 10, + FPS_CHOICES.len() - 1, self.imgs.slider_indicator, self.imgs.slider, ) @@ -1808,7 +1822,7 @@ impl<'a> Widget for SettingsWindow<'a> { events.push(Event::MaximumFPS(FPS_CHOICES[which])); } - Text::new(&format!("{}", self.global_state.settings.graphics.max_fps)) + Text::new(&self.global_state.settings.graphics.max_fps.to_string()) .right_from(state.ids.max_fps_slider, 8.0) .font_size(self.fonts.cyri.scale(14)) .font_id(self.fonts.cyri.conrod_id) diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index 16e58b0505..50117191a9 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -9,7 +9,7 @@ use veloren_voxygen::{ logging, profile::Profile, run, - settings::{AudioOutput, Settings}, + settings::{get_fps, AudioOutput, Settings}, window::Window, GlobalState, }; @@ -180,7 +180,7 @@ fn main() { profile, window, clock: Clock::new(std::time::Duration::from_secs_f64( - 1.0 / settings.graphics.max_fps as f64, + 1.0 / get_fps(settings.graphics.max_fps) as f64, )), settings, info_message: None, diff --git a/voxygen/src/run.rs b/voxygen/src/run.rs index 85bf0ea7e5..de26f0c287 100644 --- a/voxygen/src/run.rs +++ b/voxygen/src/run.rs @@ -1,5 +1,6 @@ use crate::{ menu::main::MainMenuState, + settings::get_fps, ui, window::{Event, EventLoop}, Direction, GlobalState, PlayState, PlayStateResult, @@ -177,7 +178,7 @@ fn handle_main_events_cleared( // Wait for the next tick. span!(guard, "Main thread sleep"); global_state.clock.set_target_dt(Duration::from_secs_f64( - 1.0 / global_state.settings.graphics.max_fps as f64, + 1.0 / get_fps(global_state.settings.graphics.max_fps) as f64, )); global_state.clock.tick(); drop(guard); diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index d887c08956..b5192d0b9a 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -8,7 +8,7 @@ use crate::{ use directories_next::UserDirs; use hashbrown::{HashMap, HashSet}; use serde::{Deserialize, Serialize}; -use std::{fs, path::PathBuf}; +use std::{fmt, fs, path::PathBuf}; use tracing::warn; use vek::*; use winit::event::{MouseButton, VirtualKeyCode}; @@ -622,6 +622,28 @@ impl Default for Log { } } +#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq)] +pub enum Fps { + Max(u32), + Unlimited, +} + +pub fn get_fps(max_fps: Fps) -> u32 { + match max_fps { + Fps::Max(x) => x, + Fps::Unlimited => u32::MAX, + } +} + +impl fmt::Display for Fps { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + Fps::Max(x) => write!(f, "{}", x), + Fps::Unlimited => write!(f, "Unlimited"), + } + } +} + /// `GraphicsSettings` contains settings related to framerate and in-game /// visuals. #[derive(Clone, Debug, Serialize, Deserialize)] @@ -631,7 +653,7 @@ pub struct GraphicsSettings { pub sprite_render_distance: u32, pub particles_enabled: bool, pub figure_lod_render_distance: u32, - pub max_fps: u32, + pub max_fps: Fps, pub fov: u16, pub gamma: f32, pub exposure: f32, @@ -649,7 +671,7 @@ impl Default for GraphicsSettings { sprite_render_distance: 100, particles_enabled: true, figure_lod_render_distance: 300, - max_fps: 60, + max_fps: Fps::Max(60), fov: 70, gamma: 1.0, exposure: 1.0,