From c5f22be6ee454c87c0b85f3cfeb47e13add4d5f0 Mon Sep 17 00:00:00 2001 From: John Giorshev Date: Sat, 8 Feb 2025 12:44:37 -0500 Subject: [PATCH] add texture_scale_mode --- src/sdl3/render.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/sdl3/render.rs b/src/sdl3/render.rs index 8b1f4e41..aa147483 100644 --- a/src/sdl3/render.rs +++ b/src/sdl3/render.rs @@ -818,6 +818,36 @@ fn ll_create_texture( ) } +#[repr(i32)] +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +pub enum ScaleMode { + /// nearest pixel sampling. + Nearest = sdl3_sys::everything::SDL_ScaleMode::NEAREST.0, + /// linear filtering. this is the default + Linear = sdl3_sys::everything::SDL_ScaleMode::LINEAR.0, +} + +impl Into for ScaleMode { + fn into(self) -> sdl3_sys::everything::SDL_ScaleMode { + match self { + ScaleMode::Nearest => sdl3_sys::everything::SDL_ScaleMode::NEAREST, + ScaleMode::Linear => sdl3_sys::everything::SDL_ScaleMode::LINEAR, + } + } +} + +impl TryFrom for ScaleMode { + type Error = (); + + fn try_from(n: sdl3_sys::everything::SDL_ScaleMode) -> Result { + Ok(match n { + sdl3_sys::everything::SDL_ScaleMode::NEAREST => Self::Nearest, + sdl3_sys::everything::SDL_ScaleMode::LINEAR => Self::Linear, + _ => return Err(()), + }) + } +} + /// Texture-creating methods for the renderer impl TextureCreator { // this can prevent introducing UB until @@ -1948,6 +1978,29 @@ impl InternalTexture { } } + #[doc(alias = "SDL_SetTextureScaleMode")] + pub fn set_scale_mode(&mut self, scale: ScaleMode) { + let ret = unsafe { sdl3_sys::everything::SDL_SetTextureScaleMode(self.raw, scale.into()) }; + + if !ret { + panic!("Error setting scale mode: {}", get_error()) + } + } + + #[doc(alias = "SDL_GetTextureScaleMode")] + pub fn scale_mode(&self) -> ScaleMode { + let mut scale: MaybeUninit = + mem::MaybeUninit::uninit(); + let ret = + unsafe { sdl3_sys::everything::SDL_GetTextureScaleMode(self.raw, scale.as_mut_ptr()) }; + if !ret { + panic!("{}", get_error()) + } else { + let scale = unsafe { scale.assume_init() }; + ScaleMode::try_from(scale).unwrap() + } + } + #[doc(alias = "SDL_UpdateTexture")] pub fn update( &mut self, @@ -2279,6 +2332,18 @@ impl Texture<'_> { InternalTexture { raw: self.raw }.blend_mode() } + /// Sets the scale mode for use when rendered. + #[inline] + pub fn set_scale_mode(&mut self, scale: ScaleMode) { + InternalTexture { raw: self.raw }.set_scale_mode(scale) + } + + /// Gets the scale mode for use when rendered. + #[inline] + pub fn scale_mode(&self) -> ScaleMode { + InternalTexture { raw: self.raw }.scale_mode() + } + /// Updates the given texture rectangle with new pixel data. /// /// `pitch` is the number of bytes in a row of pixel data, including padding