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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::{
    driver::{self, Driver, DriverOpts},
    event::{Input, Key, Mouse, PixEvent},
    pixel::{self, Pixel},
    sprite::Sprite,
    PixEngineErr, PixEngineResult,
};

pub mod draw;
pub mod transform;

pub trait State {
    fn on_start(&mut self, _data: &mut StateData) -> PixEngineResult<bool> {
        Ok(true)
    }
    fn on_stop(&mut self, _data: &mut StateData) -> PixEngineResult<bool> {
        Ok(true)
    }
    fn on_update(&mut self, _elapsed: f32, _data: &mut StateData) -> PixEngineResult<bool> {
        Err(PixEngineErr::new("on_update must be implemented"))
    }
}

/// Pixel blending mode
///   Normal: Ignores alpha channel blending
///   Mask: Only displays pixels if alpha == 255
///   Blend: Blends together alpha channels
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum AlphaMode {
    Normal, // Ignore alpha channel
    Mask,   // Only blend alpha if less than 255
    Blend,  // Always blend alpha
}

/// Manages all engine state including graphics and inputs
// TODO add stroke for line drawing
pub struct StateData {
    pub(super) default_target_dirty: bool,
    #[cfg(all(feature = "sdl2-driver", not(feature = "wasm-driver")))]
    pub(super) driver: driver::sdl2::Sdl2Driver,
    #[cfg(all(feature = "wasm-driver", not(feature = "sdl2-driver")))]
    pub(super) driver: driver::wasm::WasmDriver,
    pub(super) events: Vec<PixEvent>,
    main_window: u32,
    title: String,
    screen_width: u32,
    screen_height: u32,
    default_draw_target: Sprite,
    draw_target: Option<*mut Sprite>,
    default_draw_color: Pixel,
    draw_color: Pixel,
    draw_scale: u32,
    alpha_mode: AlphaMode,
    blend_factor: f32,
    mouse_x: i32,
    mouse_y: i32,
    mouse_wheel_delta: i32,
    font: Sprite,
    has_input_focus: bool,
    old_key_state: [bool; 256],
    new_key_state: [bool; 256],
    key_state: [Input; 256],
    old_mouse_state: [bool; 5],
    new_mouse_state: [bool; 5],
    mouse_state: [Input; 5],
    coord_wrapping: bool,
}

impl StateData {
    /// Engine attributes ======================================================

    pub fn main_window(&self) -> u32 {
        self.main_window
    }
    /// Custom title to append in the window
    pub fn title(&self) -> &str {
        &self.title
    }
    /// Set a custom title to append
    pub fn set_title(&mut self, title: &str) {
        self.title = title.to_string();
    }
    /// Toggle fullscreen
    pub fn fullscreen(&mut self, val: bool) -> PixEngineResult<()> {
        self.driver.fullscreen(self.main_window, val)
    }
    /// Toggle vsync
    pub fn vsync(&mut self, val: bool) -> PixEngineResult<()> {
        self.driver.vsync(self.main_window, val)
    }
    /// Screen Width
    pub fn screen_width(&self) -> u32 {
        self.screen_width
    }
    /// Screen Height
    pub fn screen_height(&self) -> u32 {
        self.screen_height
    }
    /// Change screen dimensions.
    pub fn set_screen_size(&mut self, width: u32, height: u32) -> PixEngineResult<()> {
        let mut new_draw_target = Sprite::new(width, height);
        for x in 0..std::cmp::min(width, self.screen_width) {
            for y in 0..std::cmp::min(width, self.screen_height) {
                let p = self.default_draw_target.get_pixel(x, y);
                new_draw_target.put_pixel(x, y, p);
            }
        }
        self.default_draw_target = new_draw_target;
        self.screen_width = width;
        self.screen_height = height;
        self.driver.set_size(self.main_window, width, height)
    }
    /// Whether window has focus
    pub fn is_focused(&self) -> bool {
        self.has_input_focus
    }
    /// Get the state of a specific keyboard button
    pub fn get_key(&self, key: Key) -> Input {
        self.key_state[key as usize]
    }
    /// Get the state of a specific mouse buton
    pub fn get_mouse(&self, button: Mouse) -> Input {
        self.mouse_state[button as usize]
    }
    /// Get Mouse X-coord in screen space
    pub fn get_mouse_x(&self) -> i32 {
        self.mouse_x
    }
    /// Get Mouse Y-coord in screen space
    pub fn get_mouse_y(&self) -> i32 {
        self.mouse_y
    }
    /// Get Mouse wheel data
    pub fn get_mouse_wheel(&self) -> i32 {
        self.mouse_wheel_delta
    }
    pub fn poll(&mut self) -> Vec<PixEvent> {
        self.events.drain(..).collect()
    }

