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