aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/q3/textures.rs
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-08-25 17:44:23 +0100
committertcmal <me@aria.rip>2024-08-25 17:44:23 +0100
commit439219e74090c7158f8dbc33fed4107a5eb7c003 (patch)
tree7ba62254b2d888578ff6c1c8de4f0f35c01c75dd /stockton-levels/src/q3/textures.rs
parent04f17923d38171f07f72603a54237f20ca3572dd (diff)
refactor(levels): no longer q3 specific
Diffstat (limited to 'stockton-levels/src/q3/textures.rs')
-rw-r--r--stockton-levels/src/q3/textures.rs137
1 files changed, 0 insertions, 137 deletions
diff --git a/stockton-levels/src/q3/textures.rs b/stockton-levels/src/q3/textures.rs
deleted file mode 100644
index 98c2fe4..0000000
--- a/stockton-levels/src/q3/textures.rs
+++ /dev/null
@@ -1,137 +0,0 @@
-use std::str;
-
-use super::Q3BspFile;
-use crate::coords::CoordSystem;
-use crate::helpers::slice_to_u32;
-use crate::traits::textures::*;
-use crate::types::{ParseError, Result};
-
-const TEXTURE_LUMP_SIZE: usize = 64 + 4 + 4;
-
-/// Try to parse the given buffer as an entities lump.
-/// # Format
-/// Each entity is:
-/// string[64] name Texture name.
-/// int flags Surface flags.
-/// int contents Content flags.
-/// Length of entities is total lump size / TEXTURE_LUMP_SIZE (64 + 4 + 4)
-pub fn from_data(lump: &[u8]) -> Result<Box<[Texture]>> {
- if lump.is_empty() || lump.len() % TEXTURE_LUMP_SIZE != 0 {
- return Err(ParseError::Invalid);
- }
- let length = lump.len() / TEXTURE_LUMP_SIZE;
-
- let mut textures = Vec::with_capacity(length);
- for n in 0..length {
- let offset = n * TEXTURE_LUMP_SIZE;
- textures.push(Texture {
- name: str::from_utf8(&lump[offset..offset + 64])
- .map_err(|_| ParseError::Invalid)?
- .trim_matches('\0')
- .to_owned(),
- surface: SurfaceFlags::from_bits_truncate(slice_to_u32(
- &lump[offset + 64..offset + 68],
- )),
- contents: ContentsFlags::from_bits_truncate(slice_to_u32(
- &lump[offset + 68..offset + 72],
- )),
- });
- }
-
- Ok(textures.into_boxed_slice())
-}
-
-impl<T: CoordSystem> HasTextures for Q3BspFile<T> {
- type TexturesIter<'a> = std::slice::Iter<'a, Texture>;
-
- fn textures_iter(&self) -> Self::TexturesIter<'_> {
- self.textures.iter()
- }
-
- fn get_texture(&self, idx: u32) -> Option<&Texture> {
- if idx >= self.textures.len() as u32 {
- None
- } else {
- Some(&self.textures[idx as usize])
- }
- }
-}
-
-#[test]
-fn textures_single_texture() {
- let buf: &[u8] = &[
- b'T', b'E', b'S', b'T', b' ', b'T', b'E', b'X', b'T', b'U', b'R', b'E', 0x00, 0x00, 0x00,
- 0x00, // name (padded to 64 bytes)
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x43, 0x00, 0x04, 0x00, // surface flags
- 0x09, 0x00, 0x00, 0x00, // contents flags
- ];
-
- let lump = from_data(buf).unwrap();
-
- assert_eq!(lump.len(), 1);
-
- assert_eq!(lump[0].name, "TEST TEXTURE");
- assert_eq!(
- lump[0].surface,
- SurfaceFlags::NO_DAMAGE | SurfaceFlags::SLICK | SurfaceFlags::FLESH | SurfaceFlags::DUST
- );
- assert_eq!(lump[0].contents, ContentsFlags::SOLID | ContentsFlags::LAVA);
-}
-
-#[test]
-fn textures_multiple_textures() {
- let buf: &[u8] = &[
- b'T', b'E', b'S', b'T', b' ', b'T', b'E', b'X', b'T', b'U', b'R', b'E', b'1', 0x00, 0x00,
- 0x00, // name (padded to 64 bytes)
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x43, 0x00, 0x04, 0x00, // surface flags
- 0x09, 0x00, 0x00, 0x00, // contents flags
- b'T', b'E', b'S', b'T', b' ', b'T', b'E', b'X', b'T', b'U', b'R', b'E', b'2', 0x00, 0x00,
- 0x00, // name (padded to 64 bytes)
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x2c, 0x10, 0x00, 0x00, // surface flags
- 0x01, 0x00, 0x00, 0x00, // contents flags
- b'T', b'E', b'S', b'T', b' ', b'T', b'E', b'X', b'T', b'U', b'R', b'E', b'3', 0x00, 0x00,
- 0x00, // name (padded to 64 bytes)
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, // surface flags
- 0x41, 0x00, 0x00, 0x00, // contents flags
- ];
-
- let lump = from_data(buf).unwrap();
-
- assert_eq!(lump.len(), 3);
-
- assert_eq!(lump[0].name, "TEST TEXTURE1");
- assert_eq!(lump[1].name, "TEST TEXTURE2");
- assert_eq!(lump[2].name, "TEST TEXTURE3");
-
- assert_eq!(
- lump[0].surface,
- SurfaceFlags::NO_DAMAGE | SurfaceFlags::SLICK | SurfaceFlags::FLESH | SurfaceFlags::DUST
- );
- assert_eq!(
- lump[1].surface,
- SurfaceFlags::METAL_STEPS
- | SurfaceFlags::NO_MARKS
- | SurfaceFlags::LADDER
- | SurfaceFlags::SKY
- );
- assert_eq!(
- lump[2].surface,
- SurfaceFlags::POINT_LIGHT | SurfaceFlags::SKIP
- );
-
- assert_eq!(lump[0].contents, ContentsFlags::SOLID | ContentsFlags::LAVA);
- assert_eq!(lump[1].contents, ContentsFlags::SOLID);
- assert_eq!(lump[2].contents, ContentsFlags::SOLID | ContentsFlags::FOG);
-}