soul-terminal-render 0.1.0

GPU rendering backend for soul-terminal (wgpu)
Documentation
use soul_terminal_core::{Color, RenderCommand};

/// Errors from the render backend.
#[derive(Debug)]
pub enum RenderError {
    SurfaceError(String),
    DeviceError(String),
    ShaderError(String),
    TextureError(String),
}

impl std::fmt::Display for RenderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SurfaceError(e) => write!(f, "surface error: {e}"),
            Self::DeviceError(e) => write!(f, "device error: {e}"),
            Self::ShaderError(e) => write!(f, "shader error: {e}"),
            Self::TextureError(e) => write!(f, "texture error: {e}"),
        }
    }
}

impl std::error::Error for RenderError {}

/// Trait for render backends. WgpuBackend is the primary implementation.
pub trait RenderBackend {
    /// Begin a new frame with the given clear color.
    fn begin_frame(&mut self, clear_color: Color) -> Result<(), RenderError>;

    /// Submit render commands for the current frame.
    fn submit(&mut self, commands: &[RenderCommand]) -> Result<(), RenderError>;

    /// Present the frame to the screen.
    fn present(&mut self) -> Result<(), RenderError>;

    /// Resize the render surface.
    fn resize(&mut self, width: u32, height: u32);

    /// Load a texture from RGBA bytes, returns a texture ID.
    fn load_texture(&mut self, width: u32, height: u32, data: &[u8]) -> Result<u64, RenderError>;

    /// Get the current surface size.
    fn size(&self) -> (u32, u32);

    /// Get the current scale factor.
    fn scale_factor(&self) -> f32;
}