micro_games_kit/
config.rs

1use serde::{Deserialize, Serialize};
2use spitfire_glow::app::AppConfig;
3use std::{error::Error, path::Path};
4
5#[derive(Debug, Serialize, Deserialize)]
6pub struct Config {
7    pub width: u32,
8    pub height: u32,
9    pub fullscreen: bool,
10    pub maximized: bool,
11    pub vsync: bool,
12    pub double_buffer: Option<bool>,
13    pub hardware_acceleration: Option<bool>,
14}
15
16impl Default for Config {
17    fn default() -> Self {
18        Self {
19            width: Self::default_width(),
20            height: Self::default_height(),
21            fullscreen: Self::default_fullscreen(),
22            maximized: Default::default(),
23            vsync: Self::default_vsync(),
24            double_buffer: Default::default(),
25            hardware_acceleration: Default::default(),
26        }
27    }
28}
29
30impl Config {
31    fn default_width() -> u32 {
32        1024
33    }
34
35    fn default_height() -> u32 {
36        576
37    }
38
39    fn default_fullscreen() -> bool {
40        true
41    }
42
43    fn default_vsync() -> bool {
44        true
45    }
46
47    pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, Box<dyn Error>> {
48        Self::load_from_str(&std::fs::read_to_string(path).unwrap_or_default())
49    }
50
51    pub fn load_from_str(content: &str) -> Result<Self, Box<dyn Error>> {
52        Ok(toml::from_str(content)?)
53    }
54
55    pub fn to_app_config(&self, name: impl ToString) -> AppConfig {
56        AppConfig {
57            title: name.to_string(),
58            width: self.width,
59            height: self.height,
60            fullscreen: self.fullscreen,
61            maximized: self.maximized,
62            vsync: self.vsync,
63            double_buffer: self.double_buffer,
64            hardware_acceleration: self.hardware_acceleration,
65            color: [0.0, 0.0, 0.0, 0.0],
66            ..Default::default()
67        }
68    }
69}