rust/hwrunner/src/main.rs
author alfadur
Mon, 09 May 2022 19:10:43 +0300
changeset 15864 4172debdff61
parent 15820 c4d931ce2659
child 15920 b3295f94e5e9
permissions -rw-r--r--
fix minigun bullet collision detection
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
     1
use std::time::Duration;
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
     2
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
     3
use futures::executor::block_on;
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
     4
use glutin::event_loop::ControlFlow;
14208
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
     5
use glutin::{
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
     6
    dpi,
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
     7
    event::{DeviceEvent, ElementState, Event, MouseButton, MouseScrollDelta, WindowEvent},
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
     8
    event_loop::EventLoop,
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
     9
    window::{Window, WindowBuilder},
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    10
    ContextWrapper, GlProfile, GlRequest, NotCurrent, PossiblyCurrent, WindowedContext,
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    11
};
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    12
use hedgewars_engine::instance::EngineInstance;
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    13
use integral_geometry::Point;
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    14
use std::error::Error;
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    15
use wgpu::{
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    16
    Adapter, BackendBit, Color, CommandEncoderDescriptor, Device, DeviceDescriptor, Features,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    17
    LoadOp, Operations, PowerPreference, PresentMode, Queue, RenderPassColorAttachmentDescriptor,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    18
    RenderPassDescriptor, RequestAdapterOptions, Surface, SwapChain, SwapChainDescriptor,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    19
    TextureFormat, TextureUsage,
14208
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    20
};
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
    21
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    22
type HwGlRendererContext = ContextWrapper<PossiblyCurrent, Window>;
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    23
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    24
struct HwWgpuRenderingContext {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    25
    window: Window,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    26
    surface: Surface,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    27
    adapter: Adapter,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    28
    device: Device,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    29
    queue: Queue,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    30
    swap_chain: SwapChain,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    31
}
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    32
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    33
enum HwRendererContext {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    34
    Gl(HwGlRendererContext),
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    35
    Wgpu(HwWgpuRenderingContext),
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    36
}
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    37
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    38
struct ErrorStub;
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    39
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    40
impl<T: Error> From<T> for ErrorStub {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    41
    fn from(_: T) -> Self {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    42
        ErrorStub
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    43
    }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    44
}
14725
12db7e435ea6 move camera into engine
alfadur
parents: 14723
diff changeset
    45
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
    46
