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
use std::sync::Arc;

use vulkano::{
    VulkanLibrary,
    instance::{Instance, InstanceCreateInfo},
    swapchain::Surface
};

use winit::event_loop::EventLoop;

use crate::error::{
    Error,
    ErrorType
};

#[derive(Debug)]
pub struct Renderer {
    winit_event_loop: EventLoop<()>,
    vk_instance: Arc<Instance>,
}

impl Renderer {
    pub fn new() -> Result<Renderer, Error> {
        let winit_event_loop = match EventLoop::new() {
            Ok(eloop) => eloop,
            Err(err) => return Err(Error::new(
                ErrorType::WinitEventLoopCreationFailed, 
                "Winit event loop creation failed".to_owned(),
                err.to_string()
            ))
        };

        let library = match VulkanLibrary::new() {
            Ok(lib) => lib,
            Err(err) => return Err(Error::new(
                ErrorType::VulkanMissing, 
                "System could not find vulkan. Make sure your system supports vulkan and your drivers are up-to-date".to_owned(),
                err.to_string()
            ))
        };

        let vk_instance = match Instance::new(library, InstanceCreateInfo {
            enabled_extensions: Surface::required_extensions(&winit_event_loop),
            ..Default::default()
        }) {
            Ok(instance) => instance,
            Err(err) => return Err(Error::new(
                ErrorType::FailedToCreateVulkanInstance, 
                "Vulkan instance creation failed".to_owned(),
                err.to_string()
            ))
        };

        Ok(Renderer {
            winit_event_loop,
            vk_instance
        })
    }
}