rust/hwrunner/src/main.rs
changeset 14702 29dbe9ce8b7d
parent 14334 7ae81969330f
child 14704 12db7e435ea6
--- a/rust/hwrunner/src/main.rs	Thu Mar 21 01:23:05 2019 +0300
+++ b/rust/hwrunner/src/main.rs	Fri Mar 22 18:01:08 2019 +0200
@@ -1,24 +1,21 @@
 use glutin::{
-    dpi::LogicalSize,
+    dpi,
     Event,
     WindowEvent,
+    DeviceEvent,
+    ElementState,
+    MouseButton,
+    MouseScrollDelta,
     EventsLoop,
-    GlWindow,
-    GlContext
+    WindowedContext,
+    GlRequest,
+    GlProfile,
+    ContextTrait,
 };
 
-use gfx::{
-    texture,
-    format,
-    Encoder,
-    Device
-};
-
-use gfx_window_glutin::init_existing;
-
 use hedgewars_engine::instance::EngineInstance;
 
-fn init(event_loop: &EventsLoop, size: LogicalSize) -> GlWindow {
+fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
     use glutin::{
         ContextBuilder,
         WindowBuilder
@@ -28,41 +25,96 @@
         .with_title("hwengine")
         .with_dimensions(size);
 
-    let context = ContextBuilder::new();
-    GlWindow::new(window, context, event_loop).unwrap()
+    let cxt = ContextBuilder::new()
+        .with_gl(GlRequest::Latest)
+        .with_gl_profile(GlProfile::Core)
+        .build_windowed(window, &event_loop).ok().unwrap();
+
+    unsafe {
+        cxt.make_current().unwrap();
+        gl::load_with(|ptr| cxt.get_proc_address(ptr) as *const _);
+        
+        if let Some(sz) = cxt.get_inner_size() {
+            let phys = sz.to_physical(cxt.get_hidpi_factor());
+            
+            gl::Viewport(0, 0, phys.width as i32, phys.height as i32);
+        }
+    }
+
+    cxt
 }
 
 fn main() {
     let mut event_loop = EventsLoop::new();
-    let window = init(&event_loop, LogicalSize::new(1024.0, 768.0));
+    let (w, h) = (1024.0, 768.0);
+    let window = init(&event_loop, dpi::LogicalSize::new(w, h));
 
-    let (mut device, mut factory, color_view, depth_view) =
-        init_existing::<format::Rgba8, format::Depth>(&window);
+    let mut engine = EngineInstance::new();
 
-    let mut encoder: Encoder<_, _> = factory.create_command_buffer().into();
+    // dirty dirty code follows; DO NOT USE
+    let mut zoom = 1f32;
+    let mut dragging = false;
+    let mut x = 0f32;
+    let mut y = 0f32;
 
-    let engine = EngineInstance::new();
+    use std::time::Instant;
 
+    let mut now = Instant::now();
+    
     let mut is_running = true;
     while is_running {
+        let curr = Instant::now();
+        let delta = curr - now;
+        now = curr;
+        let ms = delta.as_secs() as f64 * 1000.0 + delta.subsec_millis() as f64;
+        window.set_title(&format!("hwengine {:.3}ms", ms));
+        
         event_loop.poll_events(|event| {
             match event {
                 Event::WindowEvent { event, ..} => match event {
                     WindowEvent::CloseRequested => {
                         is_running = false;
                     },
+                    WindowEvent::MouseInput { button, state, .. } => {
+                        if let MouseButton::Right = button {
+                            if let ElementState::Pressed = state {
+                                dragging = true;
+                            } else {
+                                dragging = false;
+                            }
+                        }
+                    }
+                    WindowEvent::MouseWheel { delta, .. } => {
+                        match delta {
+                            MouseScrollDelta::LineDelta(x, y) => {
+                                zoom += y as f32 * 0.1f32;
+                            }
+                            MouseScrollDelta::PixelDelta(delta) => {
+                                let physical = delta.to_physical(window.get_hidpi_factor());
+                                zoom += physical.y as f32 * 0.1f32;
+                            }
+                        }
+                    }
                     _ => ()
                 },
+                Event::DeviceEvent { event, .. } => match event {
+                    DeviceEvent::MouseMotion { delta } => {
+                        if dragging {
+                            x -= delta.0 as f32;
+                            y -= delta.1 as f32;
+                        }
+                    }
+                    _ => {}
+                }
                 _ => ()
             }
         });
 
-        encoder.clear(&color_view, [0.5, 0.0, 0.0, 1.0]);
-        engine.render(&mut encoder, &color_view);
+        unsafe { window.make_current().unwrap() };
 
-        encoder.flush(&mut device);
+        // temporary params.. dont actually handle input here
+        engine.render(x, y, w as f32 * zoom, h as f32 * zoom);
 
         window.swap_buffers().unwrap();
-        device.cleanup();
     }
 }