Skip to main content

libdqg/
game.rs

1use winit::event_loop::EventLoop;
2use crate::scene::Scene;
3
4use crate::app::App;
5use crate::types::Color;
6
7pub struct Game {
8    app: App<'static>,
9}
10
11impl Game {
12    pub(crate) fn new(app: App<'static>) -> Self {
13        Self {
14            app,
15        }
16    }
17
18    pub fn run(&mut self) {
19        let event_loop = EventLoop::new().unwrap();
20        event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
21        match event_loop.run_app(&mut self.app) {
22            Ok(_) => println!("Application exited successfully."),
23            Err(e) => eprintln!("Application error: {:?}", e),
24        }
25    }
26}
27
28pub struct GameBuilder {
29    title: Option<String>,
30    width: Option<u32>,
31    height: Option<u32>,
32    scenes: Vec<Box<dyn Scene>>,
33    integrated_titlebar: bool,
34    clear_color: Color,
35    resizable: bool,
36}
37
38impl GameBuilder {
39    pub fn new(initial_scene: Box<dyn Scene>) -> Self {
40        Self {
41            title: None,
42            width: None,
43            height: None,
44            scenes: vec![initial_scene],
45            integrated_titlebar: true,
46            clear_color: Color::from_hex(0x7391C8),
47            resizable: true,
48        }
49    }
50
51    pub fn title(mut self, title: String) -> Self {
52        self.title = Some(title);
53        self
54    }
55
56    pub fn size(mut self, width: u32, height: u32) -> Self {
57        self.width = Some(width);
58        self.height = Some(height);
59        self
60    }
61
62    pub fn add_scene(mut self, scene: Box<dyn Scene>) -> Self {
63        self.scenes.push(scene);
64        self
65    }
66
67    pub fn integrated_titlebar(mut self, integrated: bool) -> Self {
68        self.integrated_titlebar = integrated;
69        self
70    }
71
72    pub fn resizable(mut self, resizable: bool) -> Self {
73        self.resizable = resizable;
74        self
75    }
76
77    pub fn clear_color(mut self, clear_color: Color) -> Self {
78        self.clear_color = clear_color;
79        self
80    }
81
82    pub fn build(self) -> Game {
83        let title = self.title.unwrap_or_else(|| "libDQG Game".to_string());
84        let width = self.width.unwrap_or(800);
85        let height = self.height.unwrap_or(600);
86
87        let mut app = App::new(title, width, height, self.integrated_titlebar, self.clear_color, self.resizable);
88        for scene in self.scenes {
89            app.add_scene(scene);
90        }
91        Game::new(app)
92    }
93}