Skip to content

Commit e443428

Browse files
authored
Merge pull request #1346 from serpilliere/shape_window
Add shape window
2 parents 77c1eb4 + a600d3a commit e443428

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

src/sdl2/video.rs

+51-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,12 @@ 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+
}
12791298
}
12801299

12811300
impl From<Window> for CanvasBuilder {
@@ -1533,6 +1552,30 @@ impl Window {
15331552
Ok(())
15341553
}
15351554

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

0 commit comments

Comments
 (0)