-
Notifications
You must be signed in to change notification settings - Fork 472
Add texture scale mode #1444
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
Merged
Merged
Add texture scale mode #1444
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fad5b95
Add texture scale mode
jagprog5 325cd16
Formatting
jagprog5 aa858db
remove UB
jagprog5 cf7d253
cargo fmt
jagprog5 4c83d44
changelog entry
jagprog5 74153e5
Fix docs string
jagprog5 7db2550
rm repr
jagprog5 5449674
Merge remote-tracking branch 'origin/master'
jagprog5 0770d6b
Merge remote-tracking branch 'origin/master'
jagprog5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -110,11 +111,12 @@ impl TryFrom<u32> for TextureAccess { | |
use self::TextureAccess::*; | ||
use crate::sys::SDL_TextureAccess::*; | ||
|
||
Ok(match unsafe { transmute(n) } { | ||
SDL_TEXTUREACCESS_STATIC => Static, | ||
SDL_TEXTUREACCESS_STREAMING => Streaming, | ||
SDL_TEXTUREACCESS_TARGET => Target, | ||
}) | ||
match n { | ||
x if x == SDL_TEXTUREACCESS_STATIC as u32 => Ok(Static), | ||
x if x == SDL_TEXTUREACCESS_STREAMING as u32 => Ok(Streaming), | ||
x if x == SDL_TEXTUREACCESS_TARGET as u32 => Ok(Target), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
|
@@ -164,14 +166,43 @@ impl TryFrom<u32> for BlendMode { | |
use self::BlendMode::*; | ||
use crate::sys::SDL_BlendMode::*; | ||
|
||
Ok(match unsafe { transmute(n) } { | ||
SDL_BLENDMODE_NONE => None, | ||
SDL_BLENDMODE_BLEND => Blend, | ||
SDL_BLENDMODE_ADD => Add, | ||
SDL_BLENDMODE_MOD => Mod, | ||
SDL_BLENDMODE_MUL => Mul, | ||
SDL_BLENDMODE_INVALID => Invalid, | ||
}) | ||
match n { | ||
x if x == SDL_BLENDMODE_NONE as u32 => Ok(None), | ||
x if x == SDL_BLENDMODE_BLEND as u32 => Ok(Blend), | ||
x if x == SDL_BLENDMODE_ADD as u32 => Ok(Add), | ||
x if x == SDL_BLENDMODE_MOD as u32 => Ok(Mod), | ||
x if x == SDL_BLENDMODE_MUL as u32 => Ok(Mul), | ||
x if x == SDL_BLENDMODE_INVALID as u32 => Ok(Invalid), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
#[repr(i32)] | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] | ||
pub enum ScaleMode { | ||
/// nearest pixel sampling. default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not include this test, as it requires creating a texture creator, which I doubt would work with the CI. Of note, the default is Nearest for SDL2, but Linear for SDL3. use sdl2::{render::ScaleMode, surface::Surface};
extern crate sdl2;
extern crate sdl2_sys;
#[test]
fn test_scale_mode() {
let sdl_context = sdl2::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().build().unwrap();
let texture_creator = canvas.texture_creator();
let surface = Surface::new(1, 1, sdl2::pixels::PixelFormatEnum::ARGB8888).unwrap();
let mut texture = texture_creator.create_texture_from_surface(surface).unwrap();
// checking the default scale mode
assert_eq!(texture.scale_mode(), ScaleMode::Nearest);
// check set get
texture.set_scale_mode(ScaleMode::Nearest);
assert_eq!(texture.scale_mode(), ScaleMode::Nearest);
} |
||
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> { | ||
match n { | ||
x if x == crate::sys::SDL_ScaleMode::SDL_ScaleModeNearest as u32 => { | ||
Ok(ScaleMode::Nearest) | ||
} | ||
x if x == crate::sys::SDL_ScaleMode::SDL_ScaleModeLinear as u32 => { | ||
Ok(ScaleMode::Linear) | ||
} | ||
x if x == crate::sys::SDL_ScaleMode::SDL_ScaleModeBest as u32 => Ok(ScaleMode::Best), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
|
@@ -2097,6 +2128,26 @@ 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) }; | ||
|
@@ -2440,6 +2491,18 @@ impl<'r> Texture<'r> { | |
InternalTexture { raw: self.raw }.color_mod() | ||
} | ||
|
||
/// 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() | ||
} | ||
|
||
/// Sets an additional alpha value multiplied into render copy operations. | ||
#[inline] | ||
pub fn set_alpha_mod(&mut self, alpha: u8) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.