1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#![feature(arbitrary_self_types)]
#![feature(integer_atomics)]

extern crate winit;
#[macro_use]
pub extern crate vulkano;
extern crate vulkano_win;
#[macro_use]
extern crate vulkano_shaders;
extern crate rand;
extern crate parking_lot;
extern crate crossbeam;
extern crate num_cpus;
extern crate image;
extern crate decorum;
extern crate freetype_sys;

pub mod interface;
pub mod atlas;
pub mod misc;
pub mod shaders;
#[allow(warnings)]
pub mod bindings;
pub mod input;
pub mod window;

use atlas::Atlas;
use interface::interface::Interface;
use vulkano::sync::GpuFuture;
use vulkano::instance::{Instance,PhysicalDevice};
use vulkano::device::{self,Device,DeviceExtensions};
use vulkano::swapchain::{self,Swapchain};
use vulkano::command_buffer::AutoCommandBufferBuilder;
use std::sync::Arc;
use std::time::Instant;
use parking_lot::{Mutex,RwLock};
use std::sync::atomic::{self,AtomicBool,AtomicUsize};
use std::collections::VecDeque;
use std::thread;
use std::sync::Barrier;
use vulkano::swapchain::Surface;
use std::thread::JoinHandle;
use std::time::Duration;
use input::Input;
use window::BasaltWindow;

const SHOW_SWAPCHAIN_WARNINGS: bool = false;

#[derive(Debug)]
pub struct Limits {
	pub max_image_dimension_2d: u32,
	pub max_image_dimension_3d: u32,
}

struct Initials {
	device: Arc<Device>,
	graphics_queue: Arc<device::Queue>,
	transfer_queue: Arc<device::Queue>,
	surface: Arc<Surface<Arc<dyn BasaltWindow + Send + Sync>>>,
	swap_caps: swapchain::Capabilities,
	limits: Arc<Limits>,
	pdevi: usize,
	window_size: [u32; 2],
}

impl Initials {
	pub fn use_first_device(options: Options) -> Result<Self, String> {
		let mut device_num = 0;
		let mut show_devices = false;
		
		for arg in ::std::env::args() {
			if arg.starts_with("--use-device=") {
				let split_by_eq: Vec<_> = arg.split("=").collect();
				
				if split_by_eq.len() < 2 {
					println!("Incorrect '--use-device' usage. Example: '--use-device=2'");
					break;
				} else {
					device_num = match split_by_eq[1].parse() {
						Ok(ok) => ok,
						Err(_) => {
							println!("Incorrect '--use-device' usage. Example: '--use-device=2'");
							continue;
						}
					};
					
					println!("Using device: {}", device_num);
				}
			} else if arg.starts_with("--show-devices") {
				show_devices = true;
			}
		}
	
		let device_ext = DeviceExtensions { khr_swapchain: true, .. DeviceExtensions::none() };
		let extensions = vulkano_win::required_extensions();
		
		let instance = match Instance::new(None, &extensions, None) {
			Ok(ok) => ok,
			Err(e) => return Err(format!("Failed to create instance: {}", e))
		};
				
		let surface = match window::open_surface(options.clone(), instance.clone()) {
			Ok(ok) => ok,
			Err(e) => return Err(e)
		};
				
		let mut physical_devs: Vec<_> = PhysicalDevice::enumerate(&instance).collect();
		
		if show_devices {
			println!("Devices:");
			for (i, dev) in physical_devs.iter().enumerate() {
				println!("  {}: {}", i, dev.name());
			}
		}
		
		match physical_devs.get(device_num) {
			Some(_) => (),
			None => if device_num == 0 {
				return Err(format!("No physical devices available."))
			} else {
				return Err(format!("Phyiscal device not found."))
			}
		};
		
		let physical = physical_devs.remove(device_num);
		let mut queue_family_opts = Vec::new();
	
		for family in physical.queue_families() {
			for _ in 0..family.queues_count() {
				queue_family_opts.push(family);
			}
		}
		
		let mut graphics_queue_ = None;
		let mut transfer_queue_ = None;
		
		for i in 0..queue_family_opts.len() {
			if
				queue_family_opts[i].supports_graphics() &&
				surface.is_supported(queue_family_opts[i]).unwrap_or(false)
			{	
				graphics_queue_ = Some((queue_family_opts[i], 0.8));
				queue_family_opts.remove(i);
				break;
			}
		} if graphics_queue_.is_none() {
			return Err(format!("Couldn't find a suitable queue for graphics."));
		}
		
		for i in 0..queue_family_opts.len() {
			transfer_queue_ = Some((queue_family_opts[i], 0.2));
			queue_family_opts.remove(i);
			break;
		} if transfer_queue_.is_none() {
			println!("Couldn't find a suitable queue for transfers.\
				\nUsing graphics queue for transfers also.");
		}
		
		let mut req_queues = Vec::new();
		req_queues.push(graphics_queue_.unwrap());
		
		if let Some(transfer_queue) = transfer_queue_ {
			req_queues.push(transfer_queue);
		}
		
		let (device, mut queues) = match Device::new(
			physical, physical.supported_features(), 
			&device_ext, req_queues)
		{
			Ok(ok) => ok,
			Err(e) => return Err(format!("Failed to create device: {}", e))
		}; let graphics_queue = match queues.next() {
			Some(some) => some,
			None => return Err(format!("Device didn't have any queues"))
		}; let transfer_queue = match queues.next() {
			Some(some) => some,
			None => graphics_queue.clone()
		}; let swap_caps = match surface.capabilities(physical) {
			Ok(ok) => ok,
			Err(e) => return Err(format!("Failed to get surface capabilities: {}", e))
		};
		
		let phy_limits = physical.limits();
		
		let limits = Arc::new(Limits {
			max_image_dimension_2d: phy_limits.max_image_dimension_2d(),
			max_image_dimension_3d: phy_limits.max_image_dimension_3d(),
		});
				
		Ok(Initials {
			device,
			graphics_queue,
			transfer_queue,
			surface,
			swap_caps,
			limits,
			pdevi: device_num,
			window_size: options.window_size,
		})
	}
}

