rust/hwrunner/src/main.rs
author Wuzzy <Wuzzy2@mail.ru>
Sun, 05 May 2019 15:24:19 +0200
changeset 14884 1f0e8a144bad
parent 14716 8e74d4eb89f5
child 15766 19ad3fe48ea6
permissions -rw-r--r--
Re-style QHeaderView colors
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;
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14714
diff changeset
     9
use std::time::Duration;
14188
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14187
diff changeset
    10
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    11
fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    12
    use glutin::{ContextBuilder, WindowBuilder};
14187
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
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    18
    let cxt = ContextBuilder::new()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    19
        .with_gl(GlRequest::Latest)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    20
        .with_gl_profile(GlProfile::Core)
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    21
        .build_windowed(window, &event_loop)
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    22
        .ok()
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    23
        .unwrap();
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    24
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    25
    unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    26
        cxt.make_current().unwrap();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    27
        gl::load_with(|ptr| cxt.get_proc_address(ptr) as *const _);
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    28
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    29
        if let Some(sz) = cxt.get_inner_size() {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    30
            let phys = sz.to_physical(cxt.get_hidpi_factor());
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    31
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    32
            gl::Viewport(0, 0, phys.width as i32, phys.height as i32);
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
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    36
    cxt
14187
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();
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    41
    let (w, h) = (1024.0, 768.0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    42
    let window = init(&event_loop, dpi::LogicalSize::new(w, h));
14187
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    43
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    44
    let mut engine = EngineInstance::new();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    45
    engine.world.create_renderer(w as u16, h as u16);
14188
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14187
diff changeset
    46
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    47
    let mut dragging = false;
14188
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14187
diff changeset
    48
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    49
    use std::time::Instant;
14188
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14187
diff changeset
    50
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    51
    let mut now = Instant::now();
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14714
diff changeset
    52
    let mut update = Instant::now();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    53
14187
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 {
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    56
        let curr = Instant::now();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    57
        let delta = curr - now;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    58
        now = curr;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
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: 14334
diff changeset
    60
        window.set_title(&format!("hwengine {:.3}ms", ms));
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    61
14716
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14714
diff changeset
    62
        if update.elapsed() > Duration::from_millis(10) {
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14714
diff changeset
    63
            update = curr;
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14714
diff changeset
    64
            engine.world.step()
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14714
diff changeset
    65
        }
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14714
diff changeset
    66
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    67
        event_loop.poll_events(|event| match event {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    68
            Event::WindowEvent { event, .. } => match event {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    69
                WindowEvent::CloseRequested => {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    70
                    is_running = false;
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    71
                }
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    72
                WindowEvent::MouseInput { button, state, .. } => {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    73
                    if let MouseButton::Right = button {
14709
65c971417780 fix zoom
alfadur
parents: 14705
diff changeset
    74
                        dragging = state == ElementState::Pressed;
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    75
                    }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    76
                }
14709
65c971417780 fix zoom
alfadur
parents: 14705
diff changeset
    77
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    78
                WindowEvent::MouseWheel { delta, .. } => {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    79
                    let zoom_change = match delta {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    80
                        MouseScrollDelta::LineDelta(x, y) => y as f32 * 0.1f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    81
                        MouseScrollDelta::PixelDelta(delta) => {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    82
                            let physical = delta.to_physical(window.get_hidpi_factor());
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    83
                            physical.y as f32 * 0.1f32
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
                    engine.world.move_camera(Point::ZERO, zoom_change);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    87
                }
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    88
                _ => (),
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    89
            },
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    90
            Event::DeviceEvent { event, .. } => match event {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    91
                DeviceEvent::MouseMotion { delta } => {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    92
                    if dragging {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    93
                        engine
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    94
                            .world
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    95
                            .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
    96
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
    97
                }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    98
                _ => {}
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
    99
            },
19122a329774 make world renderer optional + fmt
alfadur
parents: 14704
diff changeset
   100
            _ => (),
14188
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14187
diff changeset
   101
        });
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14187
diff changeset
   102
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14334
diff changeset
   103
        unsafe { window.make_current().unwrap() };
14188
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14187
diff changeset
   104
14704
12db7e435ea6 move camera into engine
alfadur
parents: 14702
diff changeset
   105
        engine.render();
14188
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14187
diff changeset
   106
        window.swap_buffers().unwrap();
14187
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   107
    }
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   108
}