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