roast2d_internal 0.3.6

Roast2D internal crate
Documentation
use std::fmt::Debug;

use anyhow::Result;
use glam::UVec2;

use crate::{
    engine::{Engine, Scene},
    platform::platform_run,
    render::ScaleMode,
};

type SetupFn = Box<dyn FnOnce(&mut Engine)>;

pub struct App {
    pub title: String,
    pub window: UVec2,
    pub vsync: bool,
    pub resizable: bool,
    pub fullscreen: bool,
    pub cursor_visible: bool,
    pub(crate) setup: Option<SetupFn>,
    pub(crate) scale_mode: Option<ScaleMode>,
    pub(crate) scene: Option<Box<dyn Scene>>,
}

impl Debug for App {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "App {{ title: {}, window: {:?}, vsync: {}, resizable: {}, fullscreen: {} }}",
            self.title, self.window, self.vsync, self.resizable, self.fullscreen,
        )
    }
}
impl Default for App {
    fn default() -> Self {
        Self {
            title: "Hello Roast2D".to_string(),
            window: UVec2::new(800, 600),
            vsync: true,
            resizable: false,
            fullscreen: false,
            cursor_visible: true,
            setup: None,
            scale_mode: None,
            scene: None,
        }
    }
}

impl App {
    pub fn title(mut self, title: String) -> Self {
        self.title = title;
        self
    }

    pub fn window(mut self, window: UVec2) -> Self {
        self.window = window;
        self
    }

    pub fn vsync(mut self, vsync: bool) -> Self {
        self.vsync = vsync;
        self
    }

    pub fn resizable(mut self, resizable: bool) -> Self {
        self.resizable = resizable;
        self
    }

    pub fn fullscreen(mut self, fullscreen: bool) -> Self {
        self.fullscreen = fullscreen;
        self
    }

    pub fn cursor_visible(mut self, cursor_visible: bool) -> Self {
        self.cursor_visible = cursor_visible;
        self
    }

    /// Set the scale mode for the game
    pub fn scale_mode(mut self, scale_mode: ScaleMode) -> Self {
        self.scale_mode = Some(scale_mode);
        self
    }

    /// Set the initial scene for the game
    pub fn scene<S: Scene + 'static>(mut self, scene: S) -> Self {
        self.scene = Some(Box::new(scene));
        self
    }

    /// Set a custom setup function for additional initialization
    pub fn setup<Setup: FnOnce(&mut Engine) + 'static>(mut self, setup: Setup) -> Self {
        self.setup = Some(Box::new(setup));
        self
    }

    /// Run the game
    pub fn run(self) -> Result<()> {
        platform_run(self)
    }
}