Skip to content

Commit

Permalink
Add texture scale mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jagprog5 committed Dec 3, 2024
1 parent 400e033 commit fad5b95
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/sdl2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use std::rc::Rc;

use crate::sys;
use crate::sys::SDL_BlendMode;
use crate::sys::SDL_ScaleMode;
use crate::sys::SDL_TextureAccess;

/// Contains the description of an error returned by SDL
Expand Down Expand Up @@ -175,6 +176,29 @@ impl TryFrom<u32> for BlendMode {
}
}

#[repr(i32)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum ScaleMode {
/// nearest pixel sampling. default
Nearest = SDL_ScaleMode::SDL_ScaleModeNearest as i32,
/// linear filtering
Linear = SDL_ScaleMode::SDL_ScaleModeLinear as i32,
/// anisotropic filtering
Best = SDL_ScaleMode::SDL_ScaleModeBest as i32,
}

impl TryFrom<u32> for ScaleMode {
type Error = ();

fn try_from(n: u32) -> Result<Self, Self::Error> {
Ok(match unsafe { transmute(n) } {
crate::sys::SDL_ScaleMode::SDL_ScaleModeNearest => self::ScaleMode::Nearest,
crate::sys::SDL_ScaleMode::SDL_ScaleModeLinear => self::ScaleMode::Linear,
crate::sys::SDL_ScaleMode::SDL_ScaleModeBest => self::ScaleMode::Best,
})
}
}

impl RendererInfo {
pub unsafe fn from_ll(info: &sys::SDL_RendererInfo) -> RendererInfo {
let texture_formats: Vec<PixelFormatEnum> = info.texture_formats
Expand Down Expand Up @@ -2097,6 +2121,27 @@ impl InternalTexture {
}
}

#[doc(alias = "SDL_SetTextureScaleMode")]
pub fn set_scale_mode(&mut self, scale: ScaleMode) {
let ret =
unsafe { sys::SDL_SetTextureScaleMode(self.raw, transmute(scale as u32)) };
if ret != 0 {
panic!("Error setting scale mode: {}", get_error())
}
}

#[doc(alias = "SDL_GetTextureScaleMode")]
pub fn scale_mode(&self) -> ScaleMode {
let mut scale: MaybeUninit<SDL_ScaleMode> = mem::MaybeUninit::uninit();
let ret = unsafe { sys::SDL_GetTextureScaleMode(self.raw, scale.as_mut_ptr()) };
if ret != 0 {
panic!("{}", get_error())
} else {
let scale = unsafe { scale.assume_init() };
ScaleMode::try_from(scale as u32).unwrap()
}
}

#[doc(alias = "SDL_SetTextureAlphaMod")]
pub fn set_alpha_mod(&mut self, alpha: u8) {
let ret = unsafe { sys::SDL_SetTextureAlphaMod(self.raw, alpha) };
Expand Down Expand Up @@ -2440,6 +2485,18 @@ impl<'r> Texture<'r> {
InternalTexture { raw: self.raw }.color_mod()
}

/// Sets an additional color value multiplied into render copy operations.
#[inline]
pub fn set_scale_mode(&mut self, scale: ScaleMode) {
InternalTexture { raw: self.raw }.set_scale_mode(scale)
}

/// Gets the additional color value multiplied into render copy operations.
#[inline]
pub fn scale_mode(&self) -> ScaleMode {
InternalTexture { raw: self.raw }.scale_mode()
}

/// Sets an additional alpha value multiplied into render copy operations.
#[inline]
pub fn set_alpha_mod(&mut self, alpha: u8) {
Expand Down

0 comments on commit fad5b95

Please sign in to comment.