Skip to content

Commit

Permalink
Add option for configuring fps limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Sep 18, 2024
1 parent 8bb27f4 commit 5894223
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
bevy = { version = "0.14.2", features = ["wav"] }
bevy_dev_tools = "0.14.2"
bevy_framepace = "0.17.1"
rand = "0.8.5"
num-traits = "0.2"
num-derive = "0.4"
Expand Down
39 changes: 39 additions & 0 deletions src/game_option_menu/fps_limiter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::time::Duration;

use bevy::prelude::*;
use num_traits::FromPrimitive;

#[derive(Default, Clone, Copy, FromPrimitive, Resource)]
pub enum FPSLimiter {
#[default]
Auto,
Unlimited,
F60,
F144,
F240,
F360,
F480,
}

impl FPSLimiter {
pub fn enum_prev(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 - 1).map(|n| std::mem::replace(self, n))
}

pub fn enum_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
}

pub fn get_limiter(&self) -> bevy_framepace::Limiter {
let ft = |fps| Duration::from_secs_f32(1.0 / fps as f32);
match self {
FPSLimiter::Auto => bevy_framepace::Limiter::Auto,
FPSLimiter::Unlimited => bevy_framepace::Limiter::Off,
FPSLimiter::F60 => bevy_framepace::Limiter::Manual(ft(60)),
FPSLimiter::F144 => bevy_framepace::Limiter::Manual(ft(144)),
FPSLimiter::F240 => bevy_framepace::Limiter::Manual(ft(240)),
FPSLimiter::F360 => bevy_framepace::Limiter::Manual(ft(360)),
FPSLimiter::F480 => bevy_framepace::Limiter::Manual(ft(480)),
}
}
}
1 change: 1 addition & 0 deletions src/game_option_menu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod fps_limiter;
pub mod plugin;
pub mod transform;
64 changes: 61 additions & 3 deletions src/game_option_menu/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use bevy::window::WindowMode;

use super::transform::GameOptionMenuTransform;

#[cfg(not(target_arch = "wasm32"))]
use super::fps_limiter::FPSLimiter;

