use crate::{error::Result, image::Icon, prelude::*};
use std::num::NonZeroUsize;
pub(crate) use crate::{texture::TextureRenderer, window::WindowRenderer};
#[cfg(not(target_arch = "wasm32"))]
pub mod sdl;
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use sdl::Renderer;
#[cfg(target_arch = "wasm32")]
pub mod wasm;
#[cfg(target_arch = "wasm32")]
pub(crate) use wasm::Renderer;
const TEXTURE_CACHE_SIZE: usize = 256;
const TEXT_CACHE_SIZE: usize = 512;
#[derive(Debug, Clone)]
pub(crate) struct RendererSettings {
pub(crate) title: String,
pub(crate) icon: Option<Icon>,
pub(crate) x: Position,
pub(crate) y: Position,
pub(crate) width: u32,
pub(crate) height: u32,
pub(crate) scale_x: f32,
pub(crate) scale_y: f32,
pub(crate) audio_sample_rate: Option<i32>,
pub(crate) audio_channels: Option<u8>,
pub(crate) audio_buffer_size: Option<u16>,
pub(crate) fullscreen: bool,
pub(crate) vsync: bool,
pub(crate) resizable: bool,
pub(crate) borderless: bool,
pub(crate) allow_highdpi: bool,
pub(crate) hidden: bool,
pub(crate) show_frame_rate: bool,
pub(crate) target_frame_rate: Option<usize>,
pub(crate) texture_cache_size: NonZeroUsize,
pub(crate) text_cache_size: NonZeroUsize,
}
impl Default for RendererSettings {
#[allow(clippy::expect_used)]
fn default() -> Self {
Self {
title: String::new(),
icon: None,
x: Position::default(),
y: Position::default(),
width: 640,
height: 480,
scale_x: 1.0,
scale_y: 1.0,
audio_sample_rate: None,
audio_channels: None,
audio_buffer_size: None,
fullscreen: false,
vsync: false,
resizable: false,
borderless: false,
allow_highdpi: false,
hidden: false,
show_frame_rate: false,
target_frame_rate: None,
texture_cache_size: TEXTURE_CACHE_SIZE.try_into().expect("valid cache size"),
text_cache_size: TEXT_CACHE_SIZE.try_into().expect("valid cache size"),
}
}
}
pub(crate) trait Rendering: Sized {
fn new(settings: RendererSettings) -> Result<Self>;
fn clear(&mut self) -> Result<()>;
fn set_draw_color(&mut self, color: Color) -> Result<()>;
fn clip(&mut self, rect: Option<Rect<i32>>) -> Result<()>;
fn blend_mode(&mut self, mode: BlendMode);
fn present(&mut self);
fn scale(&mut self, x: f32, y: f32) -> Result<()>;
fn font_size(&mut self, size: u32) -> Result<()>;
fn font_style(&mut self, style: FontStyle);
fn font_family(&mut self, font: &Font) -> Result<()>;
fn clipboard_text(&self) -> String;
fn set_clipboard_text(&self, value: &str) -> Result<()>;
fn open_url(&self, url: &str) -> Result<()>;
#[allow(clippy::too_many_arguments)]
fn text(
&mut self,
position: Point<i32>,
text: &str,
wrap_width: Option<u32>,
angle: Option<f64>,
center: Option<Point<i32>>,
flipped: Option<Flipped>,
fill: Option<Color>,
outline: u16,
) -> Result<(u32, u32)>;
fn size_of(&self, text: &str, wrap_width: Option<u32>) -> Result<(u32, u32)>;
fn point(&mut self, p: Point<i32>, color: Color) -> Result<()>;
fn line(&mut self, line: Line<i32>, smooth: bool, width: u8, color: Color) -> Result<()>;
fn bezier<I>(&mut self, ps: I, detail: i32, stroke: Option<Color>) -> Result<()>
where
I: Iterator<Item = Point<i32>>;
fn triangle(
&mut self,
tri: Tri<i32>,
smooth: bool,
fill: Option<Color>,
stroke: Option<Color>,
) -> Result<()>;
fn rect(
&mut self,
rect: Rect<i32>,
radius: Option<i32>,
fill: Option<Color>,
stroke: Option<Color>,
) -> Result<()>;
fn quad(
&mut self,
quad: Quad<i32>,
smooth: bool,
fill: Option<Color>,
stroke: Option<Color>,
) -> Result<()>;
fn polygon<I>(
&mut self,
ps: I,
smooth: bool,
fill: Option<Color>,
stroke: Option<Color>,
) -> Result<()>
where
I: Iterator<Item = Point<i32>>;
fn ellipse(
&mut self,
ellipse: Ellipse<i32>,
smooth: bool,
fill: Option<Color>,
stroke: Option<Color>,
) -> Result<()>;
#[allow(clippy::too_many_arguments)]
fn arc(
&mut self,
p: Point<i32>,
radius: i32,
start: i32,
end: i32,
mode: ArcMode,
fill: Option<Color>,
stroke: Option<Color>,
) -> Result<()>;
#[allow(clippy::too_many_arguments)]
fn image(
&mut self,
img: &Image,
src: Option<Rect<i32>>,
dst: Option<Rect<i32>>,
angle: f64,
center: Option<Point<i32>>,
flipped: Option<Flipped>,
tint: Option<Color>,
) -> Result<()>;
fn to_bytes(&mut self) -> Result<Vec<u8>>;
fn open_controller(&mut self, controller_id: ControllerId) -> Result<()>;
fn close_controller(&mut self, controller_id: ControllerId);
}