Skip to content

Commit 7f22c0e

Browse files
committed
Add shape window
1 parent 77c1eb4 commit 7f22c0e

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

src/sdl2/video.rs

+56-8
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ pub struct WindowBuilder {
11131113
/// The window builder cannot be built on a non-main thread, so prevent cross-threaded moves and references.
11141114
/// `!Send` and `!Sync`,
11151115
subsystem: VideoSubsystem,
1116+
shaped: bool,
11161117
}
11171118

11181119
impl WindowBuilder {
@@ -1127,6 +1128,7 @@ impl WindowBuilder {
11271128
window_flags: 0,
11281129
subsystem: v.clone(),
11291130
create_metal_view: false,
1131+
shaped: false,
11301132
}
11311133
}
11321134

@@ -1148,14 +1150,25 @@ impl WindowBuilder {
11481150
let raw_width = self.width as c_int;
11491151
let raw_height = self.height as c_int;
11501152
unsafe {
1151-
let raw = sys::SDL_CreateWindow(
1152-
title.as_ptr() as *const c_char,
1153-
to_ll_windowpos(self.x),
1154-
to_ll_windowpos(self.y),
1155-
raw_width,
1156-
raw_height,
1157-
self.window_flags,
1158-
);
1153+
let raw = if self.shaped {
1154+
sys::SDL_CreateShapedWindow(
1155+
title.as_ptr() as *const c_char,
1156+
to_ll_windowpos(self.x) as u32,
1157+
to_ll_windowpos(self.y) as u32,
1158+
raw_width as u32,
1159+
raw_height as u32,
1160+
self.window_flags,
1161+
)
1162+
} else {
1163+
sys::SDL_CreateWindow(
1164+
title.as_ptr() as *const c_char,
1165+
to_ll_windowpos(self.x),
1166+
to_ll_windowpos(self.y),
1167+
raw_width,
1168+
raw_height,
1169+
self.window_flags,
1170+
)
1171+
};
11591172

11601173
if raw.is_null() {
11611174
Err(SdlError(get_error()))
@@ -1276,6 +1289,13 @@ impl WindowBuilder {
12761289
self.create_metal_view = true;
12771290
self
12781291
}
1292+
1293+
/// Sets shaped state, to create via SDL_CreateShapedWindow instead of SDL_CreateWindow
1294+
pub fn set_shaped(&mut self) -> &mut WindowBuilder {
1295+
self.shaped = true;
1296+
self
1297+
}
1298+
12791299
}
12801300

12811301
impl From<Window> for CanvasBuilder {
@@ -1533,6 +1553,34 @@ impl Window {
15331553
Ok(())
15341554
}
15351555

1556+
/// Set the shape of the window
1557+
/// To be effective:
1558+
/// - shaped must have been set using windows builder
1559+
/// - binarizationCutoff: specify the cutoff value for the shape's alpha
1560+
/// channel: At or above that cutoff value, a pixel is visible in the
1561+
/// shape. Below that, it's not part of the shape.
1562+
pub fn set_window_shape_alpha<S: AsRef<SurfaceRef>>(&mut self, shape: S, binarizationCutoff: u8) -> Result<(), i32> {
1563+
let mode = sys::WindowShapeMode::ShapeModeBinarizeAlpha;
1564+
let parameters = sys::SDL_WindowShapeParams { binarizationCutoff};
1565+
let mut shape_mode = sys::SDL_WindowShapeMode {
1566+
mode,
1567+
parameters,
1568+
};
1569+
let result = unsafe {
1570+
sys::SDL_SetWindowShape(
1571+
self.context.raw,
1572+
shape.as_ref().raw(),
1573+
&mut shape_mode,
1574+
)
1575+
};
1576+
if result == 0 {
1577+
Ok(())
1578+
} else {
1579+
Err(result)
1580+
}
1581+
}
1582+
1583+
15361584
#[doc(alias = "SDL_GetWindowTitle")]
15371585
pub fn title(&self) -> &str {
15381586
unsafe {

0 commit comments

Comments
 (0)