micro_games_kit/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use serde::{Deserialize, Serialize};
use spitfire_glow::app::AppConfig;
use std::{error::Error, path::Path};

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    pub width: u32,
    pub height: u32,
    pub fullscreen: bool,
    pub maximized: bool,
    pub vsync: bool,
    pub double_buffer: Option<bool>,
    pub hardware_acceleration: Option<bool>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            width: Self::default_width(),
            height: Self::default_height(),
            fullscreen: Self::default_fullscreen(),
            maximized: Default::default(),
            vsync: Self::default_vsync(),
            double_buffer: Default::default(),
            hardware_acceleration: Default::default(),
        }
    }
}

impl Config {
    fn default_width() -> u32 {
        1024
    }

    fn default_height() -> u32 {
        576
    }

    fn default_fullscreen() -> bool {
        true
    }

    fn default_vsync() -> bool {
        true
    }

    pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, Box<dyn Error>> {
        Self::load_from_str(&std::fs::read_to_string(path).unwrap_or_default())
    }

    pub fn load_from_str(content: &str) -> Result<Self, Box<dyn Error>> {
        Ok(toml::from_str(content)?)
    }

    pub fn to_app_config(&self, name: impl ToString) -> AppConfig {
        AppConfig {
            title: name.to_string(),
            width: self.width,
            height: self.height,
            fullscreen: self.fullscreen,
            maximized: self.maximized,
            vsync: self.vsync,
            double_buffer: self.double_buffer,
            hardware_acceleration: self.hardware_acceleration,
            color: [0.0, 0.0, 0.0, 0.0],
            ..Default::default()
        }
    }
}