pel 0.1.0

OpenGL backed framebuffer
Documentation
use std::marker::PhantomData;

use glutin::{dpi::PhysicalSize, PossiblyCurrent, WindowedContext};

use ogl33::{functions::glViewport, types::GLsizei};

use crate::{
    config::PelAttributes,
    gpu::{Program, Texture, TextureHandle},
};

/// Pel Context accessible in [`Events`][1].
///
/// [1]: trait.Events.html
pub struct Context {
    pub(crate) window: WindowedContext<PossiblyCurrent>,
    _program: Program,
    texture: Texture,
    phantom: PhantomData<*const ()>, // !Send + !Sync
}

impl Context {
    /// Returns handle to window pel texture, allowing direct modification.
    ///
    /// Ideally, should only be accessed once in [`Events::draw`][1].
    ///
    /// See documentation of [`TextureHandle`][2] for more information.
    ///
    /// [1]: trait.Events.html#method.draw
    /// [2]: struct.TextureHandle.html
    pub fn texture(&mut self) -> TextureHandle {
        self.texture.texture()
    }

    /// Resizes window pel texture.
    ///
    /// Size is specified in logical pixels.
    pub fn resize_texture(&mut self, w: usize, h: usize) {
        self.texture.resize(w, h);
    }

    pub(super) fn new(window: WindowedContext<PossiblyCurrent>, attribs: &PelAttributes) -> Self {
        let (texture_width, texture_height) = attribs.texture_size.expect("Pel texture size was not set!");

        Self {
            window,
            _program: Program::default(),
            texture: Texture::new(texture_width, texture_height),
            phantom: PhantomData,
        }
    }

    pub(crate) fn resize_viewport(&self, physical_size: PhysicalSize<u32>) {
        let (w, h): (u32, u32) = physical_size.into();
        unsafe {
            glViewport(0, 0, w as GLsizei, h as GLsizei);
        }
    }

    pub(crate) fn request_redraw(&self) {
        self.window.window().request_redraw();
    }

    pub(crate) fn draw(&self) {
        self.texture.draw()
    }

    pub(crate) fn swap_buffers(&self) {
        self.window.swap_buffers().expect("failed to swap window buffers");
    }
}