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