sevenx_engine 0.2.11

Engine de jogos 2D/3D completa com suporte Android, física, áudio, partículas, tilemap, UI, eventos e sistema 3D avançado com PBR.
Documentation
use image::ImageError;

/// Representa uma folha de sprites (sprite sheet).
pub struct SpriteSheet {
    pub width: u32,       // Largura total da imagem
    pub height: u32,      // Altura total da imagem
    pub rgba_data: Vec<u8>,
    pub frame_width: u32, // Largura de uma única frame
    pub frame_height: u32,// Altura de uma única frame
}

impl SpriteSheet {
    /// Carrega uma folha de sprites.
    pub fn load(path: &str, frame_width: u32, frame_height: u32) -> Result<Self, ImageError> {
        let img = image::open(path)?;
        let rgba_image = img.to_rgba8();

        Ok(Self {
            width: img.width(),
            height: img.height(),
            rgba_data: rgba_image.into_raw(),
            frame_width,
            frame_height,
        })
    }
    
    /// Cria um sprite quadriculado temporário de 2 frames (64x32).
    pub fn create_placeholder() -> Self {
        const FRAME_WIDTH: u32 = 32;
        const FRAME_HEIGHT: u32 = 32;
        const NUM_FRAMES: u32 = 2;
        let width = FRAME_WIDTH * NUM_FRAMES;
        let height = FRAME_HEIGHT;
        let mut rgba_data = vec![0; (width * height * 4) as usize];

        // Frame 1: Magenta/Preto
        for y in 0..FRAME_HEIGHT {
            for x in 0..FRAME_WIDTH {
                let index = ((y * width) + x) as usize * 4;
                if (x + y) % 2 == 0 {
                    rgba_data[index..index + 4].copy_from_slice(&[255, 0, 255, 255]);
                } else {
                    rgba_data[index..index + 4].copy_from_slice(&[0, 0, 0, 255]);
                }
            }
        }
        
        // Frame 2: Ciano/Branco
        for y in 0..FRAME_HEIGHT {
            for x in 0..FRAME_WIDTH {
                let index = ((y * width) + (x + FRAME_WIDTH)) as usize * 4;
                if (x + y) % 2 == 0 {
                    rgba_data[index..index + 4].copy_from_slice(&[0, 255, 255, 255]);
                } else {
                    rgba_data[index..index + 4].copy_from_slice(&[255, 255, 255, 255]);
                }
            }
        }
        Self { width, height, rgba_data, frame_width: FRAME_WIDTH, frame_height: FRAME_HEIGHT }
    }
    
    /// Calcula o número de frames na folha de sprites.
    pub fn num_frames(&self) -> usize {
        (self.width / self.frame_width) as usize
    }
}