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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::{
    driver::Driver,
    event::PixEvent,
    state::{State, StateData},
    PixEngineErr, PixEngineResult,
};
use std::collections::VecDeque;

const FPS_SAMPLE_SIZE: usize = 30;

/// Primary PixEngine object that controls Window and StateData
pub struct PixEngine<S>
where
    S: State,
{
    app_name: String,
    state: S,
    should_close: bool,
    debug: bool,
    data: StateData,
}

impl<S> PixEngine<S>
where
    S: State,
{
    /// Create a new PixEngine instance
    pub fn new(
        app_name: String,
        state: S,
        screen_width: u32,
        screen_height: u32,
        vsync: bool,
    ) -> PixEngineResult<Self> {
        let data = StateData::new(&app_name, screen_width, screen_height, vsync)?;
        Ok(Self {
            app_name,
            state,
            should_close: false,
            debug: false,
            data,
        })
    }
    /// Set a custom window icon
    pub fn set_icon(&mut self, path: &str) -> PixEngineResult<()> {
        self.data.driver.load_icon(path)
    }
    /// Toggle fullscreen
    pub fn fullscreen(&mut self, val: bool) -> PixEngineResult<()> {
        self.data.fullscreen(val)
    }
    /// Toggle vsync
    pub fn vsync(&mut self, val: bool) -> PixEngineResult<()> {
        self.data.vsync(val)
    }
    /// Set audio sample rate
    pub fn set_audio_sample_rate(&mut self, sample_rate: i32) -> PixEngineResult<()> {
        self.data.set_audio_sample_rate(sample_rate)
    }

    /// Starts the engine loop. Will execute until one of on_create, on_update, or on_destroy
    /// returns false or the Window receives a termination event
    pub fn run(&mut self) -> PixEngineResult<()> {
        use std::time::{Duration, Instant};

        if self.data.screen_width() == 0 || self.data.screen_height() == 0 {
            return Err(PixEngineErr::new("invalid screen dimensions"));
        }

        // Create user resources on start up
        match self.state.on_start(&mut self.data) {
            Ok(false) => return Ok(()),
            Err(e) => return Err(e),
            _ => (), // continue on
        }

        // Average FPS if debug enabled
        let mut fps_samples = VecDeque::new();

        // Start main loop
        let main_screen = format!("screen{}", self.data.main_window_id()); // TODO abstract this out
        let mut timer = Instant::now();
        let mut frame_timer = Duration::new(0, 0);
        let mut frame_counter = 0;
        let one_second = Duration::new(1, 0);
        let zero_seconds = Duration::new(0, 0);
        while !self.should_close {
            // Extra loop allows on_destroy to prevent closing
            while !self.should_close {
                let elapsed = timer.elapsed();
                timer = Instant::now();

                let events: Vec<PixEvent> = self.data.driver.poll()?;
                self.data.events.clear();
                for event in events {
                    self.data.events.push(event);
                    match event {
                        PixEvent::Quit | PixEvent::AppTerminating => self.should_close = true,
                        PixEvent::WinClose(window_id) => {
                            if window_id == self.data.main_window_id() {
                                self.should_close = true;
                            } else {
                                self.data.driver.close_window(window_id);
                            }
                        }
                        PixEvent::KeyPress(key, pressed, ..) => {
                            self.data.set_new_key_state(key, pressed);
                        }
                        PixEvent::MousePress(button, .., pressed) => {
                            // TODO add functionality for mouse click coords
                            self.data.set_new_mouse_state(button, pressed);
                        }
                        PixEvent::MouseMotion(x, y) => self.data.update_mouse(x, y),
                        PixEvent::MouseWheel(delta) => self.data.update_mouse_wheel(delta),
                        PixEvent::Focus(_, focused) => self.data.set_focused(focused),
                        _ => (), // Skip anything else
                    }
                }

                self.data.update_key_states();
                self.data.update_mouse_states();

                // Handle user frame updates
                match self.state.on_update(elapsed.as_secs_f32(), &mut self.data) {
                    Ok(false) => self.should_close = true,
                    Err(e) => return Err(e),
                    _ => (), // continue on
                }

                // Display updated frame
                if self.data.default_target_dirty {
                    self.data
                        .copy_draw_target(self.data.main_window_id(), &main_screen)?;
                }
                self.data.driver.present();

                // Update window title and FPS counter
                frame_timer = frame_timer.checked_add(elapsed).unwrap_or(one_second);
                frame_counter += 1;
                if frame_timer >= one_second {
                    frame_timer = frame_timer.checked_sub(one_second).unwrap_or(zero_seconds);
                    if self.debug {
                        fps_samples.push_back(frame_counter);
                        if fps_samples.len() > FPS_SAMPLE_SIZE {
                            let _ = fps_samples.pop_front();
                        }
                    }
                    let mut title = format!("{} - FPS: {}", self.app_name, frame_counter);
                    if !self.data.title().is_empty() {
                        title.push_str(&format!(" - {}", self.data.title()));
                    }
                    self.data
                        .driver
                        .set_title(self.data.main_window_id(), &title)?;
                    frame_counter = 0;
                }
            }

            match self.state.on_stop(&mut self.data) {
                Ok(false) => self.should_close = false,
                Err(e) => return Err(e),
                _ => (), // continue on
            }
        }

        if self.debug {
            let fps_avg = if !fps_samples.is_empty() {
                fps_samples.iter().sum::<u32>() as f32 / fps_samples.len() as f32
            } else {
                0.0
            };
            println!("Average FPS: {}", fps_avg);
        }

        Ok(())
    }
}