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