14192
|
1 |
use glutin::{
|
14707
|
2 |
dpi,
|
14192
|
3 |
Event,
|
|
4 |
WindowEvent,
|
14707
|
5 |
DeviceEvent,
|
|
6 |
ElementState,
|
|
7 |
MouseButton,
|
|
8 |
MouseScrollDelta,
|
14192
|
9 |
EventsLoop,
|
14707
|
10 |
WindowedContext,
|
|
11 |
GlRequest,
|
|
12 |
GlProfile,
|
|
13 |
ContextTrait,
|
14192
|
14 |
};
|
|
15 |
|
14709
|
16 |
use hedgewars_engine::{
|
|
17 |
instance::EngineInstance,
|
|
18 |
};
|
|
19 |
|
|
20 |
use integral_geometry::Point;
|
14193
|
21 |
|
14707
|
22 |
fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
|
14192
|
23 |
use glutin::{
|
|
24 |
ContextBuilder,
|
|
25 |
WindowBuilder
|
|
26 |
};
|
|
27 |
|
|
28 |
let window = WindowBuilder::new()
|
|
29 |
.with_title("hwengine")
|
|
30 |
.with_dimensions(size);
|
|
31 |
|
14707
|
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
|
14192
|
49 |
}
|
|
50 |
|
|
51 |
fn main() {
|
|
52 |
let mut event_loop = EventsLoop::new();
|
14707
|
53 |
let (w, h) = (1024.0, 768.0);
|
|
54 |
let window = init(&event_loop, dpi::LogicalSize::new(w, h));
|
14192
|
55 |
|
14707
|
56 |
let mut engine = EngineInstance::new();
|
14193
|
57 |
|
14707
|
58 |
let mut dragging = false;
|
14193
|
59 |
|
14707
|
60 |
use std::time::Instant;
|
14193
|
61 |
|
14707
|
62 |
let mut now = Instant::now();
|
|
63 |
|
14192
|
64 |
let mut is_running = true;
|
|
65 |
while is_running {
|
14707
|
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 |
|
14192
|
72 |
event_loop.poll_events(|event| {
|
|
73 |
match event {
|
|
74 |
Event::WindowEvent { event, ..} => match event {
|
|
75 |
WindowEvent::CloseRequested => {
|
|
76 |
is_running = false;
|
|
77 |
},
|
14707
|
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, .. } => {
|
14709
|
88 |
let zoom_change = match delta {
|
14707
|
89 |
MouseScrollDelta::LineDelta(x, y) => {
|
14709
|
90 |
y as f32 * 0.1f32
|
14707
|
91 |
}
|
|
92 |
MouseScrollDelta::PixelDelta(delta) => {
|
|
93 |
let physical = delta.to_physical(window.get_hidpi_factor());
|
14709
|
94 |
physical.y as f32 * 0.1f32
|
14707
|
95 |
}
|
14709
|
96 |
};
|
|
97 |
engine.world.move_camera(Point::ZERO, zoom_change);
|
14707
|
98 |
}
|
14192
|
99 |
_ => ()
|
|
100 |
},
|
14707
|
101 |
Event::DeviceEvent { event, .. } => match event {
|
|
102 |
DeviceEvent::MouseMotion { delta } => {
|
|
103 |
if dragging {
|
14709
|
104 |
engine.world.move_camera(
|
|
105 |
Point::new(delta.0 as i32, delta.1 as i32), 0.0
|
|
106 |
)
|
14707
|
107 |
}
|
|
108 |
}
|
|
109 |
_ => {}
|
|
110 |
}
|
14192
|
111 |
_ => ()
|
|
112 |
}
|
14193
|
113 |
});
|
|
114 |
|
14707
|
115 |
unsafe { window.make_current().unwrap() };
|
14193
|
116 |
|
14709
|
117 |
engine.render();
|
14193
|
118 |
|
|
119 |
window.swap_buffers().unwrap();
|
14192
|
120 |
}
|
|
121 |
}
|