Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add texture_scale_mode #100

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/sdl3/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,36 @@ fn ll_create_texture(
)
}

#[repr(i32)]
Copy link
Contributor Author

@jagprog5 jagprog5 Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't include the following test, as it requires creating a texture creator and doubt this would work with the CI:

use sdl3::{pixels::PixelFormat, render::{ScaleMode, SurfaceCanvas}, surface::Surface};
use sdl3_sys::pixels::SDL_PIXELFORMAT_RGB24;

extern crate sdl3;

#[test]
fn test_scale_mode() {
    let sdl_context = sdl3::init().unwrap();
    let sdl_video_subsystem = sdl_context.video().unwrap();
    let window = sdl_video_subsystem
    .window(
        "tester",
        100,
        100,
    )
    .build()
    .unwrap();

    let canvas = window.into_canvas();
    let texture_creator = canvas.texture_creator();
    let surface = Surface::new(1, 1, PixelFormat::try_from(SDL_PIXELFORMAT_RGB24).unwrap()).unwrap();
    let mut texture = texture_creator.create_texture_from_surface(surface).unwrap();

    // checking the default scale mode
    assert_eq!(texture.scale_mode(), ScaleMode::Linear);
    // check set get
    texture.set_scale_mode(ScaleMode::Nearest);
    assert_eq!(texture.scale_mode(), ScaleMode::Nearest);
}

#[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<sdl3_sys::everything::SDL_ScaleMode> 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<sdl3_sys::everything::SDL_ScaleMode> for ScaleMode {
type Error = ();

fn try_from(n: sdl3_sys::everything::SDL_ScaleMode) -> Result<Self, Self::Error> {
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<T> TextureCreator<T> {
// this can prevent introducing UB until
Expand Down Expand Up @@ -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<sdl3_sys::everything::SDL_ScaleMode> =
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<R>(
&mut self,
Expand Down Expand Up @@ -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
Expand Down
Loading