Skip to content

Commit

Permalink
update API
Browse files Browse the repository at this point in the history
  • Loading branch information
r4v3n6101 committed Oct 16, 2024
1 parent a1c55bb commit b568292
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "goldsrc-rs"
version = "0.12.1"
version = "0.13.1"
authors = ["r4v3n6101 <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ In your **Cargo.toml** add new dependency:

```toml
[dependcies]
goldsrc-rs = "0.10"
goldsrc-rs = "0.13"
```

## Usage
Expand Down
18 changes: 9 additions & 9 deletions src/parser/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ use std::{

use byteorder::{LittleEndian, ReadBytesExt};

use crate::texture::{CharInfo, ColourData, Font, MipTexture, Palette, Picture, Rgb};
use crate::texture::{CharInfo, ColorData, Font, MipTexture, Palette, Picture, Rgb, MIP_LEVELS};

use super::{chunk, chunk_with_offset, cstr16};

fn palette<R: Read>(mut reader: R) -> io::Result<Box<Palette>> {
let colours_used = reader.read_u16::<LittleEndian>()?.min(256) as usize; // index is u8
let colors_used = reader.read_u16::<LittleEndian>()?.min(256) as usize; // index is u8

let mut boxed_palette = Box::<Palette>::new_zeroed_slice(colours_used);
let mut boxed_palette = Box::<Palette>::new_zeroed_slice(colors_used);
let buf = unsafe {
slice::from_raw_parts_mut(
boxed_palette.as_mut_ptr() as *mut u8,
colours_used * mem::size_of::<Rgb>(),
colors_used * mem::size_of::<Rgb>(),
)
};
reader.read_exact(buf)?;
Expand All @@ -34,23 +34,23 @@ pub fn qpic<R: Read>(mut reader: R) -> io::Result<Picture> {
Ok(Picture {
width,
height,
data: ColourData { indices, palette },
data: ColorData { indices, palette },
})
}

pub fn miptex<R: Read>(mut reader: R) -> io::Result<MipTexture> {
let name = cstr16(&mut reader)?;
let width = reader.read_u32::<LittleEndian>()?;
let height = reader.read_u32::<LittleEndian>()?;
let offsets: [_; 4] = array::try_from_fn(|_| reader.read_u32::<LittleEndian>())?;
let offsets: [_; MIP_LEVELS] = array::try_from_fn(|_| reader.read_u32::<LittleEndian>())?;
let data = if offsets.iter().all(|&x| x != 0) {
let pixels = (width * height) as usize;

// Skip something between header and first mip indices
for _ in 0..(offsets[0].saturating_sub(40)) {
reader.read_u8()?;
}
let data_len = ((pixels * 85) >> 6) as u32 + 2 + 256 * 3;
let data_len = (pixels * 4 / 3) as u32 + 2 + 256 * 3;
let mut cursor = Cursor::new(vec![0; data_len as usize]);
reader.read_exact(cursor.get_mut())?;

Expand All @@ -63,7 +63,7 @@ pub fn miptex<R: Read>(mut reader: R) -> io::Result<MipTexture> {
})?;
let palette = palette(&mut cursor)?;

Some(ColourData { indices, palette })
Some(ColorData { indices, palette })
} else {
None
};
Expand Down Expand Up @@ -103,6 +103,6 @@ pub fn font<R: Read>(mut reader: R) -> io::Result<Font> {
row_count,
row_height,
chars_info,
data: ColourData { indices, palette },
data: ColorData { indices, palette },
})
}
16 changes: 16 additions & 0 deletions src/repr/bsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@ pub type BBoxShort = BBox<Vec3s>;
assert_eq_size!(BBoxFloat, [Vec3f; 2]);
pub type BBoxFloat = BBox<Vec3f>;

#[derive(Debug, Clone)]
pub struct BBox<T> {
pub min: T,
pub max: T,
}

assert_eq_size!(Plane, [u8; 20]);

#[derive(Debug, Clone)]
pub struct Plane {
pub normal: Vec3f,
pub distance: f32,
pub ty: u32,
}

assert_eq_size!(Node, [u8; 24]);

#[derive(Debug, Clone)]
pub struct Node {
pub plane_id: u32,
pub children: [i16; 2],
Expand All @@ -37,6 +42,8 @@ pub struct Node {
}

assert_eq_size!(TextureInfo, [u8; 40]);

#[derive(Debug, Clone)]
pub struct TextureInfo {
pub s: Vec3f,
pub s_shift: f32,
Expand All @@ -47,6 +54,8 @@ pub struct TextureInfo {
}

assert_eq_size!(Face, [u8; 20]);

#[derive(Debug, Clone)]
pub struct Face {
pub plane_id: u16,
pub plane_side: u16,
Expand All @@ -58,12 +67,16 @@ pub struct Face {
}

assert_eq_size!(ClipNode, [u8; 8]);

#[derive(Debug, Clone)]
pub struct ClipNode {
pub plane_id: u32,
pub children: [i16; 2],
}

assert_eq_size!(Leaf, [u8; 28]);

#[derive(Debug, Clone)]
pub struct Leaf {
pub contents: i32,
// TODO : rename probably
Expand All @@ -75,6 +88,8 @@ pub struct Leaf {
}

assert_eq_size!(Model, [u8; 64]);

#[derive(Debug, Clone)]
pub struct Model {
pub bounds: BBoxFloat,
pub origin: Vec3f,
Expand All @@ -87,6 +102,7 @@ pub struct Model {
pub faces_num: u32,
}

#[derive(Debug, Clone)]
pub struct Level {
pub entities: Entities,
pub planes: Vec<Plane>,
Expand Down
26 changes: 11 additions & 15 deletions src/repr/texture.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
use std::array;

use crate::CStr16;

pub const MIP_LEVELS: usize = 4;

pub type Rgb = [u8; 3];
pub type Palette = [Rgb];

pub struct ColourData<const N: usize> {
#[derive(Debug, Clone)]
pub struct ColorData<const N: usize> {
pub indices: [Vec<u8>; N],
pub palette: Box<Palette>,
}

impl<const N: usize> Default for ColourData<N> {
fn default() -> Self {
Self {
indices: array::from_fn(|_| Vec::new()),
palette: Box::default(),
}
}
}

#[derive(Debug, Clone)]
pub struct MipTexture {
pub name: CStr16,
pub width: u32,
pub height: u32,
pub data: Option<ColourData<4>>,
pub data: Option<ColorData<MIP_LEVELS>>,
}

#[derive(Debug, Clone)]
pub struct Picture {
pub width: u32,
pub height: u32,
pub data: ColourData<1>,
pub data: ColorData<1>,
}

#[derive(Debug, Clone)]
pub struct CharInfo {
pub offset: u16,
pub width: u16,
}

#[derive(Debug, Clone)]
pub struct Font {
pub width: u32,
pub height: u32,
pub row_count: u32,
pub row_height: u32,
pub chars_info: Box<[CharInfo]>,
pub data: ColourData<1>,
pub data: ColorData<1>,
}
14 changes: 14 additions & 0 deletions src/repr/wad.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
fmt,
io::{self, Read, Seek, SeekFrom},
sync::{Arc, Mutex},
};
Expand Down Expand Up @@ -42,6 +43,7 @@ impl Read for SharedChunkReader {
}
}

#[derive(Clone)]
pub struct Entry {
pub(crate) source: Arc<Mutex<dyn Reader>>,
pub offset: u32,
Expand All @@ -51,6 +53,18 @@ pub struct Entry {
pub compression: u8,
}

impl fmt::Debug for Entry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Entry")
.field("offset", &self.offset)
.field("full_size", &self.full_size)
.field("size", &self.size)
.field("ty", &self.ty)
.field("compression", &(self.compression != 0))
.finish()
}
}

impl Entry {
pub fn reader(&self) -> impl Read {
SharedChunkReader {
Expand Down
4 changes: 2 additions & 2 deletions tests/wad.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use goldsrc_rs::{texture::ColourData, wad::ContentType};
use goldsrc_rs::{texture::ColorData, wad::ContentType};

fn save_img<const N: usize>(name: &str, width: u32, height: u32, data: &ColourData<N>) {
fn save_img<const N: usize>(name: &str, width: u32, height: u32, data: &ColorData<N>) {
let data = data.indices[0]
.iter()
.flat_map(|&i| {
Expand Down

0 comments on commit b568292

Please sign in to comment.