forked from rust-windowing/softbuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinit_tiny_skia.rs
More file actions
121 lines (105 loc) · 5.1 KB
/
winit_tiny_skia.rs
File metadata and controls
121 lines (105 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use softbuffer::RGBA;
use std::num::NonZeroU32;
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
#[path = "utils/winit_app.rs"]
mod winit_app;
use tiny_skia::{BlendMode, LineCap, Paint, PathBuilder, PixmapMut, Stroke, StrokeDash, Transform};
fn main() {
let event_loop = EventLoop::new().unwrap();
let app = winit_app::WinitAppBuilder::with_init(|elwt| {
let window = winit_app::make_window(elwt, |w| w.with_transparent(true));
let context = softbuffer::Context::new(window.clone()).unwrap();
let surface = softbuffer::Surface::new_with_alpha(&context, window.clone()).unwrap();
(window, surface)
})
.with_event_handler(|state, event, elwt| {
let (window, surface) = state;
elwt.set_control_flow(ControlFlow::Wait);
match event {
Event::WindowEvent {
window_id,
event: WindowEvent::Resized(size),
} if window_id == window.id() => {
if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
surface.resize(width, height).unwrap();
}
}
Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
} if window_id == window.id() => {
let size = window.inner_size();
if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
let mut buffer = surface.buffer_mut().unwrap();
//We draw the background of our window in softbuffer writing to individual pixels
for y in 0..height.get() {
for x in 0..width.get() {
const SCALE_FACTOR: u32 = 3;
let red = (x/SCALE_FACTOR) % 255;
let green = (y/SCALE_FACTOR) % 255;
let blue = ((x/SCALE_FACTOR) * (y/SCALE_FACTOR)) % 255;
let alpha = if blue > 255/2{
255
}else{
0
};
let index = y as usize * width.get() as usize + x as usize;
buffer.pixels_rgb_mut()[index] = softbuffer::RGBA::new_unchecked(red,green, blue, alpha);
}
}
// buffer.fill(RGBA::new_unchecked(50,0,50, 200)); // Alternatively we could fill with a solid color
//using tiny_skia that accepts the u8 rgba format, we draw a star on top of our background
buffer.pixel_u8_slice_rgba(|u8_buffer_rgba| {
let mut pixmap =
PixmapMut::from_bytes(u8_buffer_rgba, width.get(), height.get())
.unwrap();
let mut paint = Paint::default();
// paint.set_color_rgba8(255, 0, 255, 0); // <-- We could set the color, but because we are using BlendMode::Clear the color does not matter
paint.anti_alias = true;
paint.blend_mode = BlendMode::Clear; // <-- Set Blend mode so that we can draw transparent pixels
let path = {
let mut pb = PathBuilder::new();
let RADIUS: f32 = (width.get().min(height.get()) / 2) as f32;
let CENTER: f32 = (width.get().min(height.get()) / 2) as f32;
pb.move_to(CENTER + RADIUS, CENTER);
for i in 1..8 {
let a = 2.6927937 * i as f32;
pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
}
pb.finish().unwrap()
};
let mut stroke = Stroke::default();
stroke.width = 24.0;
stroke.line_cap = LineCap::Round;
stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
});
buffer.present().unwrap();
}
}
Event::WindowEvent {
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
});
winit_app::run_app(event_loop, app);
}