tear8 0.1.78

Tear8 is a Rust library that enables you to create your own games or fantasy consoles using Winit and Pixels.
Documentation
use std::time::Duration;

use winit::event::VirtualKeyCode;
use winit_input_helper::WinitInputHelper;

use crate::{color::Color, program::{DeltaTime, Fps}, resources::Buffer, resources::Renderable};

/// All base mouse buttons
pub enum MouseButton {
    Left,
    Right,
    Middle,
    Other(u16),
}

/// A struct for main program cmd
/// 
/// # Example
/// ```no_run 
/// cmd.render(&mut self.sprite)
/// 
/// if cmd.key_pressed(Key::Escape) {
///     cmd.quit_program()
/// }
/// ```
pub struct Commands {
    pub(crate) buffer: Buffer,
    pub(crate) winit_input: WinitInputHelper,
    pub(crate) quit: bool,
    fps: Fps,
    delta: DeltaTime,
}

impl Commands {
    /// Create new commands struct
    pub(crate) fn new(buffer: Buffer, fps: Fps, delta: DeltaTime) -> Self {
        let winit_input = WinitInputHelper::new();
        
        Self {
            buffer, winit_input,
            quit: false,
            fps, delta
        }
    }
    // # Windowing
    /// Interrupts the program by closing the window
    pub fn quit_program(&mut self) { self.quit = true; }
    /// Check if program is quitting
    pub fn program_is_quit(&mut self) { self.winit_input.close_requested(); }
    // # Keyboard
    // Controlla se viene premuto un tasto della tastiera
    /// Check if the keyboard key is pressed
    pub fn key_pressed(&self, key: VirtualKeyCode) -> bool { self.winit_input.key_held(key) }
    // Controlla se viene rilasciato un tasto della tastiera
    /// Check if the keyboard key is released
    pub fn key_released(&self, key: VirtualKeyCode) -> bool { self.winit_input.key_released(key) }
    // Converts mouse button to number
    fn mouse_button_to_number(&self, button: MouseButton) -> u16 {
        match button { 
            MouseButton::Left => 0, MouseButton::Right => 1, 
            MouseButton::Middle => 2, MouseButton::Other(num) => num, 
        }
    }
    // # Mouse
    // Controlla se viene premuto un tasto del mouse
    /// Check if the mouse button is pressed
    pub fn mouse_pressed(&self, button: MouseButton) -> bool { 
        let button = self.mouse_button_to_number(button);
        self.winit_input.mouse_held(button as usize)
    }
    // Controlla se viene rilasciato un tasto del mouse
    /// Check if the mouse button is released
    pub fn mouse_released(&self, button: MouseButton) -> bool { 
        let button = self.mouse_button_to_number(button);
        self.winit_input.mouse_released(button as usize) 
    }
    /// Get mouse position
    pub fn mouse_position(&self) -> Option<(f32, f32)> { self.winit_input.mouse() }
    // Return delta time 
    pub fn delta_time(&mut self) -> Duration { self.delta.get() }
    // Return fps
    pub fn fps(&mut self) -> f32 { self.fps.get() }
    /// A function for clear the buffer 
    pub fn clear_buffer(&mut self, color: Color) {
        for pixel in self.buffer.raw_buffer().chunks_mut(4) {
            pixel[0] = color.r; // R 
            pixel[1] = color.g; // G
            pixel[2] = color.b; // B 
            pixel[3] = color.a; // A
        }
    }
    /// Render resource on the screen
    /// # Example
    /// ```no_run 
    /// cmd.render(&mut res) 
    /// ```
    pub fn render<R: Renderable>(&mut self, resource: &mut R) -> &mut Self { resource.render(&mut self.buffer); self }
}