hello world
This commit is contained in:
+44
@@ -0,0 +1,44 @@
|
|||||||
|
use winit::application::ApplicationHandler;
|
||||||
|
use winit::dpi::LogicalSize;
|
||||||
|
use winit::event::WindowEvent;
|
||||||
|
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
||||||
|
use winit::window::{Window, WindowAttributes, WindowId};
|
||||||
|
|
||||||
|
struct App {
|
||||||
|
window: Option<Window>,
|
||||||
|
attr: WindowAttributes,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
pub fn new(attr: WindowAttributes) -> App {
|
||||||
|
App { window: None, attr }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ApplicationHandler for App {
|
||||||
|
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||||
|
self.window = Some(event_loop.create_window(self.attr.clone()).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
event_loop.exit();
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
self.window.as_ref().unwrap().request_redraw();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(width: f64, height: f64) {
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
event_loop.set_control_flow(ControlFlow::Poll);
|
||||||
|
event_loop.set_control_flow(ControlFlow::Wait);
|
||||||
|
let mut w = WindowAttributes::default();
|
||||||
|
w.inner_size = Some(winit::dpi::Size::Logical(LogicalSize::new(width, height)));
|
||||||
|
let mut app = App::new(w);
|
||||||
|
event_loop.run_app(&mut app).unwrap();
|
||||||
|
}
|
||||||
+14
-9
@@ -1,14 +1,19 @@
|
|||||||
pub fn add(left: u64, right: u64) -> u64 {
|
mod app;
|
||||||
left + right
|
|
||||||
|
pub struct Window {
|
||||||
|
width: f64,
|
||||||
|
height: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
impl Window {
|
||||||
mod tests {
|
pub fn new() -> Window {
|
||||||
use super::*;
|
Window {
|
||||||
|
width: 1000.0,
|
||||||
|
height: 650.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
pub fn run(&self) {
|
||||||
fn it_works() {
|
app::run(self.width, self.height);
|
||||||
let result = add(2, 2);
|
|
||||||
assert_eq!(result, 4);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-36
@@ -1,39 +1,7 @@
|
|||||||
use winit::application::ApplicationHandler;
|
use osgui::Window;
|
||||||
use winit::event::WindowEvent;
|
|
||||||
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
|
||||||
use winit::window::{Window, WindowId};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct App {
|
|
||||||
window: Option<Window>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ApplicationHandler for App {
|
|
||||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
|
||||||
self.window = Some(
|
|
||||||
event_loop
|
|
||||||
.create_window(Window::default_attributes())
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
|
||||||
match event {
|
|
||||||
WindowEvent::CloseRequested => {
|
|
||||||
event_loop.exit();
|
|
||||||
}
|
|
||||||
WindowEvent::RedrawRequested => {
|
|
||||||
self.window.as_ref().unwrap().request_redraw();
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let event_loop = EventLoop::new().unwrap();
|
let win = Window::new();
|
||||||
event_loop.set_control_flow(ControlFlow::Poll);
|
|
||||||
event_loop.set_control_flow(ControlFlow::Wait);
|
win.run();
|
||||||
let mut app = App::default();
|
|
||||||
event_loop.run_app(&mut app).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user