#![allow(clippy::unnecessary_cast)]
use bitflags::bitflags;
use crate::texture::Texture;
use crate::{
as_raw,
geo::{Point, Rect},
};
use crate::{bind, EnumInt, Sdl};
use super::Renderer;
bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PasteExFlip: u32 {
const HORIZONTAL = bind::SDL_FLIP_HORIZONTAL as u32;
const VERTICAL = bind::SDL_FLIP_VERTICAL as u32;
const BOTH = Self::HORIZONTAL.bits() | Self::VERTICAL.bits();
}
}
impl Default for PasteExFlip {
fn default() -> Self {
Self::empty()
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct PasteExOption {
pub target_area: Option<Rect>,
pub rotation_degrees: f64,
pub center: Option<Point>,
pub flip: PasteExFlip,
}
#[derive(Debug)]
pub struct Paster<'renderer> {
renderer: &'renderer Renderer<'renderer>,
}
impl<'renderer> Drop for Paster<'renderer> {
fn drop(&mut self) {
unsafe { bind::SDL_RenderPresent(self.renderer.as_ptr()) }
}
}
impl<'renderer> Paster<'renderer> {
#[must_use]
pub fn new(renderer: &'renderer Renderer) -> Self {
Self { renderer }
}
#[must_use]
pub fn renderer(&self) -> &Renderer {
self.renderer
}
}
impl Paster<'_> {
pub fn paste(&self, texture: &Texture, target_area: Option<Rect>) {
let src = texture.clip().map(Into::into);
let dst = target_area.map(Into::into);
let ret = unsafe {
bind::SDL_RenderCopy(
self.renderer.as_ptr(),
texture.as_ptr(),
as_raw(&src),
as_raw(&dst),
)
};
if ret != 0 {
Sdl::error_then_panic("Pasting texture to renderer");
}
}
pub fn paste_ex(
&self,
texture: &Texture,
PasteExOption {
target_area,
rotation_degrees,
center,
flip,
}: PasteExOption,
) {
let src = texture.clip().map(Into::into);
let dst = target_area.map(Into::into);
let center = center.map(Into::into);
let ret = unsafe {
bind::SDL_RenderCopyEx(
self.renderer.as_ptr(),
texture.as_ptr(),
as_raw(&src),
as_raw(&dst),
rotation_degrees,
as_raw(¢er),
flip.bits() as EnumInt,
)
};
if ret != 0 {
Sdl::error_then_panic("Pasting texture to renderer ex");
}
}
}