14208
|
1 |
use glutin::{
|
14726
|
2 |
dpi, ContextTrait, DeviceEvent, ElementState, Event, EventsLoop, GlProfile, GlRequest,
|
|
3 |
MouseButton, MouseScrollDelta, WindowEvent, WindowedContext,
|
14208
|
4 |
};
|
|
5 |
|
14726
|
6 |
use hedgewars_engine::instance::EngineInstance;
|
14725
|
7 |
|
|
8 |
use integral_geometry::Point;
|
14737
|
9 |
use std::time::Duration;
|
14209
|
10 |
|
14723
|
11 |
fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
|
14726
|
12 |
use glutin::{ContextBuilder, WindowBuilder};
|
14208
|
13 |
|
|
14 |
let window = WindowBuilder::new()
|
|
15 |
.with_title("hwengine")
|
|
16 |
.with_dimensions(size);
|
|
17 |
|
14723
|
18 |
let cxt = ContextBuilder::new()
|
|
19 |
.with_gl(GlRequest::Latest)
|
|
20 |
.with_gl_profile(GlProfile::Core)
|
14726
|
21 |
.build_windowed(window, &event_loop)
|
|
22 |
.ok()
|
|
23 |
.unwrap();
|
14723
|
24 |
|
|
25 |
unsafe {
|
|
26 |
cxt.make_current().unwrap();
|
|
27 |
gl::load_with(|ptr| cxt.get_proc_address(ptr) as *const _);
|
14726
|
28 |
|
14723
|
29 |
if let Some(sz) = cxt.get_inner_size() {
|
|
30 |
let phys = sz.to_physical(cxt.get_hidpi_factor());
|
14726
|
31 |
|
14723
|
32 |
gl::Viewport(0, 0, phys.width as i32, phys.height as i32);
|
|
33 |
}
|
|
34 |
}
|
|
35 |
|
|
36 |
cxt
|
14208
|
37 |
}
|
|
38 |
|
|
39 |
fn main() {
|
|
40 |
let mut event_loop = EventsLoop::new();
|
14723
|
41 |
let (w, h) = (1024.0, 768.0);
|
|
42 |
let window = init(&event_loop, dpi::LogicalSize::new(w, h));
|
14208
|
43 |
|
14723
|
44 |
let mut engine = EngineInstance::new();
|
14726
|
45 |
engine.world.create_renderer(w as u16, h as u16);
|
14209
|
46 |
|
14723
|
47 |
let mut dragging = false;
|
14209
|
48 |
|
14723
|
49 |
use std::time::Instant;
|
14209
|
50 |
|
14723
|
51 |
let mut now = Instant::now();
|
14737
|
52 |
let mut update = Instant::now();
|
14726
|
53 |
|
14208
|
54 |
let mut is_running = true;
|
|
55 |
while is_running {
|
14723
|
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));
|
14726
|
61 |
|
14737
|
62 |
if update.elapsed() > Duration::from_millis(10) {
|
|
63 |
update = curr;
|
|
64 |
engine.world.step()
|
|
65 |
}
|
|
66 |
|
14726
|
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 {
|
14730
|
74 |
dragging = state == ElementState::Pressed;
|
14723
|
75 |
}
|
14726
|
76 |
}
|
14730
|
77 |
|
14726
|
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)
|
14723
|
96 |
}
|
|
97 |
}
|
14726
|
98 |
_ => {}
|
|
99 |
},
|
|
100 |
_ => (),
|
14209
|
101 |
});
|
|
102 |
|
14723
|
103 |
unsafe { window.make_current().unwrap() };
|
14209
|
104 |
|
14725
|
105 |
engine.render();
|
14209
|
106 |
window.swap_buffers().unwrap();
|
14208
|
107 |
}
|
|
108 |
}
|