15776
|
1 |
use std::time::Duration;
|
|
2 |
|
|
3 |
use futures::executor::block_on;
|
15798
|
4 |
use glutin::event_loop::ControlFlow;
|
14187
|
5 |
use glutin::{
|
15798
|
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,
|
15776
|
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,
|
14187
|
20 |
};
|
|
21 |
|
15798
|
22 |
type HwGlRendererContext = ContextWrapper<PossiblyCurrent, Window>;
|
15776
|
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 |
}
|
14704
|
45 |
|
15776
|
46 |
impl HwRendererContext {
|
15777
|
47 |
fn get_framebuffer_size(window: &Window) -> (u32, u32) {
|
15798
|
48 |
window.inner_size().into()
|
15777
|
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 |
|
15798
|
65 |
fn init_wgpu(
|
|
66 |
event_loop: &EventLoop<()>,
|
|
67 |
size: dpi::LogicalSize<f64>,
|
|
68 |
) -> HwWgpuRenderingContext {
|
15777
|
69 |
let builder = WindowBuilder::new()
|
|
70 |
.with_title("hwengine")
|
15798
|
71 |
.with_inner_size(size);
|
15777
|
72 |
let window = builder.build(event_loop).unwrap();
|
|
73 |
|
15798
|
74 |
let instance = wgpu::Instance::new(BackendBit::PRIMARY);
|
15777
|
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 |
|
15798
|
98 |
fn init_gl(event_loop: &EventLoop<()>, size: dpi::LogicalSize<f64>) -> HwGlRendererContext {
|
15777
|
99 |
use glutin::ContextBuilder;
|
|
100 |
|
|
101 |
let builder = WindowBuilder::new()
|
|
102 |
.with_title("hwengine")
|
15798
|
103 |
.with_inner_size(size);
|
15777
|
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 {
|
15798
|
113 |
let wrapper = context.make_current().unwrap();
|
|
114 |
gl::load_with(|ptr| wrapper.get_proc_address(ptr) as *const _);
|
15777
|
115 |
|
15798
|
116 |
let (width, height) = Self::get_framebuffer_size(wrapper.window());
|
|
117 |
gl::Viewport(0, 0, width as i32, height as i32);
|
|
118 |
wrapper
|
15777
|
119 |
}
|
|
120 |
}
|
|
121 |
|
15798
|
122 |
fn new(event_loop: &EventLoop<()>, size: dpi::LogicalSize<f64>, use_wgpu: bool) -> Self {
|
15777
|
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 |
|
15776
|
130 |
pub fn window(&self) -> &Window {
|
|
131 |
match self {
|
|
132 |
HwRendererContext::Gl(gl) => &gl.window(),
|
|
133 |
HwRendererContext::Wgpu(wgpu) => &wgpu.window,
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
15777
|
137 |
pub fn update(&mut self) {
|
15776
|
138 |
match self {
|
|
139 |
HwRendererContext::Gl(context) => unsafe {
|
15777
|
140 |
let (width, height) = Self::get_framebuffer_size(&context.window());
|
|
141 |
gl::Viewport(0, 0, width as i32, height as i32);
|
15776
|
142 |
},
|
|
143 |
HwRendererContext::Wgpu(context) => {
|
15777
|
144 |
context.swap_chain = Self::create_wpgu_swap_chain(
|
|
145 |
&context.window,
|
15776
|
146 |
&context.surface,
|
15777
|
147 |
&context.device,
|
15776
|
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;
|
14188
|
158 |
|
15776
|
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 {
|
15777
|
170 |
load: LoadOp::Clear(Color {
|
|
171 |
r: 0.7,
|
|
172 |
g: 0.4,
|
|
173 |
b: 0.2,
|
|
174 |
a: 1.0,
|
|
175 |
}),
|
15776
|
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 |
|
14187
|
189 |
fn main() {
|
15798
|
190 |
let use_wgpu = false;
|
|
191 |
let mut event_loop = EventLoop::<()>::new();
|
14702
|
192 |
let (w, h) = (1024.0, 768.0);
|
15776
|
193 |
|
15777
|
194 |
let mut context = HwRendererContext::new(&event_loop, dpi::LogicalSize::new(w, h), use_wgpu);
|
14187
|
195 |
|
14702
|
196 |
let mut engine = EngineInstance::new();
|
15776
|
197 |
if !use_wgpu {
|
|
198 |
engine.world.create_renderer(w as u16, h as u16);
|
|
199 |
}
|
14188
|
200 |
|
14702
|
201 |
let mut dragging = false;
|
14188
|
202 |
|
14702
|
203 |
use std::time::Instant;
|
14188
|
204 |
|
14702
|
205 |
let mut now = Instant::now();
|
15776
|
206 |
let mut update_time = Instant::now();
|
|
207 |
let mut render_time = Instant::now();
|
14705
|
208 |
|
15798
|
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));
|
15766
|
214 |
|
15798
|
215 |
event_loop.run(move |event, _, control_flow| {
|
|
216 |
*control_flow = ControlFlow::Poll;
|
|
217 |
match event {
|
14705
|
218 |
Event::WindowEvent { event, .. } => match event {
|
|
219 |
WindowEvent::CloseRequested => {
|
15798
|
220 |
*control_flow = ControlFlow::Exit;
|
14705
|
221 |
}
|
15798
|
222 |
WindowEvent::Resized(_) | WindowEvent::ScaleFactorChanged { .. } => {
|
|
223 |
context.update()
|
|
224 |
}
|
15776
|
225 |
|
14705
|
226 |
WindowEvent::MouseInput { button, state, .. } => {
|
|
227 |
if let MouseButton::Right = button {
|
14709
|
228 |
dragging = state == ElementState::Pressed;
|
14702
|
229 |
}
|
14705
|
230 |
}
|
14709
|
231 |
|
14705
|
232 |
WindowEvent::MouseWheel { delta, .. } => {
|
|
233 |
let zoom_change = match delta {
|
|
234 |
MouseScrollDelta::LineDelta(x, y) => y as f32 * 0.1f32,
|
15798
|
235 |
MouseScrollDelta::PixelDelta(delta) => delta.y as f32 * 0.1f32,
|
14705
|
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)
|
14702
|
247 |
}
|
|
248 |
}
|
14705
|
249 |
_ => {}
|
|
250 |
},
|
15798
|
251 |
|
14705
|
252 |
_ => (),
|
15798
|
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 |
}
|
14188
|
261 |
|
15776
|
262 |
if render_time.elapsed() > Duration::from_millis(16) {
|
|
263 |
render_time = current_time;
|
|
264 |
if !use_wgpu {
|
|
265 |
engine.render();
|
|
266 |
}
|
15777
|
267 |
context.present().ok().unwrap();
|
15766
|
268 |
}
|
15798
|
269 |
});
|
14187
|
270 |
}
|