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;
|
14193
|
9 |
|
14707
|
10 |
fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
|
14710
|
11 |
use glutin::{ContextBuilder, WindowBuilder};
|
14192
|
12 |
|
|
13 |
let window = WindowBuilder::new()
|
|
14 |
.with_title("hwengine")
|
|
15 |
.with_dimensions(size);
|
|
16 |
|
14707
|
17 |
let cxt = ContextBuilder::new()
|
|
18 |
.with_gl(GlRequest::Latest)
|
|
19 |
.with_gl_profile(GlProfile::Core)
|
14710
|
20 |
.build_windowed(window, &event_loop)
|
|
21 |
.ok()
|
|
22 |
.unwrap();
|
14707
|
23 |
|
|
24 |
unsafe {
|
|
25 |
cxt.make_current().unwrap();
|
|
26 |
gl::load_with(|ptr| cxt.get_proc_address(ptr) as *const _);
|
14710
|
27 |
|
14707
|
28 |
if let Some(sz) = cxt.get_inner_size() {
|
|
29 |
let phys = sz.to_physical(cxt.get_hidpi_factor());
|
14710
|
30 |
|
14707
|
31 |
gl::Viewport(0, 0, phys.width as i32, phys.height as i32);
|
|
32 |
}
|
|
33 |
}
|
|
34 |
|
|
35 |
cxt
|
14192
|
36 |
}
|
|
37 |
|
|
38 |
fn main() {
|
|
39 |
let mut event_loop = EventsLoop::new();
|
14707
|
40 |
let (w, h) = (1024.0, 768.0);
|
|
41 |
let window = init(&event_loop, dpi::LogicalSize::new(w, h));
|
14192
|
42 |
|
14707
|
43 |
let mut engine = EngineInstance::new();
|
14710
|
44 |
engine.world.create_renderer(w as u16, h as u16);
|
14193
|
45 |
|
14707
|
46 |
let mut dragging = false;
|
14193
|
47 |
|
14707
|
48 |
use std::time::Instant;
|
14193
|
49 |
|
14707
|
50 |
let mut now = Instant::now();
|
14710
|
51 |
|
14192
|
52 |
let mut is_running = true;
|
|
53 |
while is_running {
|
14707
|
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));
|
14710
|
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 {
|
14714
|
67 |
dragging = state == ElementState::Pressed;
|
14707
|
68 |
}
|
14710
|
69 |
}
|
14714
|
70 |
|
14710
|
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)
|
14707
|
89 |
}
|
|
90 |
}
|
14710
|
91 |
_ => {}
|
|
92 |
},
|
|
93 |
_ => (),
|
14193
|
94 |
});
|
|
95 |
|
14707
|
96 |
unsafe { window.make_current().unwrap() };
|
14193
|
97 |
|
14709
|
98 |
engine.render();
|
14193
|
99 |
|
|
100 |
window.swap_buffers().unwrap();
|
14192
|
101 |
}
|
|
102 |
}
|