Skip to content

Commit a78becd

Browse files
committed
Add rectangle toggle example
This commit adds a new example that shows a XOR pattern. A rectangle can be superimposed on the background by pressing the space bar. This example was created to demonstrate a bug in the Core Graphics backend that causes the buffer to fade when calling set_buffer, instead of applying it immediately.
1 parent 13853fe commit a78becd

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

examples/rectangle.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use softbuffer::GraphicsContext;
2+
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
3+
use winit::event_loop::{ControlFlow, EventLoop};
4+
use winit::window::WindowBuilder;
5+
6+
fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
7+
for (index, color) in buffer.iter_mut().enumerate() {
8+
let y = index / width;
9+
let x = index % width;
10+
11+
if flag && x >= 100 && x < width - 100 && y >= 100 && y < height - 100 {
12+
*color = 0x00ffffff;
13+
} else {
14+
let red = (x & 0xff) ^ (y & 0xff);
15+
let green = (x & 0x7f) ^ (y & 0x7f);
16+
let blue = (x & 0x3f) ^ (y & 0x3f);
17+
*color = (blue | (green << 8) | (red << 16)) as u32;
18+
}
19+
}
20+
}
21+
22+
fn main() {
23+
let event_loop = EventLoop::new();
24+
25+
let window = WindowBuilder::new()
26+
.with_title("Press space to show/hide a rectangle")
27+
.build(&event_loop)
28+
.unwrap();
29+
30+
#[cfg(target_arch = "wasm32")]
31+
{
32+
use winit::platform::web::WindowExtWebSys;
33+
34+
web_sys::window()
35+
.unwrap()
36+
.document()
37+
.unwrap()
38+
.body()
39+
.unwrap()
40+
.append_child(&window.canvas())
41+
.unwrap();
42+
}
43+
44+
let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();
45+
46+
let mut buffer = Vec::new();
47+
let mut flag = false;
48+
49+
event_loop.run(move |event, _, control_flow| {
50+
*control_flow = ControlFlow::Wait;
51+
52+
match event {
53+
Event::RedrawRequested(window_id) if window_id == window.id() => {
54+
// Grab the window's client area dimensions
55+
let (width, height) = {
56+
let size = window.inner_size();
57+
(size.width as usize, size.height as usize)
58+
};
59+
60+
// Resize the off-screen buffer if the window size has changed
61+
if buffer.len() != width * height {
62+
buffer.resize(width * height, 0);
63+
}
64+
65+
// Draw something in the offscreen buffer
66+
redraw(&mut buffer, width, height, flag);
67+
68+
// Blit the offscreen buffer to the window's client area
69+
graphics_context.set_buffer(&buffer, width as u16, height as u16);
70+
}
71+
72+
Event::WindowEvent {
73+
event: WindowEvent::CloseRequested,
74+
window_id,
75+
} if window_id == window.id() => {
76+
*control_flow = ControlFlow::Exit;
77+
}
78+
79+
Event::WindowEvent {
80+
event:
81+
WindowEvent::KeyboardInput {
82+
input:
83+
KeyboardInput {
84+
state: ElementState::Pressed,
85+
virtual_keycode: Some(VirtualKeyCode::Space),
86+
..
87+
},
88+
..
89+
},
90+
window_id,
91+
} if window_id == window.id() => {
92+
// Flip the rectangle flag and request a redraw to show the changed image
93+
flag = !flag;
94+
window.request_redraw();
95+
}
96+
97+
_ => {}
98+
}
99+
});
100+
}

0 commit comments

Comments
 (0)