aboutsummaryrefslogtreecommitdiff
path: root/stockton-skeleton/src/texture/image.rs
blob: 6ccd22aaf8d9cc001f26ebd2e26f6c007064e9e8 (plain)
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
use super::PIXEL_SIZE;

use core::ptr::copy_nonoverlapping;
use std::convert::TryInto;

use image::RgbaImage;

/// An object that can be loaded as an image into GPU memory
pub trait LoadableImage {
    fn width(&self) -> u32;
    fn height(&self) -> u32;

    /// # Safety
    /// Ensure the ptr is at least width() * PIXEL_SIZE bytes.
    unsafe fn copy_row(&self, y: u32, ptr: *mut u8);

    /// # Safety
    /// Ensure the ptr is at least row_size * height() * PIXEL_SIZE bytes.
    unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize) {
        for y in 0..self.height() as usize {
            let dest_base: isize = (y * row_size).try_into().unwrap();
            self.copy_row(y as u32, ptr.offset(dest_base));
        }
    }
}

impl LoadableImage for RgbaImage {
    fn width(&self) -> u32 {
        self.width()
    }

    fn height(&self) -> u32 {
        self.height()
    }

    unsafe fn copy_row(&self, y: u32, ptr: *mut u8) {
        let row_size_bytes = self.width() as usize * PIXEL_SIZE;
        let raw: &Vec<u8> = self.as_raw();
        let row = &raw[y as usize * row_size_bytes..(y as usize + 1) * row_size_bytes];

        copy_nonoverlapping(row.as_ptr(), ptr, row.len());
    }
}

/// An object that can be used to resolve a texture from a BSP File
pub trait TextureResolver {
    type Image: LoadableImage;

    /// Get the given texture, or None if it's corrupt/not there.
    fn resolve(&mut self, texture_id: u32) -> Option<Self::Image>;
}