use std::cell::RefCell;
pub use ecolor::Color32 as Color;
use glam::Vec2;
mod canvas;
mod texture;
mod transform;
pub use canvas::{Canvas, CanvasError, DisplayMode, Viewport, ViewportScaling};
pub use texture::{
LoadError as TextureLoadError, Options as TextureOptions, Origin, ScaleMode, Texture,
TextureSlice,
};
pub use transform::Transform;
thread_local! {
pub(crate) static CANVAS: RefCell<Option<Canvas>> = const { RefCell::new(None) };
}
pub fn with_canvas<T>(f: impl FnOnce(&mut Canvas) -> T) -> T {
CANVAS.with_borrow_mut(|canvas| f(canvas.as_mut().expect("no active renderer")))
}
#[repr(C)]
pub struct Vertex {
pub coord: Vec2,
pub color: Color,
pub uv: Vec2,
}
impl Vertex {
#[must_use]
pub const fn from_xy_uv(coord: Vec2, uv: Vec2) -> Self {
let color = Color::WHITE;
Self { coord, color, uv }
}
}
pub fn clear(color: Color) {
with_canvas(|canvas| canvas.clear(color))
}
pub fn display() {
with_canvas(Canvas::display)
}
pub fn draw<T: Drawable>(object: &T, transform: impl Into<Transform>) {
with_canvas(|canvas| object.draw(canvas, transform.into()))
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a `Drawable` type",
label = "Cannot be drawn"
)]
pub trait Drawable {
fn draw(&self, canvas: &mut Canvas, transform: Transform);
}