actually apply the preferences from the .toml file

This commit is contained in:
yuni 2024-04-30 23:41:37 +02:00
parent c56ae18f5a
commit 3a0b3dc1c3
3 changed files with 31 additions and 15 deletions

View file

@ -11,7 +11,7 @@
# User preferences for the game OutFly. # User preferences for the game OutFly.
# See https://codeberg.org/hut/outfly # See https://codeberg.org/hut/outfly
# fullscreen_mode may be "borderless", "true", or "sized" # fullscreen_mode may be "borderless", "legacy", or "sized"
fullscreen_mode = "borderless" fullscreen_mode = "borderless"
# window_mode may be "windowed", or "fullscreen" # window_mode may be "windowed", or "fullscreen"

View file

@ -45,9 +45,11 @@ Options:
Note: borderless fullscreen is the default, but it crashes on some systems."; Note: borderless fullscreen is the default, but it crashes on some systems.";
fn main() { fn main() {
let prefs = var::load_prefs();
let mut opt = CommandLineOptions { let mut opt = CommandLineOptions {
window_mode_fullscreen: WindowMode::BorderlessFullscreen, window_mode_fullscreen: prefs.get_fullscreen_mode(),
window_mode_initial: WindowMode::BorderlessFullscreen, window_mode_initial: prefs.get_window_mode(),
use_gl: prefs.render_mode_is_gl(),
}; };
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
@ -67,7 +69,7 @@ fn main() {
return; return;
} }
else if arg == "--gl" { else if arg == "--gl" {
env::set_var("WGPU_BACKEND", "gl"); opt.use_gl = true;
} }
else if arg == "--windowed" { else if arg == "--windowed" {
opt.window_mode_initial = WindowMode::Windowed; opt.window_mode_initial = WindowMode::Windowed;
@ -93,6 +95,10 @@ fn main() {
} }
} }
} }
if opt.use_gl {
env::set_var("WGPU_BACKEND", "gl");
}
let mut app = App::new(); let mut app = App::new();
app.insert_resource(opt); app.insert_resource(opt);
@ -101,7 +107,9 @@ fn main() {
mode: bevy_embedded_assets::PluginMode::ReplaceDefault mode: bevy_embedded_assets::PluginMode::ReplaceDefault
}); });
app.add_plugins(OutFlyPlugin).run(); app.add_plugins(OutFlyPlugin);
app.insert_resource(prefs);
app.run();
} }
pub struct OutFlyPlugin; pub struct OutFlyPlugin;
@ -127,8 +135,6 @@ impl Plugin for OutFlyPlugin {
skeleton::SkeletonPlugin, skeleton::SkeletonPlugin,
world::WorldPlugin, world::WorldPlugin,
)); ));
var::load_prefs();
} }
} }
@ -136,6 +142,7 @@ impl Plugin for OutFlyPlugin {
pub struct CommandLineOptions { pub struct CommandLineOptions {
window_mode_fullscreen: WindowMode, window_mode_fullscreen: WindowMode,
window_mode_initial: WindowMode, window_mode_initial: WindowMode,
use_gl: bool,
} }
fn setup( fn setup(

View file

@ -11,6 +11,7 @@
// This module manages variables, settings, as well as evaluating // This module manages variables, settings, as well as evaluating
// "if"-conditions in chats. // "if"-conditions in chats.
use bevy::window::WindowMode;
use bevy::prelude::*; use bevy::prelude::*;
use std::collections::HashMap; use std::collections::HashMap;
use serde::Deserialize; use serde::Deserialize;
@ -30,7 +31,7 @@ pub const TOKEN_NEGATE: &str = "~";
pub const DEFAULT_CHAT_SPEED: f32 = 10.0; pub const DEFAULT_CHAT_SPEED: f32 = 10.0;
#[derive(Resource, Deserialize, Debug)] #[derive(Resource, Deserialize, Debug, Default)]
#[serde(default)] #[serde(default)]
pub struct Preferences { pub struct Preferences {
pub fullscreen_mode: String, pub fullscreen_mode: String,
@ -41,15 +42,23 @@ pub struct Preferences {
pub source_file: Option<String>, pub source_file: Option<String>,
} }
impl Default for Preferences { impl Preferences {
fn default() -> Self { pub fn get_fullscreen_mode(&self) -> WindowMode {
Self { match self.fullscreen_mode.as_str() {
fullscreen_mode: "borderless".to_string(), "legacy" => WindowMode::Fullscreen,
window_mode: "fullscreen".to_string(), "sized" => WindowMode::SizedFullscreen,
render_mode: "vulkan".to_string(), _ => WindowMode::BorderlessFullscreen,
source_file: None,
} }
} }
pub fn get_window_mode(&self) -> WindowMode {
match self.window_mode.as_str() {
"fullscreen" => self.get_fullscreen_mode(),
_ => WindowMode::Windowed,
}
}
pub fn render_mode_is_gl(&self) -> bool {
return self.render_mode == "gl";
}
} }
fn file_is_readable(file_path: &str) -> bool { fn file_is_readable(file_path: &str) -> bool {