#![deny(clippy::all)]
pub mod color;
pub mod drawing;
pub mod image;
pub mod math;
pub mod scaling;
pub mod text;
use crate::drawing::PixelWrapper;
use pixels::{Pixels, SurfaceTexture};
use thiserror::Error;
use winit::dpi::LogicalSize;
use winit::event_loop::EventLoop;
use winit::window::{Window, WindowBuilder};
#[derive(Error, Debug)]
pub enum GraphicsError {
#[error("Creating a window: {0}")]
WindowInit(String),
#[error("Initialising Pixels: {0}")]
PixelsInit(#[source] pixels::Error),
#[error("Saving window pref: {0}")]
SavingWindowPref(String),
#[error("Loading window pref: {0}")]
LoadingWindowPref(String),
#[error("Invalid pixel array length, expected: {0}, found: {1}")]
ImageInitSize(usize, usize),
#[error("Both images must be the same size, expected: {0}x{1}, found: {2}x{3}")]
ImageBlendSize(usize, usize, usize, usize),
}
pub fn setup(
width: u32,
height: u32,
title: &str,
scale: bool,
event_loop: &EventLoop<()>,
) -> Result<(Window, PixelWrapper), GraphicsError> {
let win = create_window(width, height, title, scale, event_loop)?;
let surface = SurfaceTexture::new(width, height, &win);
let pixels = Pixels::new(width, height, surface).map_err(GraphicsError::PixelsInit)?;
Ok((
win,
PixelWrapper::new(pixels, width as usize, height as usize),
))
}
fn create_window(
width: u32,
height: u32,
title: &str,
scale: bool,
event_loop: &EventLoop<()>,
) -> Result<Window, GraphicsError> {
let window = WindowBuilder::new()
.with_visible(false)
.with_title(title)
.build(event_loop)
.map_err(|err| GraphicsError::WindowInit(format!("{:?}", err)))?;
let width = width as f64;
let height = height as f64;
let hidpi_factor = if scale { window.scale_factor() } else { 1.0 };
let size = LogicalSize::new(width * hidpi_factor, height * hidpi_factor);
window.set_inner_size(size);
window.set_min_inner_size(Some(size));
window.set_visible(true);
Ok(window)
}
pub trait Tint {
fn tint_add(&mut self, r_diff: isize, g_diff: isize, b_diff: isize, a_diff: isize);
fn tint_mul(&mut self, r_diff: f32, g_diff: f32, b_diff: f32, a_diff: f32);
}