#[derive(Debug,Clone)]
pub enum InputSource {
	Native,
	Winit,
}

#[derive(Debug,Clone)]
pub struct Options {
	ignore_dpi: bool,
	window_size: [u32; 2],
	title: String,
	scale: f32,
	input_src: InputSource,
}

impl Default for Options {
	fn default() -> Self {
		Options {
			ignore_dpi: false,
			window_size: [1920, 1080],
			title: "vk-basalt".to_string(),
			scale: 1.0,
			input_src: InputSource::Winit,
		}
	}
}

impl Options {
	pub fn ignore_dpi(mut self, to: bool) -> Self {
		self.ignore_dpi = to;
		self
	}
	
	pub fn window_size(mut self, width: u32, height: u32) -> Self {
		self.window_size = [width, height];
		self
	}
	
	pub fn title<T: AsRef<str>>(mut self, title: T) -> Self {
		self.title = String::from(title.as_ref());
		self
	}
	
	pub fn scale(mut self, to: f32) -> Self {
		self.scale = to;
		self
	}
	
	pub fn native_input(mut self) -> Self {
		self.input_src = InputSource::Native;
		self
	}
}

pub enum ResizeTo {
	Dims(u32, u32),
	FullScreen(bool),
}

#[allow(dead_code)]
pub struct Basalt {
	device: Arc<Device>,
	graphics_queue: Arc<device::Queue>,
	transfer_queue: Arc<device::Queue>,
	surface: Arc<Surface<Arc<dyn BasaltWindow + Send + Sync>>>,
	swap_caps: swapchain::Capabilities,
	do_every: RwLock<Vec<Arc<dyn Fn() + Send + Sync>>>,
	mouse_capture: AtomicBool,
	allow_mouse_cap: AtomicBool,
	fps: AtomicUsize,
	interface: Arc<Interface>,
	atlas: Arc<Atlas>,
	input: Arc<Input>,
	wants_exit: AtomicBool,
	force_resize: AtomicBool,
	#[allow(dead_code)]
	limits: Arc<Limits>,
	resize_requested: AtomicBool,
	resize_to: Mutex<Option<ResizeTo>>,
	loop_thread: Mutex<Option<JoinHandle<Result<(), String>>>>,
	pdevi: usize,
	vsync: Mutex<bool>,
	wait_on_futures: Mutex<Vec<(Box<dyn GpuFuture + Send + Sync>, Arc<Barrier>)>>,
	window_size: Mutex<[u32; 2]>,
	custom_scale: Mutex<f32>,
	options: Options,
	ignore_dpi_data: Mutex<Option<(usize, Instant, u32, u32)>>,
}

