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
use crate::backend_glow::{build_program, GfxCtxInnards, PrerenderInnards};
use crate::{ScreenDims, Settings};

pub fn setup(settings: &Settings) -> (PrerenderInnards, winit::event_loop::EventLoop<()>) {
    let event_loop = winit::event_loop::EventLoop::new();
    let window = winit::window::WindowBuilder::new()
        .with_title(&settings.window_title)
        .with_maximized(true);
    // TODO If people are hitting problems with context not matching what their GPU provides, dig up
    // backend_glium.rs from git and bring the fallback behavior here. (Ideally, there'd be
    // something in glutin to directly express this.)
    let context = glutin::ContextBuilder::new()
        // 2 looks really bad, 4 looks alright. For thin lines at low zoom, 8 looks better, and
        // should have common videocard support.
        .with_multisampling(8)
        .with_depth_buffer(2)
        .build_windowed(window.clone(), &event_loop)
        .or_else(|err| {
            warn!("Trying default graphics context after standard graphics context failed with error: {:?}",  err);
            glutin::ContextBuilder::new().build_windowed(window.clone(), &event_loop)
        })
        .or_else(|err| {
            warn!("Trying graphics context with vsync after default graphics context failed with error: {:?}", err);
            glutin::ContextBuilder::new()
                .with_vsync(true)
                .build_windowed(window.clone(), &event_loop)
        }).unwrap_or_else(|err| {
        panic!("Your videocard doesn't support the OpenGL mode requested. This is a common issue when running inside a virtual machine; please run natively if possible. See https://github.com/a-b-street/abstreet/issues/103 for more info, and feel free to ask for help using that issue.\n\nError: {:?}", err);
    });

    let windowed_context = unsafe { context.make_current().unwrap() };
    let gl = unsafe {
        glow::Context::from_loader_function(|s| windowed_context.get_proc_address(s) as *const _)
    };

    let program = unsafe {
        build_program(
            &gl,
            include_str!("../shaders/vertex_140.glsl"),
            include_str!("../shaders/fragment_140.glsl"),
        )
        .or_else(|err| {
            warn!(
                "unable to build program with default shaderrs, falling back to v300. error: {:?}",
                err
            );
            build_program(
                &gl,
                include_str!("../shaders/vertex_300.glsl"),
                include_str!("../shaders/fragment_300.glsl"),
            )
        })
        .unwrap_or_else(|err| {
            panic!("error building program: {:?}", err);
        })
    };

    (
        PrerenderInnards::new(gl, true, program, Some(WindowAdapter(windowed_context))),
        event_loop,
    )
}

pub struct WindowAdapter(glutin::WindowedContext<glutin::PossiblyCurrent>);

impl WindowAdapter {
    pub fn window(&self) -> &winit::window::Window {
        self.0.window()
    }

    pub fn window_resized(&self, new_size: ScreenDims, scale_factor: f64) {
        let physical_size = winit::dpi::LogicalSize::from(new_size).to_physical(scale_factor);
        self.0.resize(physical_size);
    }

    pub fn draw_finished(&self, _gfc_ctx_innards: GfxCtxInnards) {
        self.0.swap_buffers().unwrap();
    }
}