pub fn setup(app: &mut App) {
app.insert_resource(GameOptionMenuTransform::default())
.insert_resource(GameOptionMenuData::default())
Expand Down Expand Up @@ -55,13 +58,15 @@ enum GameOptionMenuSelection {
BlankLine3,
ScaleFactor,
#[cfg(not(target_arch = "wasm32"))]
FPSLimiter,
#[cfg(not(target_arch = "wasm32"))]
WindowMode,
}

impl GameOptionMenuSelection {
pub fn iter() -> std::slice::Iter<'static, GameOptionMenuSelection> {
#[cfg(not(target_arch = "wasm32"))]
type ArrayType = [GameOptionMenuSelection; 13];
type ArrayType = [GameOptionMenuSelection; 14];
#[cfg(target_arch = "wasm32")]
type ArrayType = [GameOptionMenuSelection; 12];
const STATES: ArrayType = [
Expand All @@ -78,6 +83,8 @@ impl GameOptionMenuSelection {
GameOptionMenuSelection::BlankLine3,
GameOptionMenuSelection::ScaleFactor,
#[cfg(not(target_arch = "wasm32"))]
GameOptionMenuSelection::FPSLimiter,
#[cfg(not(target_arch = "wasm32"))]
GameOptionMenuSelection::WindowMode,
];
STATES.iter()
Expand All @@ -91,6 +98,8 @@ struct GameOptionMenuData {
linecap: Linecap,
drop_speed: DropSpeed,
#[cfg(not(target_arch = "wasm32"))]
fps_limiter: FPSLimiter,
#[cfg(not(target_arch = "wasm32"))]
window_mode: WindowMode,
}

Expand All @@ -102,6 +111,8 @@ impl GameOptionMenuData {
linecap: Linecap::default(),
drop_speed: DropSpeed::default(),
#[cfg(not(target_arch = "wasm32"))]
fps_limiter: FPSLimiter::default(),
#[cfg(not(target_arch = "wasm32"))]
window_mode: WindowMode::Windowed,
}
}
Expand Down Expand Up @@ -313,6 +324,19 @@ fn update_ui_system(
}
}
#[cfg(not(target_arch = "wasm32"))]
GameOptionMenuSelection::FPSLimiter => {
text.sections[0].value = fname("FPS LIMITER", NameKind::Option);
match game_option_menu_data.fps_limiter {
FPSLimiter::Auto => text.sections[1].value = fopt("AUTO", false, true),
FPSLimiter::Unlimited => text.sections[1].value = fopt("UNLIMITED", true, true),
FPSLimiter::F60 => text.sections[1].value = fopt("60 FPS", true, true),
FPSLimiter::F144 => text.sections[1].value = fopt("144 FPS", true, true),
FPSLimiter::F240 => text.sections[1].value = fopt("240 FPS", true, true),
FPSLimiter::F360 => text.sections[1].value = fopt("360 FPS", true, true),
FPSLimiter::F480 => text.sections[1].value = fopt("480 FPS", true, false),
}
}
#[cfg(not(target_arch = "wasm32"))]
GameOptionMenuSelection::WindowMode => {
text.sections[0].value = fname("WINDOW MODE", NameKind::Option);
match game_option_menu_data.window_mode {
Expand All @@ -339,6 +363,9 @@ fn handle_input_system(
mut app_state: ResMut<NextState<AppState>>,
mut e_play_sound: EventWriter<PlaySoundEvent>,
mut scale_factor: ResMut<ScaleFactor>,
#[cfg(not(target_arch = "wasm32"))] mut framepace_settins: ResMut<
bevy_framepace::FramepaceSettings,
>,
#[cfg(not(target_arch = "wasm32"))] mut query: Query<&mut Window>,
) {
let player_inputs = PlayerInputs::with_keyboard(&keys)
Expand All @@ -360,6 +387,8 @@ fn handle_input_system(
let mut option_changed = false;
let mut scale_changed = false;
#[cfg(not(target_arch = "wasm32"))]
let mut fps_changed = false;
#[cfg(not(target_arch = "wasm32"))]
let mut window_mode_changed = false;

match game_option_menu_data.selection {
Expand Down Expand Up @@ -474,7 +503,7 @@ fn handle_input_system(
} else if player_inputs.down.0 {
#[cfg(not(target_arch = "wasm32"))]
{
game_option_menu_data.selection = GameOptionMenuSelection::WindowMode;
game_option_menu_data.selection = GameOptionMenuSelection::FPSLimiter;
}
#[cfg(target_arch = "wasm32")]
{
Expand All @@ -494,10 +523,30 @@ fn handle_input_system(
}
}
#[cfg(not(target_arch = "wasm32"))]
GameOptionMenuSelection::WindowMode => {
GameOptionMenuSelection::FPSLimiter => {
if player_inputs.up.0 {
game_option_menu_data.selection = GameOptionMenuSelection::ScaleFactor;
selection_changed = true;
} else if player_inputs.down.0 {
game_option_menu_data.selection = GameOptionMenuSelection::WindowMode;
selection_changed = true;
}

if player_inputs.right.0 {
if let Some(_) = game_option_menu_data.fps_limiter.enum_next() {
fps_changed = true;
}
} else if player_inputs.left.0 {
if let Some(_) = game_option_menu_data.fps_limiter.enum_prev() {
fps_changed = true;
}
}
}
#[cfg(not(target_arch = "wasm32"))]
GameOptionMenuSelection::WindowMode => {
if player_inputs.up.0 {
game_option_menu_data.selection = GameOptionMenuSelection::FPSLimiter;
selection_changed = true;
} else if player_inputs.down.0 {
game_option_menu_data.selection = GameOptionMenuSelection::Tetris;
selection_changed = true;
Expand Down Expand Up @@ -526,6 +575,15 @@ fn handle_input_system(
}
option_changed |= scale_changed;
#[cfg(not(target_arch = "wasm32"))]
{
if fps_changed {
*framepace_settins = bevy_framepace::FramepaceSettings {
limiter: game_option_menu_data.fps_limiter.get_limiter(),
};
}
option_changed |= fps_changed;
}
#[cfg(not(target_arch = "wasm32"))]
{
if window_mode_changed {
let mut window = query.single_mut();
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn main() {
},
},
})
.add_plugins(bevy_framepace::FramepacePlugin)
.insert_resource(ClearColor(Color::BLACK)) // application background color
.init_state::<AppState>()
.add_systems(Startup, setup_camera)
Expand Down

0 comments on commit 5894223

Please sign in to comment.