#[allow(dead_code)]
impl Basalt {
	pub fn new(options: Options) -> Result<Arc<Self>, String> {
		unsafe {
			let initials = match Initials::use_first_device(options.clone()) {
				Ok(ok) => ok,
				Err(e) => return Err(e)
			};
			
			let mut basalt_ret = Arc::new(Basalt {
				device: initials.device,
				graphics_queue: initials.graphics_queue,
				transfer_queue: initials.transfer_queue,
				surface: initials.surface,
				swap_caps: initials.swap_caps,
				do_every: RwLock::new(Vec::new()),
				mouse_capture: AtomicBool::new(false),
				allow_mouse_cap: AtomicBool::new(true),
				fps: AtomicUsize::new(0),
				interface: ::std::mem::uninitialized(),
				limits: initials.limits.clone(),
				atlas: ::std::mem::uninitialized(),
				input: ::std::mem::uninitialized(),
				wants_exit: AtomicBool::new(false),
				force_resize: AtomicBool::new(false),
				resize_requested: AtomicBool::new(false),
				resize_to: Mutex::new(None),
				loop_thread: Mutex::new(None),
				pdevi: initials.pdevi,
				vsync: Mutex::new(true),
				wait_on_futures: Mutex::new(Vec::new()),
				window_size: Mutex::new(initials.window_size),
				custom_scale: Mutex::new(options.scale),
				options,
				ignore_dpi_data: Mutex::new(None),
			});
			
			let atlas_ptr = &mut Arc::get_mut(&mut basalt_ret).unwrap().atlas as *mut _;
			let interface_ptr = &mut Arc::get_mut(&mut basalt_ret).unwrap().interface as *mut _;
			let input_ptr = &mut Arc::get_mut(&mut basalt_ret).unwrap().input as *mut _;
			::std::ptr::write(atlas_ptr, Atlas::new(basalt_ret.clone()));
			::std::ptr::write(interface_ptr, Interface::new(basalt_ret.clone()));
			::std::ptr::write(input_ptr, Input::new(basalt_ret.clone()));
			basalt_ret.surface.window().attach_basalt(basalt_ret.clone());
			
			basalt_ret.input_ref().add_hook(input::InputHook::Press {
				global: false,
				keys: vec![input::Qwery::F1],
				mouse_buttons: Vec::new()
			}, Arc::new(move |_| {
				println!("\
			    -------------------------------------\r\n\
	             F1: Prints keys used by basalt\r\n\
	             F2: Prints fps while held\r\n\
	             F7: Decreases msaa level\r\n\
	             F8: Increases msaa level\r\n\
	             F10: Toggles vsync\r\n\
	             LCtrl + Dash: Decreases ui scale\r\n\
	             LCtrl + Equal: Increaes ui scale\r\n\
			    -------------------------------------");
				input::InputHookRes::Success
			}));
			
			let basalt = basalt_ret.clone();
			basalt_ret.input_ref().add_hook(input::InputHook::Hold {
				global: false,
				keys: vec![input::Qwery::F2],
				mouse_buttons: Vec::new(),
				initial_delay: Duration::from_millis(0),
				interval: Duration::from_millis(100),
				accel: 0.0,
			}, Arc::new(move |_| {
				println!("FPS: {}", basalt.fps());
				input::InputHookRes::Success
			}));
			
			let basalt = basalt_ret.clone();
			basalt_ret.input_ref().add_hook(input::InputHook::Press {
				global: false,
				keys: vec![input::Qwery::F7],
				mouse_buttons: Vec::new()
			}, Arc::new(move |_| {
				basalt.interface_ref().decrease_msaa();
				println!("MSAA set to {}X", basalt.interface_ref().msaa());
				input::InputHookRes::Success
			}));
			
			let basalt = basalt_ret.clone();
			basalt_ret.input_ref().add_hook(input::InputHook::Press {
				global: false,
				keys: vec![input::Qwery::F8],
				mouse_buttons: Vec::new()
			}, Arc::new(move |_| {
				basalt.interface_ref().increase_msaa();
				println!("MSAA set to {}X", basalt.interface_ref().msaa());
				input::InputHookRes::Success
			}));
			
			let basalt = basalt_ret.clone();
			basalt_ret.input_ref().add_hook(input::InputHook::Press {
				global: false,
				keys: vec![input::Qwery::F10],
				mouse_buttons: Vec::new()
			}, Arc::new(move |_| {
				let mut vsync = basalt.vsync.lock();
				*vsync = !*vsync;
				basalt.force_resize.store(true, atomic::Ordering::Relaxed);
				
				if *vsync {
					println!("VSync Enabled!");
				} else {
					println!("VSync Disabled!");
				}
				
				input::InputHookRes::Success
			}));
			
			let basalt = basalt_ret.clone();
			basalt_ret.input_ref().add_hook(input::InputHook::Press {
				global: false,
				keys: vec![input::Qwery::LCtrl, input::Qwery::Dash],
				mouse_buttons: Vec::new()
			}, Arc::new(move |_| {
				basalt.add_scale(-0.05);
				println!("Current Scale: {:.1} %", basalt.current_scale() * 100.0);
				input::InputHookRes::Success
			}));
			
			let basalt = basalt_ret.clone();
			basalt_ret.input_ref().add_hook(input::InputHook::Press {
				global: false,
				keys: vec![input::Qwery::LCtrl, input::Qwery::Equal],
				mouse_buttons: Vec::new()
			}, Arc::new(move |_| {
				basalt.add_scale(0.05);
				println!("Current Scale: {:.1} %", basalt.current_scale() * 100.0);		
				input::InputHookRes::Success
			}));
			
			Ok(basalt_ret)
		}
	}
	
