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 #1444

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
122 changes: 61 additions & 61 deletions src/sdl2/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,74 +331,74 @@ impl TryFrom<u32> for EventType {
use self::EventType::*;
use crate::sys::SDL_EventType::*;

Ok(match unsafe { transmute(n) } {
SDL_FIRSTEVENT => First,

SDL_QUIT => Quit,
SDL_APP_TERMINATING => AppTerminating,
SDL_APP_LOWMEMORY => AppLowMemory,
SDL_APP_WILLENTERBACKGROUND => AppWillEnterBackground,
SDL_APP_DIDENTERBACKGROUND => AppDidEnterBackground,
SDL_APP_WILLENTERFOREGROUND => AppWillEnterForeground,
SDL_APP_DIDENTERFOREGROUND => AppDidEnterForeground,

SDL_DISPLAYEVENT => Display,
SDL_WINDOWEVENT => Window,

SDL_KEYDOWN => KeyDown,
SDL_KEYUP => KeyUp,
SDL_TEXTEDITING => TextEditing,
SDL_TEXTINPUT => TextInput,

SDL_MOUSEMOTION => MouseMotion,
SDL_MOUSEBUTTONDOWN => MouseButtonDown,
SDL_MOUSEBUTTONUP => MouseButtonUp,
SDL_MOUSEWHEEL => MouseWheel,

SDL_JOYAXISMOTION => JoyAxisMotion,
SDL_JOYBALLMOTION => JoyBallMotion,
SDL_JOYHATMOTION => JoyHatMotion,
SDL_JOYBUTTONDOWN => JoyButtonDown,
SDL_JOYBUTTONUP => JoyButtonUp,
SDL_JOYDEVICEADDED => JoyDeviceAdded,
SDL_JOYDEVICEREMOVED => JoyDeviceRemoved,

SDL_CONTROLLERAXISMOTION => ControllerAxisMotion,
SDL_CONTROLLERBUTTONDOWN => ControllerButtonDown,
SDL_CONTROLLERBUTTONUP => ControllerButtonUp,
SDL_CONTROLLERDEVICEADDED => ControllerDeviceAdded,
SDL_CONTROLLERDEVICEREMOVED => ControllerDeviceRemoved,
SDL_CONTROLLERDEVICEREMAPPED => ControllerDeviceRemapped,
SDL_CONTROLLERTOUCHPADDOWN => ControllerTouchpadDown,
SDL_CONTROLLERTOUCHPADMOTION => ControllerTouchpadMotion,
SDL_CONTROLLERTOUCHPADUP => ControllerTouchpadUp,
match n {
x if x == SDL_FIRSTEVENT as u32 => Ok(First),

x if x == SDL_QUIT as u32 => Ok(Quit),
x if x == SDL_APP_TERMINATING as u32 => Ok(AppTerminating),
x if x == SDL_APP_LOWMEMORY as u32 => Ok(AppLowMemory),
x if x == SDL_APP_WILLENTERBACKGROUND as u32 => Ok(AppWillEnterBackground),
x if x == SDL_APP_DIDENTERBACKGROUND as u32 => Ok(AppDidEnterBackground),
x if x == SDL_APP_WILLENTERFOREGROUND as u32 => Ok(AppWillEnterForeground),
x if x == SDL_APP_DIDENTERFOREGROUND as u32 => Ok(AppDidEnterForeground),

x if x == SDL_DISPLAYEVENT as u32 => Ok(Display),
x if x == SDL_WINDOWEVENT as u32 => Ok(Window),

x if x == SDL_KEYDOWN as u32 => Ok(KeyDown),
x if x == SDL_KEYUP as u32 => Ok(KeyUp),
x if x == SDL_TEXTEDITING as u32 => Ok(TextEditing),
x if x == SDL_TEXTINPUT as u32 => Ok(TextInput),

x if x == SDL_MOUSEMOTION as u32 => Ok(MouseMotion),
x if x == SDL_MOUSEBUTTONDOWN as u32 => Ok(MouseButtonDown),
x if x == SDL_MOUSEBUTTONUP as u32 => Ok(MouseButtonUp),
x if x == SDL_MOUSEWHEEL as u32 => Ok(MouseWheel),

x if x == SDL_JOYAXISMOTION as u32 => Ok(JoyAxisMotion),
x if x == SDL_JOYBALLMOTION as u32 => Ok(JoyBallMotion),
x if x == SDL_JOYHATMOTION as u32 => Ok(JoyHatMotion),
x if x == SDL_JOYBUTTONDOWN as u32 => Ok(JoyButtonDown),
x if x == SDL_JOYBUTTONUP as u32 => Ok(JoyButtonUp),
x if x == SDL_JOYDEVICEADDED as u32 => Ok(JoyDeviceAdded),
x if x == SDL_JOYDEVICEREMOVED as u32 => Ok(JoyDeviceRemoved),

x if x == SDL_CONTROLLERAXISMOTION as u32 => Ok(ControllerAxisMotion),
x if x == SDL_CONTROLLERBUTTONDOWN as u32 => Ok(ControllerButtonDown),
x if x == SDL_CONTROLLERBUTTONUP as u32 => Ok(ControllerButtonUp),
x if x == SDL_CONTROLLERDEVICEADDED as u32 => Ok(ControllerDeviceAdded),
x if x == SDL_CONTROLLERDEVICEREMOVED as u32 => Ok(ControllerDeviceRemoved),
x if x == SDL_CONTROLLERDEVICEREMAPPED as u32 => Ok(ControllerDeviceRemapped),
x if x == SDL_CONTROLLERTOUCHPADDOWN as u32 => Ok(ControllerTouchpadDown),
x if x == SDL_CONTROLLERTOUCHPADMOTION as u32 => Ok(ControllerTouchpadMotion),
x if x == SDL_CONTROLLERTOUCHPADUP as u32 => Ok(ControllerTouchpadUp),
#[cfg(feature = "hidapi")]
SDL_CONTROLLERSENSORUPDATE => ControllerSensorUpdated,
x if x == SDL_CONTROLLERSENSORUPDATE as u32 => Ok(ControllerSensorUpdated),

SDL_FINGERDOWN => FingerDown,
SDL_FINGERUP => FingerUp,
SDL_FINGERMOTION => FingerMotion,
SDL_DOLLARGESTURE => DollarGesture,
SDL_DOLLARRECORD => DollarRecord,
SDL_MULTIGESTURE => MultiGesture,
x if x == SDL_FINGERDOWN as u32 => Ok(FingerDown),
x if x == SDL_FINGERUP as u32 => Ok(FingerUp),
x if x == SDL_FINGERMOTION as u32 => Ok(FingerMotion),
x if x == SDL_DOLLARGESTURE as u32 => Ok(DollarGesture),
x if x == SDL_DOLLARRECORD as u32 => Ok(DollarRecord),
x if x == SDL_MULTIGESTURE as u32 => Ok(MultiGesture),

SDL_CLIPBOARDUPDATE => ClipboardUpdate,
SDL_DROPFILE => DropFile,
SDL_DROPTEXT => DropText,
SDL_DROPBEGIN => DropBegin,
SDL_DROPCOMPLETE => DropComplete,
x if x == SDL_CLIPBOARDUPDATE as u32 => Ok(ClipboardUpdate),
x if x == SDL_DROPFILE as u32 => Ok(DropFile),
x if x == SDL_DROPTEXT as u32 => Ok(DropText),
x if x == SDL_DROPBEGIN as u32 => Ok(DropBegin),
x if x == SDL_DROPCOMPLETE as u32 => Ok(DropComplete),

SDL_AUDIODEVICEADDED => AudioDeviceAdded,
SDL_AUDIODEVICEREMOVED => AudioDeviceRemoved,
x if x == SDL_AUDIODEVICEADDED as u32 => Ok(AudioDeviceAdded),
x if x == SDL_AUDIODEVICEREMOVED as u32 => Ok(AudioDeviceRemoved),

SDL_RENDER_TARGETS_RESET => RenderTargetsReset,
SDL_RENDER_DEVICE_RESET => RenderDeviceReset,
x if x == SDL_RENDER_TARGETS_RESET as u32 => Ok(RenderTargetsReset),
x if x == SDL_RENDER_DEVICE_RESET as u32 => Ok(RenderDeviceReset),

SDL_USEREVENT => User,
SDL_LASTEVENT => Last,
x if x == SDL_USEREVENT as u32 => Ok(User),
x if x == SDL_LASTEVENT as u32 => Ok(Last),

_ => return Err(()),
})
_ => Err(()),
}
}
}

