Configured winit window
+ constructed winit's window and event loop ! window will not show up until we draw something on it
This commit is contained in:
parent
ecbcbf0db3
commit
313d311be8
@ -1,6 +1,8 @@
|
|||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use log::{debug, error, info, trace, warn};
|
use log::{debug, error, info, trace, warn};
|
||||||
|
|
||||||
|
use raw_window_handle::HasRawWindowHandle;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
io::{Key, Scroll},
|
io::{Key, Scroll},
|
||||||
sprite::{Sprite, TextureSprite, TextSprite, ShapeSprite},
|
sprite::{Sprite, TextureSprite, TextSprite, ShapeSprite},
|
||||||
@ -26,19 +28,10 @@ pub struct Canvas {}
|
|||||||
|
|
||||||
impl Canvas {
|
impl Canvas {
|
||||||
|
|
||||||
pub fn create<T: Into<String>, A: Application>(title: T, size: Size, app: A) {
|
pub fn create<W: HasRawWindowHandle>(_window: &W) -> Result<Canvas, &'static str> {
|
||||||
use winit::{
|
|
||||||
event_loop::EventLoop,
|
|
||||||
window::WindowBuilder,
|
|
||||||
};
|
|
||||||
|
|
||||||
let event_loop = EventLoop::new();
|
Ok(Self {
|
||||||
let window = WindowBuilder::new()
|
})
|
||||||
.with_inner_size(size)
|
|
||||||
.with_resizable(false)
|
|
||||||
.with_title(title)
|
|
||||||
.build(&event_loop)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--Create functions--
|
//--Create functions--
|
||||||
@ -112,7 +105,7 @@ impl Canvas {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
unimplemented!();
|
//unimplemented!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
59
src/lib.rs
59
src/lib.rs
@ -1,3 +1,6 @@
|
|||||||
|
#[allow(unused_imports)]
|
||||||
|
use log::{debug, error, info, trace, warn};
|
||||||
|
|
||||||
mod canvas;
|
mod canvas;
|
||||||
pub use canvas::Canvas;
|
pub use canvas::Canvas;
|
||||||
pub use canvas::Application;
|
pub use canvas::Application;
|
||||||
@ -9,9 +12,57 @@ pub mod utils;
|
|||||||
|
|
||||||
use utils::Size;
|
use utils::Size;
|
||||||
|
|
||||||
pub fn create_vk_canvas<A: Application>(_title: &'static str, _size: Size, app: A) -> !
|
pub fn run_vk_canvas<A: 'static + Application>(title: &'static str, size: Size, mut app: A) -> ! {
|
||||||
{
|
use winit::{
|
||||||
unimplemented!();
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
// construct window
|
||||||
|
let event_loop = EventLoop::new();
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_inner_size(size)
|
||||||
|
.with_resizable(false)
|
||||||
|
.with_title(title)
|
||||||
|
.with_visible(false) //keep window invisible until we are ready to write to it
|
||||||
|
.build(&event_loop)
|
||||||
|
.expect("Failed to create window");
|
||||||
|
|
||||||
|
// construct canvas
|
||||||
|
let mut canvas = Canvas::create(&window)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// init application
|
||||||
|
app.init(&mut canvas).unwrap();
|
||||||
|
canvas.update();
|
||||||
|
window.set_visible(true);
|
||||||
|
|
||||||
|
// let event loop highjack thread
|
||||||
|
event_loop.run(move |event, _, control_flow| {
|
||||||
|
use winit::{
|
||||||
|
event_loop::ControlFlow,
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
};
|
||||||
|
|
||||||
|
*control_flow = ControlFlow::Poll;
|
||||||
|
|
||||||
|
let _ = match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
info!("Close requested, shutting down...");
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
Event::MainEventsCleared => app.tick(&mut canvas),
|
||||||
|
Event::RedrawRequested(_) => {
|
||||||
|
canvas.update();
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
_ => Ok(())
|
||||||
|
}.unwrap();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -61,7 +112,7 @@ mod tests {
|
|||||||
fn vk_create() {
|
fn vk_create() {
|
||||||
use crate::utils::Size;
|
use crate::utils::Size;
|
||||||
|
|
||||||
crate::create_vk_canvas("vk_create", Size {w: 1280, h: 720}, TestApp {});
|
crate::run_vk_canvas("vk_create", Size {w: 1280, h: 720}, TestApp {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
112
src/main.rs
112
src/main.rs
@ -42,67 +42,69 @@ impl Application for ExampleApp {
|
|||||||
shape::{Shape, Rectangle},
|
shape::{Shape, Rectangle},
|
||||||
};
|
};
|
||||||
|
|
||||||
// inputs
|
//// inputs
|
||||||
if canvas.key_pressed(Key::A) {
|
//if canvas.key_pressed(Key::A) {
|
||||||
unimplemented!();
|
// unimplemented!();
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
if canvas.key_released(Key::A) {
|
//if canvas.key_released(Key::A) {
|
||||||
unimplemented!();
|
// unimplemented!();
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
match canvas.get_key_presses() {
|
//match canvas.get_key_presses() {
|
||||||
Key::A => unimplemented!(),
|
// Key::A => unimplemented!(),
|
||||||
_ => (),
|
// _ => (),
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
match canvas.get_key_releases() {
|
//match canvas.get_key_releases() {
|
||||||
Key::A => unimplemented!(),
|
// Key::A => unimplemented!(),
|
||||||
_ => (),
|
// _ => (),
|
||||||
}
|
//}
|
||||||
|
|
||||||
let _mouse = canvas.get_mouse_position();
|
//let _mouse = canvas.get_mouse_position();
|
||||||
|
|
||||||
// outputs
|
//// outputs
|
||||||
canvas.set_clear_color(Color::BLACK);
|
//canvas.set_clear_color(Color::BLACK);
|
||||||
canvas.clear();
|
//canvas.clear();
|
||||||
|
|
||||||
// white rectangle inset by one in the sprite
|
//// white rectangle inset by one in the sprite
|
||||||
let mut bas_sprite = canvas.create_shape_sprite();
|
//let mut bas_sprite = canvas.create_shape_sprite();
|
||||||
bas_sprite.set_shape(Shape::Rectangle (Rectangle {size: Size {w: 38, h: 38}}));
|
//bas_sprite.set_shape(Shape::Rectangle (Rectangle {size: Size {w: 38, h: 38}}));
|
||||||
bas_sprite.set_color(Color::WHITE);
|
//bas_sprite.set_color(Color::WHITE);
|
||||||
canvas.draw(bas_sprite);
|
//canvas.draw(bas_sprite);
|
||||||
|
|
||||||
// 20 x 20 sprite of a picture
|
//// 20 x 20 sprite of a picture
|
||||||
let texture = canvas.create_texture_from_file(
|
//let texture = canvas.create_texture_from_file(
|
||||||
Position {x: 0, y: 0},
|
// Position {x: 0, y: 0},
|
||||||
"texture.png",
|
// "texture.png",
|
||||||
Some(Color::WHITE)
|
// Some(Color::WHITE)
|
||||||
);
|
//);
|
||||||
let mut pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 1.0);
|
//let mut pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 1.0);
|
||||||
pix_sprite.set_texture(texture, Some(Position {x: 0, y: 0}));
|
//pix_sprite.set_texture(texture, Some(Position {x: 0, y: 0}));
|
||||||
canvas.draw(pix_sprite);
|
//canvas.draw(pix_sprite);
|
||||||
|
|
||||||
// scaled sprite of a manually drawed texture
|
//// scaled sprite of a manually drawed texture
|
||||||
let texture = canvas.create_texture(Size {w: 20, h: 20}, Some(Color::WHITE));
|
//let texture = canvas.create_texture(Size {w: 20, h: 20}, Some(Color::WHITE));
|
||||||
texture.borrow_mut().set_pixel(Position {x: 0, y: 0}, Pixel::rgb(0, 255, 0));
|
//texture.borrow_mut().set_pixel(Position {x: 0, y: 0}, Pixel::rgb(0, 255, 0));
|
||||||
let _ = texture.borrow_mut().iter_mut(); //iterate on whole texture
|
//let _ = texture.borrow_mut().iter_mut(); //iterate on whole texture
|
||||||
pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 2.0);
|
//pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 2.0);
|
||||||
pix_sprite.set_texture(texture, None);
|
//pix_sprite.set_texture(texture, None);
|
||||||
let _ = pix_sprite.iter_mut(); //only iterate on the part used by the sprite
|
//let _ = pix_sprite.iter_mut(); //only iterate on the part used by the sprite
|
||||||
canvas.draw(pix_sprite);
|
//canvas.draw(pix_sprite);
|
||||||
|
|
||||||
// floating text
|
//// floating text
|
||||||
let mut txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
|
//let mut txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
|
||||||
txt_sprite.set_text("text");
|
//txt_sprite.set_text("text");
|
||||||
txt_sprite.set_color(Color::BLACK);
|
//txt_sprite.set_color(Color::BLACK);
|
||||||
|
|
||||||
// generic operations
|
//// generic operations
|
||||||
txt_sprite.set_position(Position {x: 0, y: 0});
|
//txt_sprite.set_position(Position {x: 0, y: 0});
|
||||||
txt_sprite.set_rotation(50.0);
|
//txt_sprite.set_rotation(50.0);
|
||||||
txt_sprite.set_alpha(255);
|
//txt_sprite.set_alpha(255);
|
||||||
txt_sprite.set_scale(0.5);
|
//txt_sprite.set_scale(0.5);
|
||||||
canvas.draw(txt_sprite);
|
//canvas.draw(txt_sprite);
|
||||||
|
|
||||||
|
//canvas.update();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -113,6 +115,6 @@ fn main() -> Result<(), &'static str> {
|
|||||||
|
|
||||||
setup_logger()
|
setup_logger()
|
||||||
.map_err(|_| "Failed to setup logger")?;
|
.map_err(|_| "Failed to setup logger")?;
|
||||||
canvas::create_vk_canvas("vk_example", Size {w: 1280, h: 720}, ExampleApp {});
|
canvas::run_vk_canvas("vk_example", Size {w: 1280, h: 720}, ExampleApp {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user