	pub(crate) fn force_resize(&self) {
		self.force_resize.store(true, atomic::Ordering::Relaxed);
	}
	
	pub fn input_ref(&self) -> &Arc<Input> {
		&self.input
	}
	
	pub fn limits(&self) -> Arc<Limits> {
		self.limits.clone()
	}
	
	pub fn current_scale(&self) -> f32 {
		*self.custom_scale.lock()
	}
	
	pub fn set_scale(&self, to: f32) {
		let mut custom_scale = self.custom_scale.lock();
		*custom_scale = to;
		self.interface_ref().set_scale(*custom_scale);
	}
	
	pub fn add_scale(&self, amt: f32) {
		let mut custom_scale = self.custom_scale.lock();
		*custom_scale += amt;
		self.interface_ref().set_scale(*custom_scale);
	}
	
	
	/// This will only work if the basalt is handling the loop thread. This
	/// is done via the method ``spawn_app_loop()``
	pub fn wait_for_exit(&self) -> Result<(), String> {
		match self.loop_thread.lock().take() {
			Some(handle) => match handle.join() {
				Ok(ok) => ok,
				Err(_) => Err(format!("Failed to join loop thread."))
			}, None => Ok(())
		}
	}
	
	pub fn spawn_app_loop(self: &Arc<Self>) {
		let basalt = self.clone();
		
		*self.loop_thread.lock() = Some(thread::spawn(move || {
			basalt.app_loop()
		}));
	}
	
	/// only works with app loop
	pub fn resize(&self, w: u32, h: u32) {
		*self.resize_to.lock() = Some(ResizeTo::Dims(w, h));
		self.resize_requested.store(true, atomic::Ordering::Relaxed);
	}
	
	/// only works with app loop
	pub fn fullscreen(&self, fullscreen: bool) {
		*self.resize_to.lock() = Some(ResizeTo::FullScreen(fullscreen));
		self.resize_requested.store(true, atomic::Ordering::Relaxed);
	}
	
	/// only works with app loop
	pub fn exit(&self) {
		self.wants_exit.store(true, atomic::Ordering::Relaxed);
	}
	
	/// only works with app loop
	pub fn do_every(&self, func: Arc<dyn Fn() + Send + Sync>) {
		self.do_every.write().push(func);
	}
	
