Skip to content

Commit a23b7f9

Browse files
committed
Added fruit example to show a more recognizable image for debugging
1 parent b5573de commit a23b7f9

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ x11-dl = "2.19.1"
1717
winapi = "0.3.9"
1818

1919
[dev-dependencies]
20-
winit = "0.26.1"
20+
winit = "0.26.1"
21+
image = "0.23.14"

examples/fruit.jpg

113 KB
Loading

examples/fruit.jpg.license

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fruit.jpg is licensed under the Creative Commons Attribution 3.0 Unported license and was put on Wikimedia by user Atomcibre
2+
3+
Source: https://commons.wikimedia.org/wiki/File:Culinary_fruits_front_view.jpg
4+
5+
License: https://creativecommons.org/licenses/by/3.0/deed.en

examples/fruit.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use image::GenericImageView;
2+
use softbuffer::GraphicsContext;
3+
use winit::event::{Event, WindowEvent};
4+
use winit::event_loop::{ControlFlow, EventLoop};
5+
use winit::window::WindowBuilder;
6+
7+
fn main() {
8+
//see fruit.jpg.license for the license of fruit.jpg
9+
let fruit = image::load_from_memory(include_bytes!("fruit.jpg")).unwrap();
10+
let buffer = fruit.pixels().map(|(_x, _y, pixel)|{
11+
let red = pixel.0[0] as u32;
12+
let green = pixel.0[1] as u32;
13+
let blue = pixel.0[2] as u32;
14+
15+
blue | (green << 8) | (red << 16)
16+
}).collect::<Vec<_>>();
17+
18+
let event_loop = EventLoop::new();
19+
let window = WindowBuilder::new().build(&event_loop).unwrap();
20+
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
21+
22+
event_loop.run(move |event, _, control_flow| {
23+
*control_flow = ControlFlow::Wait;
24+
25+
match event {
26+
Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => {
27+
graphics_context.set_buffer(&buffer, fruit.width() as u16, fruit.height() as u16);
28+
}
29+
Event::WindowEvent {
30+
event: WindowEvent::CloseRequested,
31+
window_id,
32+
} if window_id == graphics_context.window().id() => {
33+
*control_flow = ControlFlow::Exit;
34+
}
35+
_ => {}
36+
}
37+
});
38+
}

0 commit comments

Comments
 (0)