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 |
|
14298
|
16 |
use hedgewars_engine::instance::EngineInstance;
|
14193
|
17 |
|
14707
|
18 |
fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
|
14192
|
19 |
use glutin::{
|
|
20 |
ContextBuilder,
|
|
21 |
WindowBuilder
|
|
22 |
};
|
|
23 |
|
|
24 |
let window = WindowBuilder::new()
|
|
25 |
.with_title("hwengine")
|
|
26 |
.with_dimensions(size);
|
|
27 |
|
14707
|
28 |
let cxt = ContextBuilder::new()
|
|
29 |
.with_gl(GlRequest::Latest)
|
|
30 |
.with_gl_profile(GlProfile::Core)
|
|
31 |
.build_windowed(window, &event_loop).ok().unwrap();
|
|
32 |
|
|
33 |
unsafe {
|
|
34 |
cxt.make_current().unwrap();
|
|
35 |
gl::load_with(|ptr| cxt.get_proc_address(ptr) as *const _);
|
|
36 |
|
|
37 |
if let Some(sz) = cxt.get_inner_size() {
|
|
38 |
let phys = sz.to_physical(cxt.get_hidpi_factor());
|
|
39 |
|
|
40 |
gl::Viewport(0, 0, phys.width as i32, phys.height as i32);
|
|
41 |
}
|
|
42 |
}
|
|
43 |
|
|
44 |
cxt
|
14192
|
45 |
}
|
|
46 |
|
|
47 |
fn main() {
|
|
48 |
let mut event_loop = EventsLoop::new();
|
14707
|
49 |
let (w, h) = (1024.0, 768.0);
|
|
50 |
let window = init(&event_loop, dpi::LogicalSize::new(w, h));
|
14192
|
51 |
|
14707
|
52 |
let mut engine = EngineInstance::new();
|
14193
|
53 |
|
14707
|
54 |
// dirty dirty code follows; DO NOT USE
|
|
55 |
let mut zoom = 1f32;
|
|
56 |
let mut dragging = false;
|
|
57 |
let mut x = 0f32;
|
|
58 |
let mut y = 0f32;
|
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, .. } => {
|
|
88 |
match delta {
|
|
89 |
MouseScrollDelta::LineDelta(x, y) => {
|
|
90 |
zoom += y as f32 * 0.1f32;
|
|
91 |
}
|
|
92 |
MouseScrollDelta::PixelDelta(delta) => {
|
|
93 |
let physical = delta.to_physical(window.get_hidpi_factor());
|
|
94 |
zoom += physical.y as f32 * 0.1f32;
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
14192
|
98 |
_ => ()
|
|
99 |
},
|
14707
|
100 |
Event::DeviceEvent { event, .. } => match event {
|
|
101 |
DeviceEvent::MouseMotion { delta } => {
|
|
102 |
if dragging {
|
|
103 |
x -= delta.0 as f32;
|
|
104 |
y -= delta.1 as f32;
|
|
105 |
}
|
|
106 |
}
|
|
107 |
_ => {}
|
|
108 |
}
|
14192
|
109 |
_ => ()
|
|
110 |
}
|
14193
|
111 |
});
|
|
112 |
|
14707
|
113 |
unsafe { window.make_current().unwrap() };
|
14193
|
114 |
|
14707
|
115 |
// temporary params.. dont actually handle input here
|
|
116 |
engine.render(x, y, w as f32 * zoom, h as f32 * zoom);
|
14193
|
117 |
|
|
118 |
window.swap_buffers().unwrap();
|
14192
|
119 |
}
|
|
120 |
}
|