summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..2ebdd28
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,132 @@
+#![forbid(unsafe_code)]
+// use vulkanalia::vk::{ DeviceV1_4, EntryV1_4, InstanceV1_4 };
+use winit::dpi::LogicalSize;
+use winit::application::ApplicationHandler;
+use winit::event::WindowEvent;
+use winit::event_loop::{ ActiveEventLoop, EventLoop };
+use winit::window::{ Window, WindowAttributes, WindowId };
+
+#[derive(Debug)]
+struct Error {
+  message: String,
+}
+type Result<T> = std::result::Result<T, Error>;
+
+impl std::fmt::Display for Error {
+  fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>)
+      -> std::result::Result<(), std::fmt::Error>
+  {
+    fmt.write_str(&self.message)
+  }
+}
+
+impl From<winit::error::EventLoopError> for Error {
+  fn from(e: winit::error::EventLoopError) -> Self {
+    match e {
+      winit::error::EventLoopError::NotSupported(e) => Self::from(e),
+      winit::error::EventLoopError::Os(e) => Self::from(e),
+      winit::error::EventLoopError::RecreationAttempt => Error {
+        message:
+            "There may only ever be a single winit event loop.".to_string()
+      },
+      winit::error::EventLoopError::ExitFailure(code) => Error {
+        message:
+            format!("Clean unhappy exit with code {} via winit event loop",
+                    code)
+      }
+    }
+  }
+}
+
+impl From<winit::error::NotSupportedError> for Error {
+  fn from(e: winit::error::NotSupportedError) -> Self {
+    Error {
+      message:
+          format!("The winit backend does not support an operation: {}",
+                  e.to_string())
+    }
+  }
+}
+
+impl From<winit::error::OsError> for Error {
+  fn from(e: winit::error::OsError) -> Self {
+    Error {
+      message: format!("The OS told winit about an error: {}", e.to_string())
+    }
+  }
+}
+
+
+struct Surreality {
+  window: Window
+}
+
+impl Surreality {
+  fn new(window: Window) -> Result<Self> {
+    Ok(Surreality {
+      window
+    })
+  }
+
+  fn render(&mut self, window_id: WindowId) -> Result<()> {
+    if window_id == self.window.id() {
+      println!("render the window");
+    } else {
+      println!("render something unknown");
+    }
+
+    Ok(())
+  }
+}
+
+impl ApplicationHandler for Surreality {
+  fn resumed(&mut self, event_loop: &ActiveEventLoop) {
+    println!("resumed");
+  }
+
+  fn window_event(&mut self, event_loop: &ActiveEventLoop,
+                  window_id: WindowId, event: WindowEvent)
+  {
+    println!("window event {:?}", event);
+
+    match event {
+      WindowEvent::RedrawRequested => {
+        if !event_loop.exiting() {
+          self.render(window_id).unwrap(); // TODO
+        }
+      }
+      WindowEvent::CloseRequested => {
+        event_loop.exit();
+      }
+      _ => { }
+    }
+  }
+}
+
+
+fn main() -> std::process::ExitCode {
+  let body: fn() -> Result<()>  = || {
+    let event_loop = EventLoop::new()?;
+
+    let window_attributes = WindowAttributes::default()
+            .with_title("Love, Curiosity, Justice")
+            .with_inner_size(LogicalSize::new(1024, 768));
+    let window = event_loop.create_window(window_attributes)?;
+
+    let mut surreality = Surreality::new(window)?;
+
+    event_loop.run_app(&mut surreality)?;
+
+    Ok(())
+  };
+
+  println!("Hello!");
+  match body() {
+    Ok(()) => std::process::ExitCode::SUCCESS,
+    Err(e) => {
+      eprintln!("Error: {}", e);
+      std::process::ExitCode::from(1)
+    }
+  }
+}
+