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