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 {
|
14709
|
67 |
dragging = state == ElementState::Pressed;
|
14702
|
68 |
}
|
14705
|
69 |
}
|
14709
|
70 |
|
14705
|
71 |
WindowEvent::MouseWheel { delta, .. } => {
|
|
72 |
let zoom_change = match delta {
|
|
73 |
MouseScrollDelta::LineDelta(x, y) => y as f32 * 0.1f32,
|
|
74 |
MouseScrollDelta::PixelDelta(delta) => {
|
|
75 |
let physical = delta.to_physical(window.get_hidpi_factor());
|
|
76 |
physical.y as f32 * 0.1f32
|
|
77 |
}
|
|
78 |
};
|
|
79 |
engine.world.move_camera(Point::ZERO, zoom_change);
|
|
80 |
}
|
|
81 |
_ => (),
|
|
82 |
},
|
|
83 |
Event::DeviceEvent { event, .. } => match event {
|
|
84 |
DeviceEvent::MouseMotion { delta } => {
|
|
85 |
if dragging {
|
|
86 |
engine
|
|
87 |
.world
|
|
88 |
.move_camera(Point::new(delta.0 as i32, delta.1 as i32), 0.0)
|
14702
|
89 |
}
|
|
90 |
}
|
14705
|
91 |
_ => {}
|
|
92 |
},
|
|
93 |
_ => (),
|
14188
|
94 |
});
|
|
95 |
|
14702
|
96 |
unsafe { window.make_current().unwrap() };
|
14188
|
97 |
|
14704
|
98 |
engine.render();
|
14188
|
99 |
window.swap_buffers().unwrap();
|
14187
|
100 |
}
|
|
101 |
}
|