Expand Down
85 changes: 43 additions & 42 deletions src/sdl2/pixels.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::sys;
use std::convert::TryFrom;
use std::mem::transmute;

use crate::get_error;

Expand Down Expand Up @@ -453,47 +452,49 @@ impl TryFrom<u32> for PixelFormatEnum {
fn try_from(n: u32) -> Result<Self, Self::Error> {
use self::PixelFormatEnum::*;

Ok(match unsafe { transmute(n) } {
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_UNKNOWN => Unknown,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX1LSB => Index1LSB,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX1MSB => Index1MSB,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX4LSB => Index4LSB,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX4MSB => Index4MSB,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX8 => Index8,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB332 => RGB332,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB444 => RGB444,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB555 => RGB555,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGR555 => BGR555,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB4444 => ARGB4444,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA4444 => RGBA4444,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ABGR4444 => ABGR4444,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGRA4444 => BGRA4444,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB1555 => ARGB1555,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA5551 => RGBA5551,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ABGR1555 => ABGR1555,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGRA5551 => BGRA5551,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB565 => RGB565,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGR565 => BGR565,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB24 => RGB24,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGR24 => BGR24,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB888 => RGB888,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBX8888 => RGBX8888,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGR888 => BGR888,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGRX8888 => BGRX8888,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB8888 => ARGB8888,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA8888 => RGBA8888,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ABGR8888 => ABGR8888,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGRA8888 => BGRA8888,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB2101010 => ARGB2101010,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YV12 => YV12,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_IYUV => IYUV,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YUY2 => YUY2,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_UYVY => UYVY,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YVYU => YVYU,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_NV12 => NV12,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_NV21 => NV21,
_ => return Err(()),
})
match n {
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_UNKNOWN as u32 => Ok(Unknown),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX1LSB as u32 => Ok(Index1LSB),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX1MSB as u32 => Ok(Index1MSB),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX4LSB as u32 => Ok(Index4LSB),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX4MSB as u32 => Ok(Index4MSB),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_INDEX8 as u32 => Ok(Index8),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB332 as u32 => Ok(RGB332),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB444 as u32 => Ok(RGB444),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB555 as u32 => Ok(RGB555),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGR555 as u32 => Ok(BGR555),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB4444 as u32 => Ok(ARGB4444),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA4444 as u32 => Ok(RGBA4444),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ABGR4444 as u32 => Ok(ABGR4444),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGRA4444 as u32 => Ok(BGRA4444),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB1555 as u32 => Ok(ARGB1555),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA5551 as u32 => Ok(RGBA5551),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ABGR1555 as u32 => Ok(ABGR1555),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGRA5551 as u32 => Ok(BGRA5551),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB565 as u32 => Ok(RGB565),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGR565 as u32 => Ok(BGR565),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB24 as u32 => Ok(RGB24),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGR24 as u32 => Ok(BGR24),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGB888 as u32 => Ok(RGB888),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBX8888 as u32 => Ok(RGBX8888),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGR888 as u32 => Ok(BGR888),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGRX8888 as u32 => Ok(BGRX8888),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB8888 as u32 => Ok(ARGB8888),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA8888 as u32 => Ok(RGBA8888),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ABGR8888 as u32 => Ok(ABGR8888),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_BGRA8888 as u32 => Ok(BGRA8888),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_ARGB2101010 as u32 => {
Ok(ARGB2101010)
}
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YV12 as u32 => Ok(YV12),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_IYUV as u32 => Ok(IYUV),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YUY2 as u32 => Ok(YUY2),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_UYVY as u32 => Ok(UYVY),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YVYU as u32 => Ok(YVYU),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_NV12 as u32 => Ok(NV12),
x if x == sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_NV21 as u32 => Ok(NV21),
_ => Err(()),
}
}
}

Expand Down
85 changes: 72 additions & 13 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 @@ -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(()),
}
}
}

Expand Down Expand Up @@ -164,14 +166,39 @@ 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
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(()),
}
}
}

Expand Down Expand Up @@ -2097,6 +2124,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) };
Expand Down Expand Up @@ -2440,6 +2487,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