aleatico 0.1.0

stub package for fennel engine graphics
Documentation
use log::debug;
use std::sync::Arc;
use winit::window::Window;

use crate::errors::AleaticoResult;
use crate::renderer::ctx::GPUContext;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::EventLoopExtWebSys;

/// State structure
pub struct State {
    window: Arc<Window>,
    gpu_context: GPUContext,
}

impl State {
    /// Create a new instance of [`State`]
    pub async fn new(window: Arc<Window>) -> AleaticoResult<Self> {
        let gpu_context = GPUContext::new(window.clone()).await?;
        Ok(Self {
            window,
            gpu_context,
        })
    }

    /// Handle app resize
    pub fn resize(&mut self, width: u32, height: u32) {
        debug!("can we pretend to leave and then we'll meet again when both our cars collide?");
        if width > 0 && height > 0 {
            self.gpu_context.surface.config.width = width;
            self.gpu_context.surface.config.height = height;
            self.gpu_context.surface.inner.configure(
                &self.gpu_context.surface.device,
                &self.gpu_context.surface.config,
            );
            self.gpu_context.surface.is_configured = true;
        }
    }

    /// Update the state
    pub fn update(&mut self) {}

    /// Render the state
    pub fn render(&mut self) -> AleaticoResult<()> {
        self.window.request_redraw();
        self.gpu_context.render()
    }
}