summary refs log tree commit diff
path: root/src/main.rs
blob: 2ebdd285a2d6e65da178a834dffdfc0518a194e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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)
    }
  }
}