	/// only works with app loop
	pub fn fps(&self) -> usize {
		self.fps.load(atomic::Ordering::Relaxed)
	}
	
	pub fn interface(&self) -> Arc<Interface> {
		self.interface.clone()
	} pub fn interface_ref(&self) -> &Arc<Interface> {
		&self.interface
	} pub fn atlas(&self) -> Arc<Atlas> {
		self.atlas.clone()
	} pub fn mouse_captured(&self) -> bool {
		self.mouse_capture.load(atomic::Ordering::Relaxed)
	} pub fn allow_mouse_cap(&self, to: bool) {
		self.allow_mouse_cap.store(to, atomic::Ordering::Relaxed);
	} pub fn mouse_cap_allowed(&self) -> bool {
		self.allow_mouse_cap.load(atomic::Ordering::Relaxed)
	} pub fn atlas_ref(&self) -> &Arc<Atlas> {
		&self.atlas
	} pub fn device(&self) -> Arc<Device> {
		self.device.clone()
	} pub fn device_ref(&self) -> &Arc<Device> {
		&self.device
	} pub fn transfer_queue(&self) -> Arc<device::Queue> {
		self.transfer_queue.clone()
	} pub fn transfer_queue_ref(&self) -> &Arc<device::Queue> {
		&self.transfer_queue
	} pub fn graphics_queue(&self) -> Arc<device::Queue> {
		self.graphics_queue.clone()
	} pub fn graphics_queue_ref(&self) -> &Arc<device::Queue> {
		&self.graphics_queue
	} pub fn physical_device_index(&self) -> usize {
		self.pdevi
	} pub fn surface(&self) -> Arc<Surface<Arc<dyn BasaltWindow + Send + Sync>>> {
		self.surface.clone()
	} pub fn surface_ref(&self) -> &Arc<Surface<Arc<dyn BasaltWindow + Send + Sync>>> {
		&self.surface
	} pub fn swap_caps(&self) -> &swapchain::Capabilities {
		&self.swap_caps
	} pub fn wants_exit(&self) -> bool {
		self.wants_exit.load(atomic::Ordering::Relaxed)
	}
	
	pub fn mouse_capture(&self, mut to: bool) {
		if !self.mouse_cap_allowed() {
			to = false;
		} self.mouse_capture.store(to, atomic::Ordering::Relaxed);
	}
	
