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
mod background;
mod formats;
mod gfx_context;
mod input;

use chargrid_app as app;
pub use chargrid_graphical_common::*;
use chargrid_render::Size;

pub struct Context {
    events_loop: glutin::EventsLoop,
    gfx_context: gfx_context::Context<'static>,
}

pub type ContextBuildError = gfx_context::Error;

pub struct WindowHandle {}

impl WindowHandle {
    pub fn fullscreen(&self) -> bool {
        false
    }
    pub fn set_fullscreen(&self, fullscreen: bool) {
        if fullscreen {
            log::warn!("fullscreen not implemented");
        }
    }
}

impl Context {
    pub fn new_returning_window_handle(
        context_descriptor: ContextDescriptor,
    ) -> Result<(Self, WindowHandle), ContextBuildError> {
        Self::new(context_descriptor).map(|s| (s, WindowHandle {}))
    }
    pub fn new(
        ContextDescriptor {
            font_bytes,
            title,
            window_dimensions,
            cell_dimensions,
            font_dimensions,
            font_source_dimensions: _,
            underline_width,
            underline_top_offset,
        }: ContextDescriptor,
    ) -> Result<Self, ContextBuildError> {
        let normal_font: &'static [u8] = Box::leak(font_bytes.normal.into_boxed_slice());
        let bold_font: &'static [u8] = Box::leak(font_bytes.bold.into_boxed_slice());
        let builder = gfx_context::ContextBuilder::new_with_font(normal_font)
            .with_bold_font(bold_font)
            .with_title(title)
            .with_underline_width((underline_width * cell_dimensions.height) as u32)
            .with_underline_position((underline_top_offset * cell_dimensions.height) as u32)
            .with_cell_dimensions(Size::new(cell_dimensions.width as u32, cell_dimensions.height as u32))
            .with_font_scale(font_dimensions.width as f32, font_dimensions.height as f32);
        let builder = {
            let size = Size::new(window_dimensions.width as u32, window_dimensions.height as u32);
            builder
                .with_window_dimensions(size)
                .with_min_window_dimensions(size)
                .with_max_window_dimensions(size)
        };
        let (gfx_context, events_loop) = builder.build()?;
        Ok(Self {
            gfx_context,
            events_loop,
        })
    }
    pub fn run_app<A>(self, app: A)
    where
        A: app::App + 'static,
    {
        self.gfx_context.run_app(app, self.events_loop)
    }
}