2019-10-07 17:17:57 +03:00
|
|
|
use ::window::*;
|
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
struct MyWindow {
|
|
|
|
allow_close: bool,
|
|
|
|
cursor_pos: (u16, u16),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowCallbacks for MyWindow {
|
|
|
|
fn destroy(&mut self) {
|
|
|
|
Connection::get().unwrap().terminate_message_loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(&mut self, context: &mut dyn PaintContext) {
|
|
|
|
// Window contents are black in software mode
|
|
|
|
context.clear(Color::rgb(0x0, 0x0, 0x0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "opengl")]
|
|
|
|
fn paint_opengl(&mut self, frame: &mut glium::Frame) {
|
|
|
|
// Window contents are gray in opengl mode
|
|
|
|
use glium::Surface;
|
2020-01-27 05:58:29 +03:00
|
|
|
frame.clear_color_srgb(0.15, 0.15, 0.15, 1.0);
|
2019-10-07 17:17:57 +03:00
|
|
|
}
|
|
|
|
|
2020-06-15 22:33:30 +03:00
|
|
|
#[cfg(feature = "opengl")]
|
|
|
|
fn opengl_initialize(
|
|
|
|
&mut self,
|
|
|
|
_window: &dyn WindowOps,
|
|
|
|
context: anyhow::Result<std::rc::Rc<glium::backend::Context>>,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
match context {
|
|
|
|
Ok(_ctx) => eprintln!("opengl enabled!"),
|
|
|
|
Err(err) => eprintln!("opengl fail: {}", err),
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:17:57 +03:00
|
|
|
fn as_any(&mut self) -> &mut dyn Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:43:05 +03:00
|
|
|
fn spawn_window() -> anyhow::Result<()> {
|
2019-10-07 17:17:57 +03:00
|
|
|
let win = Window::new_window(
|
|
|
|
"myclass",
|
|
|
|
"the title",
|
|
|
|
800,
|
|
|
|
600,
|
|
|
|
Box::new(MyWindow {
|
|
|
|
allow_close: false,
|
|
|
|
cursor_pos: (100, 200),
|
|
|
|
}),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
#[cfg(feature = "opengl")]
|
2020-06-15 22:33:30 +03:00
|
|
|
win.enable_opengl();
|
2019-10-07 17:17:57 +03:00
|
|
|
|
|
|
|
#[cfg(not(feature = "opengl"))]
|
|
|
|
eprintln!(
|
|
|
|
"opengl not enabled at compile time: cargo run --feature opengl --example basic_opengl"
|
|
|
|
);
|
|
|
|
|
|
|
|
win.show();
|
|
|
|
win.apply(|myself, _win| {
|
|
|
|
if let Some(myself) = myself.downcast_ref::<MyWindow>() {
|
|
|
|
eprintln!(
|
|
|
|
"got myself; allow_close={}, cursor_pos:{:?}",
|
|
|
|
myself.allow_close, myself.cursor_pos
|
|
|
|
);
|
|
|
|
}
|
2019-11-23 10:37:02 +03:00
|
|
|
Ok(())
|
2019-10-07 17:17:57 +03:00
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:43:05 +03:00
|
|
|
fn main() -> anyhow::Result<()> {
|
2019-11-30 05:05:09 +03:00
|
|
|
pretty_env_logger::init();
|
2019-10-07 17:17:57 +03:00
|
|
|
let conn = Connection::init()?;
|
|
|
|
spawn_window()?;
|
|
|
|
conn.run_message_loop()
|
|
|
|
}
|