Skip to content

Commit

Permalink
Port examples to use the new softbuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Jul 10, 2023
1 parent e215150 commit bd0551e
Show file tree
Hide file tree
Showing 31 changed files with 252 additions and 147 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ image = { version = "0.24.0", default-features = false, features = ["png"] }
simple_logger = { version = "2.1.0", default_features = false }

[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dev-dependencies]
softbuffer = "0.3.0"
softbuffer = { git = "https://github.com/rust-windowing/softbuffer.git", branch = "notgull/rwh-v0.6" }

[target.'cfg(target_os = "android")'.dependencies]
# Coordinate the next winit release with android-ndk-rs: https://github.com/rust-windowing/winit/issues/1995
Expand Down
19 changes: 11 additions & 8 deletions examples/child_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod fill;
#[cfg(any(x11_platform, macos_platform, windows_platform))]
fn main() {
use std::collections::HashMap;
use std::rc::Rc;

use raw_window_handle::HasRawWindowHandle;
use winit::{
Expand All @@ -17,7 +18,7 @@ fn main() {
fn spawn_child_window(
parent: &Window,
event_loop: &EventLoopWindowTarget<()>,
windows: &mut HashMap<WindowId, Window>,
windows: &mut HashMap<WindowId, Rc<Window>>,
) {
let parent = parent.raw_window_handle().unwrap();
let mut builder = WindowBuilder::new()
Expand All @@ -27,7 +28,7 @@ fn main() {
.with_visible(true);
// `with_parent_window` is unsafe. Parent window must be a valid window.
builder = unsafe { builder.with_parent_window(Some(parent)) };
let child_window = builder.build(event_loop).unwrap();
let child_window = Rc::new(builder.build(event_loop).unwrap());

let id = child_window.id();
windows.insert(id, child_window);
Expand All @@ -37,12 +38,14 @@ fn main() {
let mut windows = HashMap::new();

let event_loop: EventLoop<()> = EventLoop::new();
let parent_window = WindowBuilder::new()
.with_title("parent window")
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
.with_inner_size(LogicalSize::new(640.0f32, 480.0f32))
.build(&event_loop)
.unwrap();
let parent_window = Rc::new(
WindowBuilder::new()
.with_title("parent window")
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
.with_inner_size(LogicalSize::new(640.0f32, 480.0f32))
.build(&event_loop)
.unwrap(),
);

println!("parent window: {parent_window:?})");

Expand Down
13 changes: 9 additions & 4 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::single_match)]

use std::rc::Rc;
use std::thread;
#[cfg(not(wasm_platform))]
use std::time;
Expand Down Expand Up @@ -37,10 +38,14 @@ fn main() {
println!("Press 'Esc' to close the window.");

let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title(
"Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.",
)
.build(&event_loop)
.unwrap(),
);

let mut mode = Mode::Wait;
let mut request_redraw = false;
Expand Down
3 changes: 2 additions & 1 deletion examples/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
Expand All @@ -14,7 +15,7 @@ fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let window = WindowBuilder::new().build(&event_loop).unwrap();
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
window.set_title("A fantastic window!");

let mut cursor_idx = 0;
Expand Down
11 changes: 7 additions & 4 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::{
event::{DeviceEvent, ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
Expand All @@ -15,10 +16,12 @@ fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_title("Super Cursor Grab'n'Hide Simulator 9000")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title("Super Cursor Grab'n'Hide Simulator 9000")
.build(&event_loop)
.unwrap(),
);

let mut modifiers = ModifiersState::default();

Expand Down
11 changes: 7 additions & 4 deletions examples/custom_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(not(wasm_platform))]
fn main() {
use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::{
event::{Event, WindowEvent},
event_loop::EventLoopBuilder,
Expand All @@ -20,10 +21,12 @@ fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoopBuilder::<CustomEvent>::with_user_event().build();

let window = WindowBuilder::new()
.with_title("A fantastic window!")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title("A fantastic window!")
.build(&event_loop)
.unwrap(),
);

// `EventLoopProxy` allows you to dispatch custom events to the main Winit event
// loop from any thread.
Expand Down
5 changes: 3 additions & 2 deletions examples/drag_window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::{
event::{ElementState, Event, KeyEvent, MouseButton, StartCause, WindowEvent},
event_loop::EventLoop,
Expand All @@ -15,8 +16,8 @@ fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let window_1 = WindowBuilder::new().build(&event_loop).unwrap();
let window_2 = WindowBuilder::new().build(&event_loop).unwrap();
let window_1 = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
let window_2 = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());

let mut switched = false;
let mut entered_id = window_2.id();
Expand Down
11 changes: 7 additions & 4 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
use winit::event_loop::EventLoop;
use winit::keyboard::Key;
Expand All @@ -19,10 +20,12 @@ fn main() {
let mut decorations = true;
let mut minimized = false;

let window = WindowBuilder::new()
.with_title("Hello world!")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title("Hello world!")
.build(&event_loop)
.unwrap(),
);

let mut monitor_index = 0;
let mut monitor = event_loop
Expand Down
11 changes: 7 additions & 4 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
Expand All @@ -15,10 +16,12 @@ fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_title("Your faithful window")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title("Your faithful window")
.build(&event_loop)
.unwrap(),
);

let mut close_requested = false;

Expand Down
11 changes: 7 additions & 4 deletions examples/ime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use log::LevelFilter;
use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::{ElementState, Event, Ime, WindowEvent},
Expand All @@ -26,10 +27,12 @@ fn main() {

let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_inner_size(winit::dpi::LogicalSize::new(256f64, 128f64))
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_inner_size(winit::dpi::LogicalSize::new(256f64, 128f64))
.build(&event_loop)
.unwrap(),
);

let mut ime_purpose = ImePurpose::Normal;
let mut ime_allowed = true;
Expand Down
12 changes: 8 additions & 4 deletions examples/key_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ fn main() {

#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
fn main() {
use std::rc::Rc;

#[path = "util/fill.rs"]
mod fill;

simple_logger::SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_inner_size(LogicalSize::new(400.0, 200.0))
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_inner_size(LogicalSize::new(400.0, 200.0))
.build(&event_loop)
.unwrap(),
);

let mut modifiers = ModifiersState::default();

Expand Down
11 changes: 7 additions & 4 deletions examples/mouse_wheel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::{
event::{Event, WindowEvent},
event_loop::EventLoop,
Expand All @@ -14,10 +15,12 @@ fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_title("Mouse Wheel events")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title("Mouse Wheel events")
.build(&event_loop)
.unwrap(),
);

println!(
r"
Expand Down
17 changes: 12 additions & 5 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

#[cfg(not(wasm_platform))]
fn main() {
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};
use std::{
collections::HashMap,
sync::{mpsc, Arc},
thread,
time::Duration,
};

use simple_logger::SimpleLogger;
use winit::{
Expand All @@ -20,10 +25,12 @@ fn main() {
let event_loop = EventLoop::new();
let mut window_senders = HashMap::with_capacity(WINDOW_COUNT);
for _ in 0..WINDOW_COUNT {
let window = WindowBuilder::new()
.with_inner_size(WINDOW_SIZE)
.build(&event_loop)
.unwrap();
let window = Arc::new(
WindowBuilder::new()
.with_inner_size(WINDOW_SIZE)
.build(&event_loop)
.unwrap(),
);

let mut video_modes: Vec<_> = window.current_monitor().unwrap().video_modes().collect();
let mut video_mode_id = 0usize;
Expand Down
5 changes: 3 additions & 2 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use std::collections::HashMap;
use std::rc::Rc;

use simple_logger::SimpleLogger;
use winit::{
Expand All @@ -19,7 +20,7 @@ fn main() {

let mut windows = HashMap::new();
for _ in 0..3 {
let window = Window::new(&event_loop).unwrap();
let window = Rc::new(Window::new(&event_loop).unwrap());
println!("Opened a new window: {:?}", window.id());
windows.insert(window.id(), window);
}
Expand Down Expand Up @@ -52,7 +53,7 @@ fn main() {
is_synthetic: false,
..
} if matches!(c.as_ref(), "n" | "N") => {
let window = Window::new(event_loop).unwrap();
let window = Rc::new(Window::new(event_loop).unwrap());
println!("Opened a new window: {:?}", window.id());
windows.insert(window.id(), window);
}
Expand Down
11 changes: 7 additions & 4 deletions examples/request_redraw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use simple_logger::SimpleLogger;
use std::rc::Rc;
use winit::{
event::{ElementState, Event, WindowEvent},
event_loop::EventLoop,
Expand All @@ -14,10 +15,12 @@ fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_title("A fantastic window!")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title("A fantastic window!")
.build(&event_loop)
.unwrap(),
);

event_loop.run(move |event, _, control_flow| {
println!("{event:?}");
Expand Down
Loading

0 comments on commit bd0551e

Please sign in to comment.