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