impl HwRendererContext {
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    47
    fn get_framebuffer_size(window: &Window) -> (u32, u32) {
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    48
        window.inner_size().into()
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    49
    }
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    50
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    51
    fn create_wpgu_swap_chain(window: &Window, surface: &Surface, device: &Device) -> SwapChain {
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    52
        let (width, height) = Self::get_framebuffer_size(window);
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    53
        device.create_swap_chain(
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    54
            &surface,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    55
            &SwapChainDescriptor {
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    56
                usage: TextureUsage::OUTPUT_ATTACHMENT,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    57
                format: TextureFormat::Bgra8Unorm,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    58
                width,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    59
                height,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    60
                present_mode: PresentMode::Fifo,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    61
            },
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    62
        )
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    63
    }
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    64
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    65
    fn init_wgpu(
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    66
        event_loop: &EventLoop<()>,
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    67
        size: dpi::LogicalSize<f64>,
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    68
    ) -> HwWgpuRenderingContext {
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    69
        let builder = WindowBuilder::new()
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    70
            .with_title("hwengine")
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    71
            .with_inner_size(size);
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    72
        let window = builder.build(event_loop).unwrap();
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    73
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    74
        let instance = wgpu::Instance::new(BackendBit::PRIMARY);
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    75
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    76
        let surface = unsafe { instance.create_surface(&window) };
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    77
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    78
        let adapter = block_on(instance.request_adapter(&RequestAdapterOptions {
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    79
            power_preference: PowerPreference::HighPerformance,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    80
            compatible_surface: Some(&surface),
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    81
        }))
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    82
        .unwrap();
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    83
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    84
        let (device, queue) = block_on(adapter.request_device(&Default::default(), None)).unwrap();
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    85
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    86
        let swap_chain = Self::create_wpgu_swap_chain(&window, &surface, &device);
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    87
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    88
        HwWgpuRenderingContext {
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    89
            window,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    90
            surface,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    91
            adapter,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    92
            device,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    93
            queue,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    94
            swap_chain,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    95
        }
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    96
    }
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    97
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
    98
    fn init_gl(event_loop: &EventLoop<()>, size: dpi::LogicalSize<f64>) -> HwGlRendererContext {
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
    99
        use glutin::ContextBuilder;
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   100
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   101
        let builder = WindowBuilder::new()
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   102
            .with_title("hwengine")
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   103
            .with_inner_size(size);
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   104
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   105
        let context = ContextBuilder::new()
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   106
            .with_gl(GlRequest::Latest)
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   107
            .with_gl_profile(GlProfile::Core)
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   108
            .build_windowed(builder, &event_loop)
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   109
            .ok()
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   110
            .unwrap();
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   111
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   112
        unsafe {
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   113
            let wrapper = context.make_current().unwrap();
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   114
            gl::load_with(|ptr| wrapper.get_proc_address(ptr) as *const _);
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   115
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   116
            let (width, height) = Self::get_framebuffer_size(wrapper.window());
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   117
            gl::Viewport(0, 0, width as i32, height as i32);
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   118
            wrapper
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   119
        }
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   120
    }
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   121
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   122
    fn new(event_loop: &EventLoop<()>, size: dpi::LogicalSize<f64>, use_wgpu: bool) -> Self {
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   123
        if use_wgpu {
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   124
            Self::Wgpu(Self::init_wgpu(event_loop, size))
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   125
        } else {
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   126
            Self::Gl(Self::init_gl(event_loop, size))
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   127
        }
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   128
    }
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   129
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   130
    pub fn window(&self) -> &Window {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   131
        match self {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   132
            HwRendererContext::Gl(gl) => &gl.window(),
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   133
            HwRendererContext::Wgpu(wgpu) => &wgpu.window,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   134
        }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   135
    }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   136
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   137
    pub fn update(&mut self) {
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   138
        match self {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   139
            HwRendererContext::Gl(context) => unsafe {
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   140
                let (width, height) = Self::get_framebuffer_size(&context.window());
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   141
                gl::Viewport(0, 0, width as i32, height as i32);
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   142
            },
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   143
            HwRendererContext::Wgpu(context) => {
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   144
                context.swap_chain = Self::create_wpgu_swap_chain(
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   145
                    &context.window,
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   146
                    &context.surface,
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   147
                    &context.device,
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   148
                );
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   149
            }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   150
        }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   151
    }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   152
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   153
    pub fn present(&mut self) -> Result<(), ErrorStub> {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   154
        match self {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   155
            HwRendererContext::Gl(context) => context.swap_buffers()?,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   156
            HwRendererContext::Wgpu(context) => {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   157
                let frame_view = &context.swap_chain.get_current_frame()?.output.view;
14209
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14208
diff changeset
   158
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   159
                let mut encoder =
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   160
                    context
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   161
                        .device
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   162
                        .create_command_encoder(&CommandEncoderDescriptor {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   163
                            label: Some("Main encoder"),
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   164
                        });
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   165
                encoder.begin_render_pass(&RenderPassDescriptor {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   166
                    color_attachments: &[RenderPassColorAttachmentDescriptor {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   167
                        attachment: &frame_view,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   168
                        resolve_target: None,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   169
                        ops: Operations {
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   170
                            load: LoadOp::Clear(Color {
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   171
                                r: 0.7,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   172
                                g: 0.4,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   173
                                b: 0.2,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   174
                                a: 1.0,
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   175
                            }),
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   176
                            store: false,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   177
                        },
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   178
                    }],
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   179
                    depth_stencil_attachment: None,
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   180
                });
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   181
                let buffer = encoder.finish();
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   182
                context.queue.submit(std::iter::once(buffer));
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   183
            }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   184
        }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   185
        Ok(())
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   186
    }
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   187
}
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   188
14208
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   189
fn main() {
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   190
    let use_wgpu = false;
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   191
    let mut event_loop = EventLoop::<()>::new();
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   192
    let (w, h) = (1024.0, 768.0);
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   193
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   194
    let mut context = HwRendererContext::new(&event_loop, dpi::LogicalSize::new(w, h), use_wgpu);
14208
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   195
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   196
    let mut engine = EngineInstance::new();
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   197
    if !use_wgpu {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   198
        engine.world.create_renderer(w as u16, h as u16);
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   199
    }
14209
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14208
diff changeset
   200
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   201
    let mut dragging = false;
14209
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14208
diff changeset
   202
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   203
    use std::time::Instant;
14209
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14208
diff changeset
   204
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   205
    let mut now = Instant::now();
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   206
    let mut update_time = Instant::now();
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   207
    let mut render_time = Instant::now();
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   208
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   209
    let current_time = Instant::now();
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   210
    let delta = current_time - now;
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   211
    now = current_time;
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   212
    let ms = delta.as_secs() as f64 * 1000.0 + delta.subsec_millis() as f64;
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   213
    context.window().set_title(&format!("hwengine {:.3}ms", ms));
15788
19ad3fe48ea6 reduce fps
alfadur
parents: 14737
diff changeset
   214
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   215
    event_loop.run(move |event, _, control_flow| {
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   216
        *control_flow = ControlFlow::Poll;
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   217
        match event {
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   218
            Event::WindowEvent { event, .. } => match event {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   219
                WindowEvent::CloseRequested => {
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   220
                    *control_flow = ControlFlow::Exit;
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   221
                }
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   222
                WindowEvent::Resized(_) | WindowEvent::ScaleFactorChanged { .. } => {
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   223
                    context.update()
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   224
                }
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   225
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   226
                WindowEvent::MouseInput { button, state, .. } => {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   227
                    if let MouseButton::Right = button {
14730
65c971417780 fix zoom
alfadur
parents: 14726
diff changeset
   228
                        dragging = state == ElementState::Pressed;
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   229
                    }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   230
                }
14730
65c971417780 fix zoom
alfadur
parents: 14726
diff changeset
   231
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   232
                WindowEvent::MouseWheel { delta, .. } => {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   233
                    let zoom_change = match delta {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   234
                        MouseScrollDelta::LineDelta(x, y) => y as f32 * 0.1f32,
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   235
                        MouseScrollDelta::PixelDelta(delta) => delta.y as f32 * 0.1f32,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   236
                    };
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   237
                    engine.world.move_camera(Point::ZERO, zoom_change);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   238
                }
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   239
                _ => (),
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   240
            },
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   241
            Event::DeviceEvent { event, .. } => match event {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   242
                DeviceEvent::MouseMotion { delta } => {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   243
                    if dragging {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   244
                        engine
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   245
                            .world
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   246
                            .move_camera(Point::new(delta.0 as i32, delta.1 as i32), 0.0)
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   247
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   248
                }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   249
                _ => {}
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   250
            },
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   251
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
   252
            _ => (),
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   253
        }
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   254
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   255
        let current_time = Instant::now();
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   256
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   257
        if update_time.elapsed() > Duration::from_millis(10) {
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   258
            update_time = current_time;
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   259
            engine.world.step()
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   260
        }
14209
3b83ec44e10b pass hwrunner gl context into engine
alfadur
parents: 14208
diff changeset
   261
15798
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   262
        if render_time.elapsed() > Duration::from_millis(16) {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   263
            render_time = current_time;
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   264
            if !use_wgpu {
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   265
                engine.render();
ec85fdf82942 add wgpu support to hwrunner
alfadur
parents: 15788
diff changeset
   266
            }
15799
2b3f8af6f647 fix framebuffer sizing
alfadur
parents: 15798
diff changeset
   267
            context.present().ok().unwrap();
15788
19ad3fe48ea6 reduce fps
alfadur
parents: 14737
diff changeset
   268
        }
15820
c4d931ce2659 update glutin
alfadur
parents: 15799
diff changeset
   269
    });
14208
675aa499a70e add standalone engine runner
alfadur
parents:
diff changeset
   270
}