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 shape window #1346

Merged
merged 1 commit into from
Nov 12, 2023
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
59 changes: 51 additions & 8 deletions src/sdl2/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ pub struct WindowBuilder {
/// The window builder cannot be built on a non-main thread, so prevent cross-threaded moves and references.
/// `!Send` and `!Sync`,
subsystem: VideoSubsystem,
shaped: bool,
}

impl WindowBuilder {
Expand All @@ -1127,6 +1128,7 @@ impl WindowBuilder {
window_flags: 0,
subsystem: v.clone(),
create_metal_view: false,
shaped: false,
}
}

Expand All @@ -1148,14 +1150,25 @@ impl WindowBuilder {
let raw_width = self.width as c_int;
let raw_height = self.height as c_int;
unsafe {
let raw = sys::SDL_CreateWindow(
title.as_ptr() as *const c_char,
to_ll_windowpos(self.x),
to_ll_windowpos(self.y),
raw_width,
raw_height,
self.window_flags,
);
let raw = if self.shaped {
sys::SDL_CreateShapedWindow(
title.as_ptr() as *const c_char,
to_ll_windowpos(self.x) as u32,
to_ll_windowpos(self.y) as u32,
raw_width as u32,
raw_height as u32,
self.window_flags,
)
} else {
sys::SDL_CreateWindow(
title.as_ptr() as *const c_char,
to_ll_windowpos(self.x),
to_ll_windowpos(self.y),
raw_width,
raw_height,
self.window_flags,
)
};

if raw.is_null() {
Err(SdlError(get_error()))
Expand Down Expand Up @@ -1276,6 +1289,12 @@ impl WindowBuilder {
self.create_metal_view = true;
self
}

/// Sets shaped state, to create via SDL_CreateShapedWindow instead of SDL_CreateWindow
pub fn set_shaped(&mut self) -> &mut WindowBuilder {
self.shaped = true;
self
}
}

impl From<Window> for CanvasBuilder {
Expand Down Expand Up @@ -1533,6 +1552,30 @@ impl Window {
Ok(())
}

/// Set the shape of the window
/// To be effective:
/// - shaped must have been set using windows builder
/// - binarizationCutoff: specify the cutoff value for the shape's alpha
/// channel: At or above that cutoff value, a pixel is visible in the
/// shape. Below that, it's not part of the shape.
pub fn set_window_shape_alpha<S: AsRef<SurfaceRef>>(
&mut self,
shape: S,
binarizationCutoff: u8,
) -> Result<(), i32> {
let mode = sys::WindowShapeMode::ShapeModeBinarizeAlpha;
let parameters = sys::SDL_WindowShapeParams { binarizationCutoff };
let mut shape_mode = sys::SDL_WindowShapeMode { mode, parameters };
let result = unsafe {
sys::SDL_SetWindowShape(self.context.raw, shape.as_ref().raw(), &mut shape_mode)
};
if result == 0 {
Ok(())
} else {
Err(result)
}
}

#[doc(alias = "SDL_GetWindowTitle")]
pub fn title(&self) -> &str {
unsafe {
Expand Down
Loading