rust/hwrunner/src/main.rs
changeset 14702 29dbe9ce8b7d
parent 14334 7ae81969330f
child 14704 12db7e435ea6
equal deleted inserted replaced
14701:5e2c892b0222 14702:29dbe9ce8b7d
     1 use glutin::{
     1 use glutin::{
     2     dpi::LogicalSize,
     2     dpi,
     3     Event,
     3     Event,
     4     WindowEvent,
     4     WindowEvent,
       
     5     DeviceEvent,
       
     6     ElementState,
       
     7     MouseButton,
       
     8     MouseScrollDelta,
     5     EventsLoop,
     9     EventsLoop,
     6     GlWindow,
    10     WindowedContext,
     7     GlContext
    11     GlRequest,
       
    12     GlProfile,
       
    13     ContextTrait,
     8 };
    14 };
     9 
       
    10 use gfx::{
       
    11     texture,
       
    12     format,
       
    13     Encoder,
       
    14     Device
       
    15 };
       
    16 
       
    17 use gfx_window_glutin::init_existing;
       
    18 
    15 
    19 use hedgewars_engine::instance::EngineInstance;
    16 use hedgewars_engine::instance::EngineInstance;
    20 
    17 
    21 fn init(event_loop: &EventsLoop, size: LogicalSize) -> GlWindow {
    18 fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
    22     use glutin::{
    19     use glutin::{
    23         ContextBuilder,
    20         ContextBuilder,
    24         WindowBuilder
    21         WindowBuilder
    25     };
    22     };
    26 
    23 
    27     let window = WindowBuilder::new()
    24     let window = WindowBuilder::new()
    28         .with_title("hwengine")
    25         .with_title("hwengine")
    29         .with_dimensions(size);
    26         .with_dimensions(size);
    30 
    27 
    31     let context = ContextBuilder::new();
    28     let cxt = ContextBuilder::new()
    32     GlWindow::new(window, context, event_loop).unwrap()
    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
    33 }
    45 }
    34 
    46 
    35 fn main() {
    47 fn main() {
    36     let mut event_loop = EventsLoop::new();
    48     let mut event_loop = EventsLoop::new();
    37     let window = init(&event_loop, LogicalSize::new(1024.0, 768.0));
    49     let (w, h) = (1024.0, 768.0);
       
    50     let window = init(&event_loop, dpi::LogicalSize::new(w, h));
    38 
    51 
    39     let (mut device, mut factory, color_view, depth_view) =
    52     let mut engine = EngineInstance::new();
    40         init_existing::<format::Rgba8, format::Depth>(&window);
       
    41 
    53 
    42     let mut encoder: Encoder<_, _> = factory.create_command_buffer().into();
    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;
    43 
    59 
    44     let engine = EngineInstance::new();
    60     use std::time::Instant;
    45 
    61 
       
    62     let mut now = Instant::now();
       
    63     
    46     let mut is_running = true;
    64     let mut is_running = true;
    47     while is_running {
    65     while is_running {
       
    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         
    48         event_loop.poll_events(|event| {
    72         event_loop.poll_events(|event| {
    49             match event {
    73             match event {
    50                 Event::WindowEvent { event, ..} => match event {
    74                 Event::WindowEvent { event, ..} => match event {
    51                     WindowEvent::CloseRequested => {
    75                     WindowEvent::CloseRequested => {
    52                         is_running = false;
    76                         is_running = false;
    53                     },
    77                     },
       
    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                     }
    54                     _ => ()
    98                     _ => ()
    55                 },
    99                 },
       
   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                 }
    56                 _ => ()
   109                 _ => ()
    57             }
   110             }
    58         });
   111         });
    59 
   112 
    60         encoder.clear(&color_view, [0.5, 0.0, 0.0, 1.0]);
   113         unsafe { window.make_current().unwrap() };
    61         engine.render(&mut encoder, &color_view);
       
    62 
   114 
    63         encoder.flush(&mut device);
   115         // temporary params.. dont actually handle input here
       
   116         engine.render(x, y, w as f32 * zoom, h as f32 * zoom);
    64 
   117 
    65         window.swap_buffers().unwrap();
   118         window.swap_buffers().unwrap();
    66         device.cleanup();
       
    67     }
   119     }
    68 }
   120 }