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
//!

use std::fs;
use std::io;
use std::path::Path;

use winit::event::{Event,WindowEvent};
use winit::event_loop::ControlFlow;

pub struct Engine {
    device: wgpu::Device,
    queue: wgpu::Queue,
    swap_chain: wgpu::SwapChain,
    render_pipeline: wgpu::RenderPipeline,
}

impl Engine {
    fn draw(&mut self) {

    }
}

pub struct GameConfig<'a> {
    pub title: &'a str,
    pub resolution: (f64, f64),
    pub vertex_shader_path: &'a Path,
    pub fragment_shader_path: &'a Path,
}

pub trait Game: Sized + 'static {
    fn start(self, config: GameConfig) -> ! {
        //
        // Create window and surface
        //

        let event_loop = winit::event_loop::EventLoop::new();

        let window = winit::window::WindowBuilder::new()
            .with_title(config.title)
            .with_inner_size(winit::dpi::LogicalSize::new(config.resolution.0, config.resolution.1))
            .with_resizable(false)
            .with_visible(false)
            .build(&event_loop)
            .expect("Failed to create window.");

        let window_dimensions = window.inner_size();

        let surface = wgpu::Surface::create(&window);

        //
        // Create adapter, device, and queue
        //

        let adapter = pollster::block_on(
            wgpu::Adapter::request(
                &wgpu::RequestAdapterOptions {
                    power_preference: wgpu::PowerPreference::Default,
                    compatible_surface: Some(&surface)
                },
                wgpu::BackendBit::PRIMARY
            )
        ).unwrap();

        let (device, queue) = pollster::block_on(
            adapter.request_device(
                &wgpu::DeviceDescriptor {
                    extensions: wgpu::Extensions { anisotropic_filtering: false },
                    limits: wgpu::Limits::default(),
                }
            )
        );

        //
        // Create swap chain
        //

        let swap_chain = device.create_swap_chain(
            &surface,
            &wgpu::SwapChainDescriptor {
                usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
                format: wgpu::TextureFormat::Bgra8UnormSrgb,
                width: window_dimensions.width,
                height: window_dimensions.height,
                present_mode: wgpu::PresentMode::Mailbox,
            }
        );

        //
        // Create shader modules
        //

        let vertex_shader = device.create_shader_module(
            &wgpu::read_spirv(
                io::BufReader::new(
                    fs::File::open(config.vertex_shader_path).unwrap()
                )
            ).unwrap()
        );

        let fragment_shader = device.create_shader_module(
            &wgpu::read_spirv(
                io::BufReader::new(
                    fs::File::open(config.fragment_shader_path).unwrap()
                )
            ).unwrap()
        );

        //
        // Create render pipeline layout
        //

        let render_pipeline_layout = device.create_pipeline_layout(
            &wgpu::PipelineLayoutDescriptor {
                bind_group_layouts: &[]
            }
        );

        //
        // Create render pipeline
        //

        let render_pipeline = device.create_render_pipeline(
            &wgpu::RenderPipelineDescriptor {
                layout: &render_pipeline_layout,
                vertex_stage: wgpu::ProgrammableStageDescriptor {
                    module: &vertex_shader,
                    entry_point: "main"
                },
                fragment_stage: Some(
                    wgpu::ProgrammableStageDescriptor {
                        module: &fragment_shader,
                        entry_point: "main",
                    }
                ),
                rasterization_state: Some(
                    wgpu::RasterizationStateDescriptor {
                        front_face: wgpu::FrontFace::Ccw,
                        cull_mode: wgpu::CullMode::None,
                        depth_bias: 0,
                        depth_bias_slope_scale: 0.0,
                        depth_bias_clamp: 0.0,
                    }
                ),
                primitive_topology: wgpu::PrimitiveTopology::TriangleList,
                color_states: &[
                    wgpu::ColorStateDescriptor {
                        format: wgpu::TextureFormat::Bgra8UnormSrgb,
                        color_blend: wgpu::BlendDescriptor::REPLACE,
                        alpha_blend: wgpu::BlendDescriptor::REPLACE,
                        write_mask: wgpu::ColorWrite::ALL,
                    }
                ],
                depth_stencil_state: None,
                vertex_state: wgpu::VertexStateDescriptor {
                    index_format: wgpu::IndexFormat::Uint16,
                    vertex_buffers: &[
                        wgpu::VertexBufferDescriptor {
                            stride: 4 * 4,
                            step_mode: wgpu::InputStepMode::Vertex,
                            attributes: &wgpu::vertex_attr_array![0 => Float4],
                        }
                    ]
                },
                sample_count: 1,
                sample_mask: !0,
                alpha_to_coverage_enabled: false
            }
        );

        //
        // Create engine (container struct)
        //

        let mut engine = Engine {
            device,
            queue,
            swap_chain,
            render_pipeline,
        };

        //
        // Start app
        //

        window.set_visible(true);

        event_loop.run(move |event, _, control_flow| {
            match event {
                Event::WindowEvent { event, .. } => {
                    match event {
                        WindowEvent::CloseRequested => {
                            *control_flow = ControlFlow::Exit;
                        },
                        _ => {
                            // open_albion.update(event);
                        }
                    }
                },
                Event::MainEventsCleared => {
                    window.request_redraw()
                },
                Event::RedrawRequested(_window_id) => {
                    engine.draw();
                },
                _event => {}
            }
        })
    }
}