Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Keep Instance in GpuState, so that it is not being dropped early.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzm3r committed Oct 24, 2022
1 parent 618fd78 commit ba48d62
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions examples/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use piet_gpu_hal::{
ImageFormat, ImageLayout, Instance, InstanceFlags, Pipeline, Semaphore, Session, Swapchain,
};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use tracing::info;

#[derive(Default)]
struct HelloState {
Expand All @@ -40,7 +41,7 @@ impl WinHandler for HelloState {
}

fn size(&mut self, size: Size) {
println!("size: {:?}", size);
info!("size: {:?}", size);
self.size = size;
}

Expand Down Expand Up @@ -107,7 +108,7 @@ impl WinHandler for HelloState {
.unwrap();
let start = std::time::Instant::now();
submitted.wait().unwrap();
println!("wait elapsed: {:?}", start.elapsed());
info!("wait elapsed: {:?}", start.elapsed());
state.current_frame += 1;
}
}
Expand All @@ -134,55 +135,55 @@ impl WinHandler for HelloState {
]);
self.handle.save_as(options);
}
_ => println!("unexpected id {}", id),
_ => info!("unexpected id {}", id),
}
}

fn save_as(&mut self, _token: FileDialogToken, file: Option<FileInfo>) {
println!("save file result: {:?}", file);
info!("save file result: {:?}", file);
}

fn open_file(&mut self, _token: FileDialogToken, file_info: Option<FileInfo>) {
println!("open file result: {:?}", file_info);
info!("open file result: {:?}", file_info);
}

fn key_down(&mut self, event: KeyEvent) -> bool {
println!("keydown: {:?}", event);
info!("keydown: {:?}", event);
false
}

fn key_up(&mut self, event: KeyEvent) {
println!("keyup: {:?}", event);
info!("keyup: {:?}", event);
}

fn wheel(&mut self, event: &MouseEvent) {
println!("mouse_wheel {:?}", event);
info!("mouse_wheel {:?}", event);
}

fn mouse_move(&mut self, event: &MouseEvent) {
self.handle.set_cursor(&Cursor::Arrow);
println!("mouse_move {:?}", event);
info!("mouse_move {:?}", event);
}

fn mouse_down(&mut self, event: &MouseEvent) {
println!("mouse_down {:?}", event);
info!("mouse_down {:?}", event);
self.render();
}

fn mouse_up(&mut self, event: &MouseEvent) {
println!("mouse_up {:?}", event);
info!("mouse_up {:?}", event);
}

fn timer(&mut self, id: TimerToken) {
println!("timer fired: {:?}", id);
info!("timer fired: {:?}", id);
}

fn got_focus(&mut self) {
println!("Got focus");
info!("Got focus");
}

fn lost_focus(&mut self) {
println!("Lost focus");
info!("Lost focus");
}

fn request_close(&mut self) {
Expand Down Expand Up @@ -258,14 +259,15 @@ impl HelloState {
.unwrap();
let start = std::time::Instant::now();
submitted.wait().unwrap();
println!("wait elapsed: {:?}", start.elapsed());
info!("wait elapsed: {:?}", start.elapsed());
state.current_frame += 1;
}
}
}

fn main() {
tracing_subscriber::fmt().init();
info!("Building file menu");
let mut file_menu = Menu::new();
file_menu.add_item(
0x100,
Expand All @@ -292,7 +294,9 @@ fn main() {
menubar.add_dropdown(Menu::new(), "Application", true);
menubar.add_dropdown(file_menu, "&File", true);

info!("Building application");
let app = Application::new().unwrap();
info!("Building window");
let mut builder = WindowBuilder::new(app.clone());
let win_state = HelloState::default();
let gpu_state = win_state.gpu_state.clone();
Expand All @@ -307,12 +311,15 @@ fn main() {
let state = GpuState::new(&window, width, height).unwrap();
*gpu_state.lock().unwrap() = Some(state);
}
info!("Showing window");
window.show();

info!("Running app");
app.run(None);
}

struct GpuState {
_instance: Instance,
current_frame: usize,
session: Session,
swapchain: Swapchain,
Expand Down Expand Up @@ -355,6 +362,7 @@ impl GpuState {
.build(&session, &pipeline)?;
let current_frame = 0;
Ok(GpuState {
_instance: instance,
current_frame,
session,
swapchain,
Expand Down

0 comments on commit ba48d62

Please sign in to comment.