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