    /// Utility functions ======================================================

    /// Collision detection for basic circle shapes
    /// Detects whether point (x, y) lies inside circle of radius r located at (cx, cy)
    pub fn is_inside_circle(&self, cx: f32, cy: f32, r: f32, x: f32, y: f32) -> bool {
        ((x - cx).powf(2.0) + (y - cy).powf(2.0)).sqrt() < r
    }
}

impl StateData {
    pub(super) fn new(
        app_name: &String,
        screen_width: u32,
        screen_height: u32,
        vsync: bool,
    ) -> PixEngineResult<Self> {
        let font = StateData::construct_font();
        // Initialize backend driver library
        let opts = DriverOpts::new(app_name, screen_width, screen_height, vsync);
        let driver = driver::load_driver(opts)?;
        let main_window = driver.window_id();
        let state_data = Self {
            default_target_dirty: false,
            driver,
            events: Vec::new(),
            main_window,
            title: String::new(),
            screen_width,
            screen_height,
            default_draw_target: Sprite::new(screen_width, screen_height),
            draw_target: None,
            default_draw_color: pixel::WHITE,
            draw_color: pixel::WHITE,
            draw_scale: 1,
            alpha_mode: AlphaMode::Normal,
            blend_factor: 1.0,
            mouse_x: 0,
            mouse_y: 0,
            mouse_wheel_delta: 0,
            has_input_focus: true,
            font,
            old_key_state: [false; 256],
            new_key_state: [false; 256],
            key_state: [Input::new(); 256],
            old_mouse_state: [false; 5],
            new_mouse_state: [false; 5],
            mouse_state: [Input::new(); 5],
            coord_wrapping: false,
        };
        Ok(state_data)
    }
    pub(super) fn set_focused(&mut self, val: bool) {
        self.has_input_focus = val;
    }
    pub(super) fn update_mouse(&mut self, x: i32, y: i32) {
        self.mouse_x = x;
        self.mouse_y = y;
    }
    pub(super) fn update_mouse_wheel(&mut self, delta: i32) {
        self.mouse_wheel_delta += delta;
    }
    pub(super) fn set_new_mouse_state(&mut self, button: Mouse, pressed: bool) {
        self.new_mouse_state[button as usize] = pressed;
    }
    pub(super) fn update_mouse_states(&mut self) {
        for i in 0..self.mouse_state.len() {
            self.mouse_state[i].pressed = false;
            self.mouse_state[i].released = false;
            if self.new_mouse_state[i] != self.old_mouse_state[i] {
                if self.new_mouse_state[i] {
                    self.mouse_state[i].pressed = !self.mouse_state[i].held;
                    self.mouse_state[i].held = true;
                } else {
                    self.mouse_state[i].released = true;
                    self.mouse_state[i].held = false;
                }
            }
            self.old_mouse_state[i] = self.new_mouse_state[i];
        }
    }
    pub(super) fn set_new_key_state(&mut self, key: Key, pressed: bool) {
        self.new_key_state[key as usize] = pressed;
    }
    pub(super) fn update_key_states(&mut self) {
        for i in 0..self.key_state.len() {
            self.key_state[i].pressed = false;
            self.key_state[i].released = false;
            if self.new_key_state[i] != self.old_key_state[i] {
                if self.new_key_state[i] {
                    self.key_state[i].pressed = !self.key_state[i].held;
                    self.key_state[i].held = true;
                } else {
                    self.key_state[i].released = true;
                    self.key_state[i].held = false;
                }
            }
            self.old_key_state[i] = self.new_key_state[i];
        }
    }
}