rust/hwrunner/src/main.rs
author fkaa
Fri, 22 Mar 2019 18:01:08 +0200
changeset 14707 29dbe9ce8b7d
parent 14339 7ae81969330f
child 14709 12db7e435ea6
permissions -rw-r--r--
add basic map rendering with gl
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
14298
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14193
diff changeset
    16
use hedgewars_engine::instance::EngineInstance;
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    17
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    18
fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    19
    use glutin::{
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    20
        ContextBuilder,
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    21
        WindowBuilder
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    22
    };
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    23
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    24
    let window = WindowBuilder::new()
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    25
        .with_title("hwengine")
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    26
        .with_dimensions(size);
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    27
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    28
    let cxt = ContextBuilder::new()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    29
        .with_gl(GlRequest::Latest)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    30
        .with_gl_profile(GlProfile::Core)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    31
        .build_windowed(window, &event_loop).ok().unwrap();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    32
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    33
    unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    34
        cxt.make_current().unwrap();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    35
        gl::load_with(|ptr| cxt.get_proc_address(ptr) as *const _);
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
        if let Some(sz) = cxt.get_inner_size() {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    38
            let phys = sz.to_physical(cxt.get_hidpi_factor());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    39
            
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    40
            gl::Viewport(0, 0, phys.width as i32, phys.height as i32);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    41
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    42
    }
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
    cxt
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    45
}
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    46
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    47
fn main() {
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    48
    let mut event_loop = EventsLoop::new();
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    49
    let (w, h) = (1024.0, 768.0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    50
    let window = init(&event_loop, dpi::LogicalSize::new(w, h));
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    51
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    52
    let mut engine = EngineInstance::new();
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    53
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    54
    // dirty dirty code follows; DO NOT USE
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    55
    let mut zoom = 1f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    56
    let mut dragging = false;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    57
    let mut x = 0f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    58
    let mut y = 0f32;
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, .. } => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    88
                        match delta {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    89
                            MouseScrollDelta::LineDelta(x, y) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    90
                                zoom += y as f32 * 0.1f32;
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());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    94
                                zoom += physical.y as f32 * 0.1f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
    95
                            }
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
                    }
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    98
                    _ => ()
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    99
                },
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   100
                Event::DeviceEvent { event, .. } => match event {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   101
                    DeviceEvent::MouseMotion { delta } => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   102
                        if dragging {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   103
                            x -= delta.0 as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   104
                            y -= delta.1 as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   105
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   106
                    }
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
                }
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   109
                _ => ()
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   110
            }
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
   111
        });
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
   112
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   113
        unsafe { window.make_current().unwrap() };
14193
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
        // temporary params.. dont actually handle input here
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14339
diff changeset
   116
        engine.render(x, y, w as f32 * zoom, h as f32 * zoom);
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
   117
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
   118
        window.swap_buffers().unwrap();
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   119
    }
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   120
}