Skip to content

Commit

Permalink
changed State to own window
Browse files Browse the repository at this point in the history
  • Loading branch information
sotrh committed Jan 14, 2023
1 parent a9230e9 commit 09207e9
Show file tree
Hide file tree
Showing 30 changed files with 444 additions and 157 deletions.
24 changes: 18 additions & 6 deletions code/beginner/tutorial2-surface/src/challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ struct State {
config: wgpu::SurfaceConfiguration,
size: winit::dpi::PhysicalSize<u32>,
clear_color: wgpu::Color,
window: Window,
}

impl State {
async fn new(window: &Window) -> Self {
async fn new(window: Window) -> Self {
let size = window.inner_size();

// The instance is a handle to our GPU
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
let instance = wgpu::Instance::new(wgpu::Backends::all());
let surface = unsafe { instance.create_surface(window) };

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&window) };

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
Expand Down Expand Up @@ -75,9 +82,14 @@ impl State {
config,
clear_color,
size,
window,
}
}

fn window(&self) -> &Window {
&self.window
}

pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
Expand Down Expand Up @@ -148,14 +160,14 @@ async fn run() {
let window = WindowBuilder::new().build(&event_loop).unwrap();

// State::new uses async code, so we're going to wait for it to finish
let mut state = State::new(&window).await;
let mut state = State::new(window).await;

event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
} if window_id == state.window().id() => {
if !state.input(event) {
match event {
WindowEvent::CloseRequested
Expand All @@ -179,7 +191,7 @@ async fn run() {
}
}
}
Event::RedrawRequested(window_id) if window_id == window.id() => {
Event::RedrawRequested(window_id) if window_id == state.window().id() => {
state.update();
match state.render() {
Ok(_) => {}
Expand All @@ -192,7 +204,7 @@ async fn run() {
}
}
Event::MainEventsCleared => {
window.request_redraw();
state.window().request_redraw();
}
_ => {}
}
Expand Down
24 changes: 18 additions & 6 deletions code/beginner/tutorial2-surface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ struct State {
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,
size: winit::dpi::PhysicalSize<u32>,
window: Window,
}

impl State {
async fn new(window: &Window) -> Self {
async fn new(window: Window) -> Self {
let size = window.inner_size();

// The instance is a handle to our GPU
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
let instance = wgpu::Instance::new(wgpu::Backends::all());
let surface = unsafe { instance.create_surface(window) };

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&window) };

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
Expand Down Expand Up @@ -69,9 +76,14 @@ impl State {
queue,
config,
size,
window,
}
}

fn window(&self) -> &Window {
&self.window
}

pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
Expand Down Expand Up @@ -161,14 +173,14 @@ pub async fn run() {
}

// State::new uses async code, so we're going to wait for it to finish
let mut state = State::new(&window).await;
let mut state = State::new(window).await;

event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
} if window_id == state.window().id() => {
if !state.input(event) {
// UPDATED!
match event {
Expand All @@ -193,7 +205,7 @@ pub async fn run() {
}
}
}
Event::RedrawRequested(window_id) if window_id == window.id() => {
Event::RedrawRequested(window_id) if window_id == state.window().id() => {
state.update();
match state.render() {
Ok(_) => {}
Expand All @@ -208,7 +220,7 @@ pub async fn run() {
Event::RedrawEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
window.request_redraw();
state.window().request_redraw();
}
_ => {}
}
Expand Down
24 changes: 18 additions & 6 deletions code/beginner/tutorial3-pipeline/src/challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ struct State {
render_pipeline: wgpu::RenderPipeline,
challenge_render_pipeline: wgpu::RenderPipeline,
use_color: bool,
window: Window,
}

impl State {
async fn new(window: &Window) -> Self {
async fn new(window: Window) -> Self {
let size = window.inner_size();

// The instance is a handle to our GPU
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
let instance = wgpu::Instance::new(wgpu::Backends::all());
let surface = unsafe { instance.create_surface(window) };

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&window) };

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
Expand Down Expand Up @@ -172,9 +179,14 @@ impl State {
challenge_render_pipeline,
use_color,
size,
window,
}
}

fn window(&self) -> &Window {
&self.window
}

pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
Expand Down Expand Up @@ -260,14 +272,14 @@ async fn run() {
let window = WindowBuilder::new().build(&event_loop).unwrap();

// State::new uses async code, so we're going to wait for it to finish
let mut state = State::new(&window).await;
let mut state = State::new(window).await;

event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
} if window_id == state.window().id() => {
if !state.input(event) {
match event {
WindowEvent::CloseRequested
Expand All @@ -291,7 +303,7 @@ async fn run() {
}
}
}
Event::RedrawRequested(window_id) if window_id == window.id() => {
Event::RedrawRequested(window_id) if window_id == state.window().id() => {
state.update();
match state.render() {
Ok(_) => {}
Expand All @@ -306,7 +318,7 @@ async fn run() {
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
window.request_redraw();
state.window().request_redraw();
}
_ => {}
}
Expand Down
24 changes: 18 additions & 6 deletions code/beginner/tutorial3-pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ struct State {
size: winit::dpi::PhysicalSize<u32>,
// NEW!
render_pipeline: wgpu::RenderPipeline,
window: Window,
}

impl State {
async fn new(window: &Window) -> Self {
async fn new(window: Window) -> Self {
let size = window.inner_size();

// The instance is a handle to our GPU
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
let instance = wgpu::Instance::new(wgpu::Backends::all());
let surface = unsafe { instance.create_surface(window) };

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&window) };

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
Expand Down Expand Up @@ -127,9 +134,14 @@ impl State {
size,
config,
render_pipeline,
window,
}
}

pub fn window(&self) -> &Window {
&self.window
}

pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
Expand Down Expand Up @@ -222,14 +234,14 @@ pub async fn run() {
}

// State::new uses async code, so we're going to wait for it to finish
let mut state = State::new(&window).await;
let mut state = State::new(window).await;

event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
} if window_id == state.window().id() => {
if !state.input(event) {
match event {
WindowEvent::CloseRequested
Expand All @@ -253,7 +265,7 @@ pub async fn run() {
}
}
}
Event::RedrawRequested(window_id) if window_id == window.id() => {
Event::RedrawRequested(window_id) if window_id == state.window().id() => {
state.update();
match state.render() {
Ok(_) => {}
Expand All @@ -268,7 +280,7 @@ pub async fn run() {
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
window.request_redraw();
state.window().request_redraw();
}
_ => {}
}
Expand Down
24 changes: 18 additions & 6 deletions code/beginner/tutorial4-buffer/src/challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,23 @@ struct State {
use_complex: bool,

size: winit::dpi::PhysicalSize<u32>,
window: Window,
}

impl State {
async fn new(window: &Window) -> Self {
async fn new(window: Window) -> Self {
let size = window.inner_size();

// The instance is a handle to our GPU
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
let instance = wgpu::Instance::new(wgpu::Backends::all());
let surface = unsafe { instance.create_surface(window) };

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&window) };

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
Expand Down Expand Up @@ -240,9 +247,14 @@ impl State {
num_challenge_indices,
use_complex,
size,
window,
}
}

pub fn window(&self) -> &Window {
&self.window
}

pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
Expand Down Expand Up @@ -337,14 +349,14 @@ async fn run() {
let window = WindowBuilder::new().build(&event_loop).unwrap();

// State::new uses async code, so we're going to wait for it to finish
let mut state = State::new(&window).await;
let mut state = State::new(window).await;

event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
} if window_id == state.window().id() => {
if !state.input(event) {
match event {
WindowEvent::CloseRequested
Expand All @@ -368,7 +380,7 @@ async fn run() {
}
}
}
Event::RedrawRequested(window_id) if window_id == window.id() => {
Event::RedrawRequested(window_id) if window_id == state.window().id() => {
state.update();
match state.render() {
Ok(_) => {}
Expand All @@ -383,7 +395,7 @@ async fn run() {
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
window.request_redraw();
state.window().request_redraw();
}
_ => {}
}
Expand Down
Loading

0 comments on commit 09207e9

Please sign in to comment.