aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/q3/light_vols.rs
blob: 1741569c3ee788de2b2fbd1be01e0f856d980ed3 (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
52
53
54
55
56
/*
 * Copyright (C) Oscar Shrimpton 2020
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use std::convert::TryInto;

use super::Q3BSPFile;
use crate::coords::CoordSystem;
use crate::traits::light_vols::*;
use crate::types::{ParseError, Result, RGB};

const VOL_LENGTH: usize = (3 * 2) + 2;

pub fn from_data(data: &[u8]) -> Result<Box<[LightVol]>> {
    if data.len() % VOL_LENGTH != 0 {
        return Err(ParseError::Invalid);
    }
    let length = data.len() / VOL_LENGTH;

    let mut vols = Vec::with_capacity(length);
    for n in 0..length {
        let data = &data[n * VOL_LENGTH..(n + 1) * VOL_LENGTH];
        vols.push(LightVol {
            ambient: RGB::from_slice(&data[0..3]),
            directional: RGB::from_slice(&data[3..6]),
            dir: data[6..8].try_into().unwrap(),
        });
    }

    Ok(vols.into_boxed_slice())
}

impl<T: CoordSystem> HasLightVols for Q3BSPFile<T> {
    type LightVolsIter<'a> = std::slice::Iter<'a, LightVol>;

    fn lightvols_iter(&self) -> Self::LightVolsIter<'_> {
        self.light_vols.iter()
    }

    fn get_lightvol(&self, index: u32) -> &LightVol {
        &self.light_vols[index as usize]
    }
}