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:
Steins7 2022-06-30 21:12:51 +02:00
parent ecbcbf0db3
commit 313d311be8
3 changed files with 118 additions and 72 deletions

View File

@ -1,6 +1,8 @@
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use raw_window_handle::HasRawWindowHandle;
use crate::{
io::{Key, Scroll},
sprite::{Sprite, TextureSprite, TextSprite, ShapeSprite},
@ -26,19 +28,10 @@ pub struct Canvas {}
impl Canvas {
pub fn create<T: Into<String>, A: Application>(title: T, size: Size, app: A) {
use winit::{
event_loop::EventLoop,
window::WindowBuilder,
};
pub fn create<W: HasRawWindowHandle>(_window: &W) -> Result<Canvas, &'static str> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_inner_size(size)
.with_resizable(false)
.with_title(title)
.build(&event_loop)
.unwrap();
Ok(Self {
})
}
//--Create functions--
@ -112,7 +105,7 @@ impl Canvas {
}
pub fn update(&mut self) {
unimplemented!();
//unimplemented!();
}
}

View File

@ -1,3 +1,6 @@
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
mod canvas;
pub use canvas::Canvas;
pub use canvas::Application;
@ -9,9 +12,57 @@ pub mod utils;
use utils::Size;
pub fn create_vk_canvas<A: Application>(_title: &'static str, _size: Size, app: A) -> !
{
unimplemented!();
pub fn run_vk_canvas<A: 'static + Application>(title: &'static str, size: Size, mut app: A) -> ! {
use winit::{
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)]
@ -61,7 +112,7 @@ mod tests {
fn vk_create() {
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 {});
}
}

View File

@ -42,67 +42,69 @@ impl Application for ExampleApp {
shape::{Shape, Rectangle},
};
// inputs
if canvas.key_pressed(Key::A) {
unimplemented!();
}
//// inputs
//if canvas.key_pressed(Key::A) {
// unimplemented!();
//}
//
//if canvas.key_released(Key::A) {
// unimplemented!();
//}
//
//match canvas.get_key_presses() {
// Key::A => unimplemented!(),
// _ => (),
//}
//
//match canvas.get_key_releases() {
// Key::A => unimplemented!(),
// _ => (),
//}
if canvas.key_released(Key::A) {
unimplemented!();
}
//let _mouse = canvas.get_mouse_position();
match canvas.get_key_presses() {
Key::A => unimplemented!(),
_ => (),
}
//// outputs
//canvas.set_clear_color(Color::BLACK);
//canvas.clear();
match canvas.get_key_releases() {
Key::A => unimplemented!(),
_ => (),
}
//// white rectangle inset by one in the 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_color(Color::WHITE);
//canvas.draw(bas_sprite);
let _mouse = canvas.get_mouse_position();
//// 20 x 20 sprite of a picture
//let texture = canvas.create_texture_from_file(
// Position {x: 0, y: 0},
// "texture.png",
// Some(Color::WHITE)
//);
//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}));
//canvas.draw(pix_sprite);
// outputs
canvas.set_clear_color(Color::BLACK);
canvas.clear();
//// scaled sprite of a manually drawed texture
//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));
//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.set_texture(texture, None);
//let _ = pix_sprite.iter_mut(); //only iterate on the part used by the sprite
//canvas.draw(pix_sprite);
// white rectangle inset by one in the 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_color(Color::WHITE);
canvas.draw(bas_sprite);
//// floating text
//let mut txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
//txt_sprite.set_text("text");
//txt_sprite.set_color(Color::BLACK);
// 20 x 20 sprite of a picture
let texture = canvas.create_texture_from_file(
Position {x: 0, y: 0},
"texture.png",
Some(Color::WHITE)
);
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}));
canvas.draw(pix_sprite);
//// generic operations
//txt_sprite.set_position(Position {x: 0, y: 0});
//txt_sprite.set_rotation(50.0);
//txt_sprite.set_alpha(255);
//txt_sprite.set_scale(0.5);
//canvas.draw(txt_sprite);
// scaled sprite of a manually drawed texture
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));
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.set_texture(texture, None);
let _ = pix_sprite.iter_mut(); //only iterate on the part used by the sprite
canvas.draw(pix_sprite);
// floating text
let mut txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
txt_sprite.set_text("text");
txt_sprite.set_color(Color::BLACK);
// generic operations
txt_sprite.set_position(Position {x: 0, y: 0});
txt_sprite.set_rotation(50.0);
txt_sprite.set_alpha(255);
txt_sprite.set_scale(0.5);
canvas.draw(txt_sprite);
//canvas.update();
Ok(())
}
@ -113,6 +115,6 @@ fn main() -> Result<(), &'static str> {
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 {});
}