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