-
Notifications
You must be signed in to change notification settings - Fork 41
/
hello_skulpin_winit.rs
180 lines (155 loc) · 5.76 KB
/
hello_skulpin_winit.rs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// This example shows how to use the renderer directly. This allows full control of winit
// and the update loop
use skulpin::CoordinateSystemHelper;
use skulpin::winit;
use skulpin::skia_safe;
use skulpin::rafx::api::RafxExtents2D;
fn main() {
// Setup logging
env_logger::Builder::from_default_env()
.filter_level(log::LevelFilter::Debug)
.init();
// Create the winit event loop
let event_loop = winit::event_loop::EventLoop::<()>::with_user_event();
// Set up the coordinate system to be fixed at 900x600, and use this as the default window size
// This means the drawing code can be written as though the window is always 900x600. The
// output will be automatically scaled so that it's always visible.
let logical_size = winit::dpi::LogicalSize::new(900.0, 600.0);
let visible_range = skulpin::skia_safe::Rect {
left: 0.0,
right: logical_size.width as f32,
top: 0.0,
bottom: logical_size.height as f32,
};
let scale_to_fit = skulpin::skia_safe::matrix::ScaleToFit::Center;
// Create a single window
let window = winit::window::WindowBuilder::new()
.with_title("Skulpin")
.with_inner_size(logical_size)
.build(&event_loop)
.expect("Failed to create window");
let window_size = window.inner_size();
let window_extents = RafxExtents2D {
width: window_size.width,
height: window_size.height,
};
// Create the renderer, which will draw to the window
let renderer = skulpin::RendererBuilder::new()
.coordinate_system(skulpin::CoordinateSystem::VisibleRange(
visible_range,
scale_to_fit,
))
.build(&window, window_extents);
// Check if there were error setting up vulkan
if let Err(e) = renderer {
println!("Error during renderer construction: {:?}", e);
return;
}
let mut renderer = renderer.unwrap();
// Increment a frame count so we can render something that moves
let mut frame_count = 0;
// Start the window event loop. Winit will not return once run is called. We will get notified
// when important events happen.
event_loop.run(move |event, _window_target, control_flow| {
match event {
//
// Halt if the user requests to close the window
//
winit::event::Event::WindowEvent {
event: winit::event::WindowEvent::CloseRequested,
..
} => *control_flow = winit::event_loop::ControlFlow::Exit,
//
// Close if the escape key is hit
//
winit::event::Event::WindowEvent {
event:
winit::event::WindowEvent::KeyboardInput {
input:
winit::event::KeyboardInput {
virtual_keycode: Some(winit::event::VirtualKeyCode::Escape),
..
},
..
},
..
} => *control_flow = winit::event_loop::ControlFlow::Exit,
//
// Request a redraw any time we finish processing events
//
winit::event::Event::MainEventsCleared => {
// Queue a RedrawRequested event.
window.request_redraw();
}
//
// Redraw
//
winit::event::Event::RedrawRequested(_window_id) => {
let window_size = window.inner_size();
let window_extents = RafxExtents2D {
width: window_size.width,
height: window_size.height,
};
if let Err(e) = renderer.draw(
window_extents,
window.scale_factor(),
|canvas, coordinate_system_helper| {
draw(canvas, coordinate_system_helper, frame_count);
frame_count += 1;
},
) {
println!("Error during draw: {:?}", e);
*control_flow = winit::event_loop::ControlFlow::Exit
}
}
//
// Ignore all other events
//
_ => {}
}
});
}
/// Called when winit passes us a WindowEvent::RedrawRequested
fn draw(
canvas: &mut skia_safe::Canvas,
_coordinate_system_helper: CoordinateSystemHelper,
frame_count: i32,
) {
// Generally would want to clear data every time we draw
canvas.clear(skia_safe::Color::from_argb(0, 0, 0, 255));
// Floating point value constantly moving between 0..1 to generate some movement
let f = ((frame_count as f32 / 30.0).sin() + 1.0) / 2.0;
// Make a color to draw with
let mut paint = skia_safe::Paint::new(skia_safe::Color4f::new(1.0 - f, 0.0, f, 1.0), None);
paint.set_anti_alias(true);
paint.set_style(skia_safe::paint::Style::Stroke);
paint.set_stroke_width(2.0);
// Draw a line
canvas.draw_line(
skia_safe::Point::new(100.0, 500.0),
skia_safe::Point::new(800.0, 500.0),
&paint,
);
// Draw a circle
canvas.draw_circle(
skia_safe::Point::new(200.0 + (f * 500.0), 420.0),
50.0,
&paint,
);
// Draw a rectangle
canvas.draw_rect(
skia_safe::Rect {
left: 10.0,
top: 10.0,
right: 890.0,
bottom: 590.0,
},
&paint,
);
//TODO: draw_bitmap
let mut font = skia_safe::Font::default();
font.set_size(100.0);
canvas.draw_str("Hello Skulpin", (65, 200), &font, &paint);
canvas.draw_str("Hello Skulpin", (68, 203), &font, &paint);
canvas.draw_str("Hello Skulpin", (71, 206), &font, &paint);
}