	pub fn app_loop(self: &Arc<Self>) -> Result<(), String> {
		let mut win_size_x;
		let mut win_size_y;
		let mut frames = 0_usize;
		let mut last_out = Instant::now();
		let mut window_grab_cursor = false;
		let mut swapchain_ = None;
		let mut resized = false;
		
		let preferred_swap_formats = vec![
			vulkano::format::Format::R8G8B8A8Srgb,
			vulkano::format::Format::B8G8R8A8Srgb,
		];
		
		let mut swapchain_format_ = None;
		
		for a in &preferred_swap_formats {
			for &(ref b, _) in &self.swap_caps.supported_formats {
				if a == b {
					swapchain_format_ = Some(*a);
					break;
				}
			} if swapchain_format_.is_some() {
				break;
			}
		}
		
		let swapchain_format = match swapchain_format_ {
			Some(some) => some,
			None => return Err(format!("Failed to find capatible format for swapchain. Avaible formats: {:?}", self.swap_caps.supported_formats))
		};
		
		let mut itf_renderer = interface::render::ItfRenderer::new(self.clone());
		
		'resize: loop {
			let [x, y] = self.surface.capabilities(PhysicalDevice::from_index(
				self.surface.instance(), self.pdevi).unwrap()).unwrap().current_extent.unwrap();
			win_size_x = x;
			win_size_y = y;
			*self.window_size.lock() = [x, y];
			
			let present_mode = if *self.vsync.lock() {
				if self.swap_caps.present_modes.relaxed {
					swapchain::PresentMode::Relaxed
				} else {
					swapchain::PresentMode::Fifo
				}
			} else {
				if self.swap_caps.present_modes.mailbox {
					swapchain::PresentMode::Mailbox
				} else if self.swap_caps.present_modes.immediate {
					swapchain::PresentMode::Immediate
				} else {
					swapchain::PresentMode::Fifo
				}
			};
			
			let old_swapchain = swapchain_.as_ref().map(|v: &(Arc<Swapchain<_>>, _)| v.0.clone());
					
			swapchain_ = Some(match Swapchain::new(
				self.device.clone(), self.surface.clone(),
				self.swap_caps.min_image_count, swapchain_format,
				[x, y], 1, self.swap_caps.supported_usage_flags,
				&self.graphics_queue, swapchain::SurfaceTransform::Identity,
				swapchain::CompositeAlpha::Opaque, present_mode,
				true, old_swapchain.as_ref()
			) {
				Ok(ok) => ok,
				Err(e) => {
					if SHOW_SWAPCHAIN_WARNINGS { println!("swapchain recreation error: {:?}", e); }
					continue;
				}
			});
			
			let (swapchain, images) = (&swapchain_.as_ref().unwrap().0, &swapchain_.as_ref().unwrap().1);
			let mut fps_avg = VecDeque::new();
			
			loop {
				if self.resize_requested.load(atomic::Ordering::Relaxed) {
					self.resize_requested.store(true, atomic::Ordering::Relaxed);
					
					if let Some(resize_to) = self.resize_to.lock().take() {
						match resize_to {
							ResizeTo::FullScreen(f) => match f {
								true => {
									self.surface.window().enable_fullscreen();
								}, false => {
									self.surface.window().disable_fullscreen();
								}
							}, ResizeTo::Dims(w, h) => {
								self.surface.window().request_resize(w, h);
							}
						}
						
						resized = true;
						continue 'resize;
					}
				}
				
				let duration = last_out.elapsed();
				let millis = (duration.as_secs()*1000) as f32 + (duration.subsec_nanos() as f32/1000000.0);
		
				if millis >= 50.0 {
					let fps = frames as f32 / (millis/1000.0);
					fps_avg.push_back(fps);
					
					if fps_avg.len() > 20 {
						fps_avg.pop_front();
					}
					
					let mut sum = 0.0;
					
					for num in &fps_avg {
						sum += *num;
					}
					
					let avg_fps = f32::floor(sum / fps_avg.len() as f32) as usize;
					self.fps.store(avg_fps, atomic::Ordering::Relaxed);
					frames = 0;
					last_out = Instant::now();
				}
		
				frames += 1;
			
				for func in &*self.do_every.read() {
					func()
				}
				
				if self.force_resize.swap(false, atomic::Ordering::Relaxed) {
					resized = true;
					continue 'resize;
				}
		
				let (image_num, acquire_future) = match swapchain::acquire_next_image(swapchain.clone(), Some(::std::time::Duration::new(1, 0))) {
					Ok(ok) => ok,
					Err(e) => {
						if SHOW_SWAPCHAIN_WARNINGS { println!("swapchain::acquire_next_image() Err: {:?}", e); }
						resized = true;
						continue 'resize;
					}
				};
				
				let cmd_buf = AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone(), self.graphics_queue.family()).unwrap();
				let (cmd_buf, _) = itf_renderer.draw(cmd_buf, [win_size_x, win_size_y], resized, images, true, image_num);
				let cmd_buf = cmd_buf.build().unwrap();	
				
				let mut future = match acquire_future.then_execute(self.graphics_queue.clone(), cmd_buf).expect("1")
					.then_swapchain_present(self.graphics_queue.clone(), swapchain.clone(), image_num)
					.then_signal_fence_and_flush()
				{
					Ok(ok) => ok,
					Err(e) => match e {
						vulkano::sync::FlushError::OutOfDate => {
							resized = true;
							continue 'resize;
						}, _ => panic!("then_signal_fence_and_flush() {:?}", e)
					}
				};
				
				future.wait(None).unwrap();
				future.cleanup_finished();
				
				let grab_cursor = self.mouse_capture.load(atomic::Ordering::Relaxed);
			
				if grab_cursor != window_grab_cursor {
					match grab_cursor {
						true => self.surface.window().capture_cursor(),
						false => self.surface.window().release_cursor()
					} window_grab_cursor = grab_cursor;
				}
				
				resized = false;
				if self.wants_exit.load(atomic::Ordering::Relaxed) { break 'resize }
			}
		}
		
		Ok(())
	}
}