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