|
1 use glutin::{ |
|
2 dpi, ContextTrait, DeviceEvent, ElementState, Event, EventsLoop, GlProfile, GlRequest, |
|
3 MouseButton, MouseScrollDelta, WindowEvent, WindowedContext, |
|
4 }; |
|
5 |
|
6 use hedgewars_engine::instance::EngineInstance; |
|
7 |
|
8 use integral_geometry::Point; |
|
9 use std::time::Duration; |
|
10 |
|
11 fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext { |
|
12 use glutin::{ContextBuilder, WindowBuilder}; |
|
13 |
|
14 let window = WindowBuilder::new() |
|
15 .with_title("hwengine") |
|
16 .with_dimensions(size); |
|
17 |
|
18 let cxt = ContextBuilder::new() |
|
19 .with_gl(GlRequest::Latest) |
|
20 .with_gl_profile(GlProfile::Core) |
|
21 .build_windowed(window, &event_loop) |
|
22 .ok() |
|
23 .unwrap(); |
|
24 |
|
25 unsafe { |
|
26 cxt.make_current().unwrap(); |
|
27 gl::load_with(|ptr| cxt.get_proc_address(ptr) as *const _); |
|
28 |
|
29 if let Some(sz) = cxt.get_inner_size() { |
|
30 let phys = sz.to_physical(cxt.get_hidpi_factor()); |
|
31 |
|
32 gl::Viewport(0, 0, phys.width as i32, phys.height as i32); |
|
33 } |
|
34 } |
|
35 |
|
36 cxt |
|
37 } |
|
38 |
|
39 fn main() { |
|
40 let mut event_loop = EventsLoop::new(); |
|
41 let (w, h) = (1024.0, 768.0); |
|
42 let window = init(&event_loop, dpi::LogicalSize::new(w, h)); |
|
43 |
|
44 let mut engine = EngineInstance::new(); |
|
45 engine.world.create_renderer(w as u16, h as u16); |
|
46 |
|
47 let mut dragging = false; |
|
48 |
|
49 use std::time::Instant; |
|
50 |
|
51 let mut now = Instant::now(); |
|
52 let mut update = Instant::now(); |
|
53 |
|
54 let mut is_running = true; |
|
55 while is_running { |
|
56 let curr = Instant::now(); |
|
57 let delta = curr - now; |
|
58 now = curr; |
|
59 let ms = delta.as_secs() as f64 * 1000.0 + delta.subsec_millis() as f64; |
|
60 window.set_title(&format!("hwengine {:.3}ms", ms)); |
|
61 |
|
62 if update.elapsed() > Duration::from_millis(10) { |
|
63 update = curr; |
|
64 engine.world.step() |
|
65 } |
|
66 |
|
67 event_loop.poll_events(|event| match event { |
|
68 Event::WindowEvent { event, .. } => match event { |
|
69 WindowEvent::CloseRequested => { |
|
70 is_running = false; |
|
71 } |
|
72 WindowEvent::MouseInput { button, state, .. } => { |
|
73 if let MouseButton::Right = button { |
|
74 dragging = state == ElementState::Pressed; |
|
75 } |
|
76 } |
|
77 |
|
78 WindowEvent::MouseWheel { delta, .. } => { |
|
79 let zoom_change = match delta { |
|
80 MouseScrollDelta::LineDelta(x, y) => y as f32 * 0.1f32, |
|
81 MouseScrollDelta::PixelDelta(delta) => { |
|
82 let physical = delta.to_physical(window.get_hidpi_factor()); |
|
83 physical.y as f32 * 0.1f32 |
|
84 } |
|
85 }; |
|
86 engine.world.move_camera(Point::ZERO, zoom_change); |
|
87 } |
|
88 _ => (), |
|
89 }, |
|
90 Event::DeviceEvent { event, .. } => match event { |
|
91 DeviceEvent::MouseMotion { delta } => { |
|
92 if dragging { |
|
93 engine |
|
94 .world |
|
95 .move_camera(Point::new(delta.0 as i32, delta.1 as i32), 0.0) |
|
96 } |
|
97 } |
|
98 _ => {} |
|
99 }, |
|
100 _ => (), |
|
101 }); |
|
102 |
|
103 unsafe { window.make_current().unwrap() }; |
|
104 |
|
105 engine.render(); |
|
106 window.swap_buffers().unwrap(); |
|
107 } |
|
108 } |