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