pub mod framebuf;
#[cfg(feature = "sdl")]
pub mod sdl;
#[cfg(feature = "sdl-gpu")]
pub mod sdl_gpu;
use crate::draw::texture::Texture;
use crate::types::{Fixed, Rect, Viewport};
pub struct DisplayInfo {
pub width: u16,
pub height: u16,
pub scale: Fixed,
pub format: crate::draw::texture::ColorFormat,
}
impl DisplayInfo {
#[inline]
pub fn viewport(&self) -> Viewport {
let phys_w = (Fixed::from(self.width) * self.scale).to_int().max(0) as u16;
let phys_h = (Fixed::from(self.height) * self.scale).to_int().max(0) as u16;
Viewport::new(phys_w, phys_h, self.scale)
}
}
pub use crate::event::input::{InputEvent, KEY_HW_BUTTON_0, KEY_ROTARY_PRESS};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BackbufferPersistence {
Persistent,
Transient,
}
pub trait Surface {
fn display_info(&self) -> DisplayInfo;
fn flush(&mut self, area: &Rect);
fn poll_event(&mut self) -> Option<InputEvent>;
fn screen_rect(&self) -> Rect {
let info = self.display_info();
Rect::new(0, 0, info.width, info.height)
}
fn physical_size(&self) -> (u32, u32) {
let info = self.display_info();
let pw = (Fixed::from(info.width) * info.scale).to_int().max(0) as u32;
let ph = (Fixed::from(info.height) * info.scale).to_int().max(0) as u32;
(pw, ph)
}
fn persistence(&self) -> BackbufferPersistence {
BackbufferPersistence::Persistent
}
}
#[inline]
pub(crate) fn logical_from_physical(phys_w: u16, phys_h: u16, scale: Fixed) -> (u16, u16) {
if scale <= Fixed::ZERO {
return (phys_w, phys_h);
}
let lw = (Fixed::from(phys_w) / scale).to_int().max(0) as u16;
let lh = (Fixed::from(phys_h) / scale).to_int().max(0) as u16;
(lw, lh)
}
pub trait FramebufferAccess: Surface {
fn framebuffer(&mut self) -> Texture<'_>;
}
#[cfg(test)]
mod tests {
use super::*;
struct NoOpBackend;
impl Surface for NoOpBackend {
fn display_info(&self) -> DisplayInfo {
DisplayInfo {
width: 1,
height: 1,
scale: Fixed::ONE,
format: crate::draw::texture::ColorFormat::RGBA8888,
}
}
fn flush(&mut self, _area: &Rect) {}
fn poll_event(&mut self) -> Option<InputEvent> {
None
}
}
#[test]
fn default_persistence_is_persistent() {
let b = NoOpBackend;
assert_eq!(b.persistence(), BackbufferPersistence::Persistent);
}
}