2019-04-21 08:13:05 +00:00
|
|
|
#![deny(warnings)]
|
2020-05-10 17:04:10 +00:00
|
|
|
#![warn(clippy::all)]
|
|
|
|
|
2020-05-03 11:28:47 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2019-11-18 19:06:41 +00:00
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
use {
|
2020-05-11 18:21:24 +00:00
|
|
|
emigui::{examples::ExampleApp, widgets::*, *},
|
2020-04-29 19:58:14 +00:00
|
|
|
glium::glutin,
|
2019-03-12 21:59:55 +00:00
|
|
|
};
|
|
|
|
|
2020-05-10 19:13:39 +00:00
|
|
|
#[derive(Default, serde_derive::Deserialize, serde_derive::Serialize)]
|
|
|
|
struct Window {
|
|
|
|
pos: Option<Pos2>,
|
|
|
|
size: Option<Vec2>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_state(memory_json_path: impl AsRef<std::path::Path>) -> Option<Window> {
|
|
|
|
match std::fs::File::open(memory_json_path) {
|
|
|
|
Ok(file) => {
|
|
|
|
let reader = std::io::BufReader::new(file);
|
|
|
|
match serde_json::from_reader(reader) {
|
|
|
|
Ok(value) => Some(value),
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("ERROR: Failed to parse json: {}", err);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_err) => {
|
|
|
|
// File probably doesn't exist. That's fine.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
fn main() {
|
2020-05-10 19:13:39 +00:00
|
|
|
// TODO: combine
|
|
|
|
let memory_path = "emigui.json";
|
|
|
|
let settings_json_path: &str = "window.json";
|
|
|
|
|
|
|
|
let mut window_settings: Window = read_state(settings_json_path).unwrap_or_default();
|
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
let mut events_loop = glutin::EventsLoop::new();
|
|
|
|
let window = glutin::WindowBuilder::new().with_title("Emigui example");
|
|
|
|
let context = glutin::ContextBuilder::new();
|
|
|
|
let display = glium::Display::new(window, context, &events_loop).unwrap();
|
|
|
|
|
2020-05-10 19:13:39 +00:00
|
|
|
let size = window_settings.size.unwrap_or(vec2(1024.0, 800.0));
|
|
|
|
|
2020-04-22 18:01:49 +00:00
|
|
|
display
|
|
|
|
.gl_window()
|
|
|
|
.set_inner_size(glutin::dpi::LogicalSize {
|
2020-05-10 19:13:39 +00:00
|
|
|
width: size.x as f64,
|
|
|
|
height: size.y as f64,
|
2020-04-22 18:01:49 +00:00
|
|
|
});
|
2020-05-10 19:13:39 +00:00
|
|
|
|
|
|
|
if let Some(pos) = window_settings.pos {
|
|
|
|
display
|
|
|
|
.gl_window()
|
|
|
|
.set_position((pos.x as f64, pos.y as f64).into());
|
|
|
|
}
|
2020-04-22 18:01:49 +00:00
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
let pixels_per_point = display.gl_window().get_hidpi_factor() as f32;
|
|
|
|
|
2020-05-17 10:26:17 +00:00
|
|
|
let mut ctx = profile("initializing emilib", || Context::new(pixels_per_point));
|
|
|
|
let mut painter = profile("initializing painter", || {
|
|
|
|
emigui_glium::Painter::new(&display)
|
|
|
|
});
|
2019-03-12 21:59:55 +00:00
|
|
|
|
|
|
|
let mut raw_input = emigui::RawInput {
|
|
|
|
screen_size: {
|
|
|
|
let (width, height) = display.get_framebuffer_dimensions();
|
2019-03-16 11:57:44 +00:00
|
|
|
vec2(width as f32, height as f32) / pixels_per_point
|
2019-03-12 21:59:55 +00:00
|
|
|
},
|
|
|
|
pixels_per_point,
|
2019-04-21 08:13:05 +00:00
|
|
|
..Default::default()
|
2019-03-12 21:59:55 +00:00
|
|
|
};
|
|
|
|
|
2020-04-21 12:46:42 +00:00
|
|
|
// used to keep track of time for animations
|
|
|
|
let start_time = Instant::now();
|
2020-04-29 19:58:14 +00:00
|
|
|
let mut running = true;
|
2019-11-18 19:06:41 +00:00
|
|
|
let mut frame_start = Instant::now();
|
2020-05-03 11:28:47 +00:00
|
|
|
let mut frame_times = emigui::MovementTracker::new(1000, 1.0);
|
2020-05-12 05:27:14 +00:00
|
|
|
let mut example_app = ExampleApp::default();
|
2020-04-29 19:58:14 +00:00
|
|
|
let mut clipboard = emigui_glium::init_clipboard();
|
2020-04-29 19:25:49 +00:00
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
emigui_glium::read_memory(&ctx, memory_path);
|
2020-05-02 09:37:12 +00:00
|
|
|
|
2020-04-28 21:05:22 +00:00
|
|
|
while running {
|
2019-11-18 19:06:41 +00:00
|
|
|
{
|
|
|
|
// Keep smooth frame rate. TODO: proper vsync
|
|
|
|
let frame_duration = frame_start.elapsed();
|
2020-05-10 17:04:10 +00:00
|
|
|
if frame_duration < Duration::from_millis(33) {
|
|
|
|
std::thread::sleep(Duration::from_millis(33) - frame_duration);
|
2019-11-18 19:06:41 +00:00
|
|
|
}
|
|
|
|
frame_start = Instant::now();
|
|
|
|
}
|
2019-03-12 21:59:55 +00:00
|
|
|
|
2020-04-28 21:05:22 +00:00
|
|
|
{
|
|
|
|
raw_input.time = start_time.elapsed().as_nanos() as f64 * 1e-9;
|
2020-05-11 18:21:24 +00:00
|
|
|
raw_input.seconds_since_midnight = Some(emigui_glium::local_time_of_day());
|
2020-04-28 21:05:22 +00:00
|
|
|
raw_input.scroll_delta = vec2(0.0, 0.0);
|
|
|
|
raw_input.dropped_files.clear();
|
|
|
|
raw_input.hovered_files.clear();
|
2020-04-29 19:25:49 +00:00
|
|
|
raw_input.events.clear();
|
|
|
|
events_loop.poll_events(|event| {
|
2020-04-29 19:58:14 +00:00
|
|
|
emigui_glium::input_event(event, clipboard.as_mut(), &mut raw_input, &mut running)
|
2020-04-29 19:25:49 +00:00
|
|
|
});
|
2020-04-28 21:05:22 +00:00
|
|
|
}
|
2019-11-18 19:06:41 +00:00
|
|
|
|
2020-04-29 19:25:49 +00:00
|
|
|
let emigui_start = Instant::now();
|
2020-05-08 20:25:28 +00:00
|
|
|
ctx.begin_frame(raw_input.clone()); // TODO: avoid clone
|
2020-05-08 20:42:31 +00:00
|
|
|
let mut ui = ctx.fullscreen_ui();
|
2020-05-12 05:27:14 +00:00
|
|
|
example_app.ui(&mut ui);
|
2020-05-12 16:21:09 +00:00
|
|
|
let mut ui = ui.centered_column(ui.available().width().min(480.0));
|
2020-05-13 19:36:15 +00:00
|
|
|
ui.set_layout(Layout::vertical(Align::Min));
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.add(label!("Emigui running inside of Glium").text_style(emigui::TextStyle::Heading));
|
|
|
|
if ui.add(Button::new("Quit")).clicked {
|
2020-04-28 21:05:22 +00:00
|
|
|
running = false;
|
2019-03-12 21:59:55 +00:00
|
|
|
}
|
2020-04-21 18:48:31 +00:00
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.add(
|
2020-04-29 05:20:27 +00:00
|
|
|
label!(
|
2020-05-03 11:28:47 +00:00
|
|
|
"CPU usage: {:.2} ms (excludes painting)",
|
|
|
|
1e3 * frame_times.average().unwrap_or_default()
|
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
|
|
|
);
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.add(
|
2020-05-03 11:28:47 +00:00
|
|
|
label!(
|
|
|
|
"FPS: {:.1}",
|
|
|
|
1.0 / frame_times.mean_time_interval().unwrap_or_default()
|
2020-04-29 05:20:27 +00:00
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
|
|
|
);
|
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
let (output, paint_batches) = ctx.end_frame();
|
2020-04-29 05:20:27 +00:00
|
|
|
|
2020-05-03 11:28:47 +00:00
|
|
|
frame_times.add(
|
|
|
|
raw_input.time,
|
|
|
|
(Instant::now() - emigui_start).as_secs_f64() as f32,
|
|
|
|
);
|
2020-04-29 05:20:27 +00:00
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
painter.paint_batches(&display, paint_batches, ctx.texture());
|
2020-04-29 19:58:14 +00:00
|
|
|
emigui_glium::handle_output(output, &display, clipboard.as_mut());
|
2020-04-28 21:05:22 +00:00
|
|
|
}
|
2020-05-02 09:37:12 +00:00
|
|
|
|
2020-05-17 10:26:17 +00:00
|
|
|
// Save state to disk:
|
2020-05-10 19:13:39 +00:00
|
|
|
window_settings.pos = display
|
|
|
|
.gl_window()
|
|
|
|
.get_position()
|
|
|
|
.map(|p| pos2(p.x as f32, p.y as f32));
|
|
|
|
window_settings.size = display
|
|
|
|
.gl_window()
|
|
|
|
.get_inner_size()
|
|
|
|
.map(|size| vec2(size.width as f32, size.height as f32));
|
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
if let Err(err) = emigui_glium::write_memory(&ctx, memory_path) {
|
2020-05-02 09:37:12 +00:00
|
|
|
eprintln!("ERROR: Failed to save emigui state: {}", err);
|
|
|
|
}
|
2020-05-10 19:13:39 +00:00
|
|
|
|
|
|
|
serde_json::to_writer_pretty(
|
|
|
|
std::fs::File::create(settings_json_path).unwrap(),
|
|
|
|
&window_settings,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-04-28 21:05:22 +00:00
|
|
|
}
|
2020-05-17 10:26:17 +00:00
|
|
|
|
|
|
|
fn profile<R>(name: &str, action: impl FnOnce() -> R) -> R {
|
|
|
|
let start = Instant::now();
|
|
|
|
let r = action();
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
eprintln!("{}: {} ms", name, elapsed.as_millis());
|
|
|
|
r
|
|
|
|
}
|