rust/hwrunner/src/main.rs
author alfadur
Sun, 25 Nov 2018 05:52:06 +0300
changeset 14298 b8871dd33ec4
parent 14193 3b83ec44e10b
child 14339 7ae81969330f
permissions -rw-r--r--
add gfx setup ffi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     1
use glutin::{
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     2
    dpi::LogicalSize,
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     3
    Event,
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     4
    WindowEvent,
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     5
    EventsLoop,
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     6
    GlWindow,
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
     7
    GlContext
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     8
};
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     9
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    10
use gfx::{
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    11
    texture,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    12
    format,
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    13
    Encoder
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    14
};
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    15
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    16
use gfx_window_glutin::init_existing;
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    17
14298
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14193
diff changeset
    18
use hedgewars_engine::instance::EngineInstance;
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    19
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    20
fn init(event_loop: &EventsLoop, size: LogicalSize) -> GlWindow {
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    21
    use glutin::{
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    22
        ContextBuilder,
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    23
        WindowBuilder
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    24
    };
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    25
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    26
    let window = WindowBuilder::new()
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    27
        .with_title("hwengine")
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    28
        .with_dimensions(size);
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    29
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    30
    let context = ContextBuilder::new();
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    31
    GlWindow::new(window, context, event_loop).unwrap()
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    32
}
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    33
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    34
fn main() {
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    35
    let mut event_loop = EventsLoop::new();
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    36
    let window = init(&event_loop, LogicalSize::new(1024.0, 768.0));
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    37
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    38
    let (mut device, mut factory, color_view, depth_view) =
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    39
        init_existing::<format::Rgba8, format::Depth>(&window);
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    40
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    41
    let mut encoder: Encoder<_, _> = factory.create_command_buffer().into();
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    42
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    43
    let engine = EngineInstance::new();
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    44
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    45
    let mut is_running = true;
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    46
    while is_running {
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    47
        event_loop.poll_events(|event| {
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    48
            match event {
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    49
                Event::WindowEvent { event, ..} => match event {
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    50
                    WindowEvent::CloseRequested => {
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    51
                        is_running = false;
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    52
                    },
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    53
                    _ => ()
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    54
                },
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    55
                _ => ()
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    56
            }
14193
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    57
        });
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    58
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    59
        encoder.clear(&color_view, [0.5, 0.0, 0.0, 1.0]);
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    60
        engine.render(&mut encoder, &color_view);
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    61
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    62
        encoder.flush(&mut device);
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    63
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14192
diff changeset
    64
        window.swap_buffers().unwrap();
14192
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    65
    }
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    66
}