better macro usage

png-branch
an 2019-02-20 23:11:57 -05:00
parent 9f6e67bbef
commit 977f3d3f40
5 changed files with 73 additions and 117 deletions

View File

@ -27,6 +27,18 @@ macro_rules! rd_1 {
rd_1!($b u32::from_be_bytes, $nam 4 $n);
let $nam = $nam as usize;
};
(BE $b:ident $nam:ident Angle $n:expr) => {
rd_1!($b u16::from_be_bytes, $nam 2 $n);
let $nam = Angle::from_bits($nam);
};
(BE $b:ident $nam:ident Fixed $n:expr) => {
rd_1!($b u32::from_be_bytes, $nam 4 $n);
let $nam = Fixed::from_bits($nam);
};
(BE $b:ident $nam:ident Unit $n:expr) => {
rd_1!($b u16::from_be_bytes, $nam 2 $n);
let $nam = Unit::from_bits($nam);
};
// little endian
(LE $b:ident $nam:ident u16 $n:expr) => {

View File

@ -56,11 +56,11 @@ pub fn read_point(b: &[u8]) -> ResultS<Point>
{
read_data! {
4, BE in b =>
x = u16[0];
y = u16[2];
x = Unit[0];
y = Unit[2];
}
Ok(Point{x: Unit::from_bits(x), y: Unit::from_bits(y)})
Ok(Point{x, y})
}
pub fn read_epnt(b: &[u8]) -> ResultS<(Point, usize)>
@ -114,7 +114,7 @@ pub fn read_sids(b: &[u8]) -> ResultS<(Side, usize)>
xfer_pri = u16[42];
xfer_sec = u16[44];
xfer_tra = u16[46];
shade = u32[48];
shade = Fixed[48];
}
let flags = ok!(SideFlags::from_bits(flags), "bad SideFlags")?;
@ -122,7 +122,6 @@ pub fn read_sids(b: &[u8]) -> ResultS<(Side, usize)>
let xfer_sec = TransferMode::from_repr(xfer_sec)?;
let xfer_tra = TransferMode::from_repr(xfer_tra)?;
let stype = SideType::from_repr(stype)?;
let shade = Fixed::from_bits(shade);
Ok((Side{stype, flags, tex_pri, tex_sec, tex_tra, paneltyp, paneldat,
xfer_pri, xfer_sec, xfer_tra, shade}, 64))
@ -136,8 +135,8 @@ pub fn read_poly(b: &[u8]) -> ResultS<(Polygon, usize)>
pdata = i16[4];
tex_flr = u16[40];
tex_cei = u16[42];
hei_flr = u16[44];
hei_cei = u16[46];
hei_flr = Unit[44];
hei_cei = Unit[46];
lit_flr = u16[48];
lit_cei = u16[50];
xfr_flr = u16[64];
@ -159,8 +158,6 @@ pub fn read_poly(b: &[u8]) -> ResultS<(Polygon, usize)>
let snd_amb = ObjID::from_repr(snd_amb);
let snd_rnd = ObjID::from_repr(snd_rnd);
let med_ind = ObjID::from_repr(med_ind);
let hei_flr = Unit::from_bits(hei_flr);
let hei_cei = Unit::from_bits(hei_cei);
Ok((Polygon{ptype, pdata, tex_flr, tex_cei, hei_flr, hei_cei, lit_flr,
lit_cei, xfr_flr, xfr_cei, ori_flr, ori_cei, med_ind, med_ctl,
@ -196,11 +193,11 @@ pub fn read_objs(b: &[u8]) -> ResultS<(Object, usize)>
16, BE in b =>
group = u16[0];
index = u16[2];
angle = u16[4];
angle = Angle[4];
poly = u16[6];
pos_x = u16[8];
pos_y = u16[10];
pos_z = u16[12];
pos_x = Unit[8];
pos_y = Unit[10];
pos_z = Unit[12];
flags = u16[14];
}
@ -208,10 +205,6 @@ pub fn read_objs(b: &[u8]) -> ResultS<(Object, usize)>
let flags = flags & 0x0F_FF;
let bias = bias >> 12;
let flags = ok!(ObjectFlags::from_bits(flags), "bad ObjectFlags")?;
let angle = Angle::from_bits(angle);
let pos_x = Unit::from_bits(pos_x);
let pos_y = Unit::from_bits(pos_y);
let pos_z = Unit::from_bits(pos_z);
Ok((Object{group, index, angle, poly, pos_x, pos_y, pos_z, flags, bias}, 16))
}
@ -254,17 +247,13 @@ pub fn read_bonk(b: &[u8]) -> ResultS<(SoundRand, usize)>
vol_dta = u16[6];
prd_nrm = u16[8];
prd_dta = u16[10];
yaw_nrm = u16[12];
yaw_dta = u16[14];
pit_nrm = u32[16];
pit_dta = u32[20];
yaw_nrm = Angle[12];
yaw_dta = Angle[14];
pit_nrm = Fixed[16];
pit_dta = Fixed[20];
}
let no_dir = flags != 0;
let yaw_nrm = Angle::from_bits(yaw_nrm);
let yaw_dta = Angle::from_bits(yaw_dta);
let pit_nrm = Fixed::from_bits(pit_nrm);
let pit_dta = Fixed::from_bits(pit_dta);
let no_dir = flags != 0;
Ok((SoundRand{no_dir, index, vol_nrm, vol_dta, prd_nrm, prd_dta, yaw_nrm,
yaw_dta, pit_nrm, pit_dta}, 32))
@ -277,13 +266,13 @@ pub fn read_medi(b: &[u8]) -> ResultS<(Media, usize)>
mtype = u16[0];
flags = u16[2];
control = u16[4];
dir = u16[6];
mag = u16[8];
hei_lo = u16[10];
hei_hi = u16[12];
dir = Angle[6];
mag = Unit[8];
hei_lo = Unit[10];
hei_hi = Unit[12];
orig = read_point[14..18];
hei_nrm = u16[18];
min_lt = u32[20];
hei_nrm = Unit[18];
min_lt = Fixed[20];
texture = u16[24];
xfer = u16[26];
}
@ -291,12 +280,6 @@ pub fn read_medi(b: &[u8]) -> ResultS<(Media, usize)>
let mtype = MediaType::from_repr(mtype)?;
let xfer = TransferMode::from_repr(xfer)?;
let texture = ObjID::from_repr(texture);
let min_lt = Fixed::from_bits(min_lt);
let hei_nrm = Unit::from_bits(hei_nrm);
let hei_hi = Unit::from_bits(hei_hi);
let hei_lo = Unit::from_bits(hei_lo);
let dir = Angle::from_bits(dir);
let mag = Unit::from_bits(mag);
let flr_obs = flags != 0;
Ok((Media{mtype, flr_obs, control, dir, mag, hei_lo, hei_hi, orig, hei_nrm,
@ -310,16 +293,13 @@ pub fn read_plat(b: &[u8]) -> ResultS<(Platform, usize)>
ptype = u16[0];
speed = u16[2];
delay = u16[4];
hei_max = u16[6];
hei_min = u16[8];
hei_max = Unit[6];
hei_min = Unit[8];
flags = u32[10];
index = u16[14];
tag = u16[16];
}
let hei_min = Unit::from_bits(hei_min);
let hei_max = Unit::from_bits(hei_max);
Ok((Platform{ptype, speed, delay, hei_min, hei_max, flags, index, tag}, 32))
}

View File

@ -5,61 +5,34 @@ pub fn read_pxpx(b: &[u8]) -> ResultS<(Physics, usize)>
{
read_data! {
104, BE in b =>
vel_fwd = u32[0];
vel_bkw = u32[4];
vel_prp = u32[8];
acc_nrm = u32[12];
dec_nrm = u32[16];
dec_air = u32[20];
acc_grv = u32[24];
acc_cli = u32[28];
vel_trm = u32[32];
dec_ext = u32[36];
acc_ang = u32[40];
dec_ang = u32[44];
vel_ang = u32[48];
vel_rec = u32[52];
fng_vel = u32[56];
fng_max = u32[60];
ele_max = u32[64];
dec_xng = u32[68];
stp_dta = u32[72];
stp_amp = u32[76];
ply_rad = u32[80];
ply_hei = u32[84];
ply_dhi = u32[88];
ply_cam = u32[92];
ply_spl = u32[96];
ply_hcm = u32[100];
vel_fwd = Fixed[0];
vel_bkw = Fixed[4];
vel_prp = Fixed[8];
acc_nrm = Fixed[12];
dec_nrm = Fixed[16];
dec_air = Fixed[20];
acc_grv = Fixed[24];
acc_cli = Fixed[28];
vel_trm = Fixed[32];
dec_ext = Fixed[36];
acc_ang = Fixed[40];
dec_ang = Fixed[44];
vel_ang = Fixed[48];
vel_rec = Fixed[52];
fng_vel = Fixed[56];
fng_max = Fixed[60];
ele_max = Fixed[64];
dec_xng = Fixed[68];
stp_dta = Fixed[72];
stp_amp = Fixed[76];
ply_rad = Fixed[80];
ply_hei = Fixed[84];
ply_dhi = Fixed[88];
ply_cam = Fixed[92];
ply_spl = Fixed[96];
ply_hcm = Fixed[100];
}
let vel_fwd = Fixed::from_bits(vel_fwd);
let vel_bkw = Fixed::from_bits(vel_bkw);
let vel_prp = Fixed::from_bits(vel_prp);
let acc_nrm = Fixed::from_bits(acc_nrm);
let dec_nrm = Fixed::from_bits(dec_nrm);
let dec_air = Fixed::from_bits(dec_air);
let acc_grv = Fixed::from_bits(acc_grv);
let acc_cli = Fixed::from_bits(acc_cli);
let vel_trm = Fixed::from_bits(vel_trm);
let dec_ext = Fixed::from_bits(dec_ext);
let acc_ang = Fixed::from_bits(acc_ang);
let dec_ang = Fixed::from_bits(dec_ang);
let vel_ang = Fixed::from_bits(vel_ang);
let vel_rec = Fixed::from_bits(vel_rec);
let fng_vel = Fixed::from_bits(fng_vel);
let fng_max = Fixed::from_bits(fng_max);
let ele_max = Fixed::from_bits(ele_max);
let dec_xng = Fixed::from_bits(dec_xng);
let stp_dta = Fixed::from_bits(stp_dta);
let stp_amp = Fixed::from_bits(stp_amp);
let ply_rad = Fixed::from_bits(ply_rad);
let ply_hei = Fixed::from_bits(ply_hei);
let ply_dhi = Fixed::from_bits(ply_dhi);
let ply_cam = Fixed::from_bits(ply_cam);
let ply_spl = Fixed::from_bits(ply_spl);
let ply_hcm = Fixed::from_bits(ply_hcm);
Ok((Physics{acc_ang, acc_cli, acc_grv, acc_nrm, dec_air, dec_ang, dec_ext,
dec_nrm, dec_xng, ele_max, fng_max, fng_vel, ply_cam, ply_dhi,
ply_hcm, ply_hei, ply_rad, ply_spl, stp_amp, stp_dta, vel_ang,

View File

@ -121,24 +121,17 @@ fn frame(b: &[u8]) -> ResultS<Frame>
read_data! {
36, BE in b =>
flags = u16[0];
min_lt = u32[2];
min_lt = Fixed[2];
bmp_ind = u16[6] as usize;
wrl_l = u16[16];
wrl_r = u16[18];
wrl_t = u16[20];
wrl_b = u16[22];
wrl_x = u16[24];
wrl_y = u16[26];
wrl_l = Unit[16];
wrl_r = Unit[18];
wrl_t = Unit[20];
wrl_b = Unit[22];
wrl_x = Unit[24];
wrl_y = Unit[26];
}
let flags = ok!(FrameFlags::from_bits(flags), "bad flag")?;
let min_lt = Fixed::from_bits(min_lt);
let wrl_l = Unit::from_bits(wrl_l);
let wrl_r = Unit::from_bits(wrl_r);
let wrl_t = Unit::from_bits(wrl_t);
let wrl_b = Unit::from_bits(wrl_b);
let wrl_x = Unit::from_bits(wrl_x);
let wrl_y = Unit::from_bits(wrl_y);
let flags = ok!(FrameFlags::from_bits(flags), "bad flag")?;
Ok(Frame{flags, min_lt, bmp_ind, wrl_l, wrl_r, wrl_t, wrl_b, wrl_x, wrl_y})
}

View File

@ -52,16 +52,14 @@ fn sound_def(b: &[u8]) -> ResultS<Option<(Vec<usize>, u16, SoundDef)>>
volume = u16[2];
flags = u16[4];
chance = u16[6];
pitch_lo = u32[8];
pitch_hi = u32[12];
pitch_lo = Fixed[8];
pitch_hi = Fixed[12];
n_sounds = u16[16] as usize;
grp_ofs = u32[20] as usize;
}
let flags = ok!(SoundFlags::from_bits(flags), "bad SoundFlags")?;
let volume = Volume::from_repr(volume)?;
let pitch_lo = Fixed::from_bits(pitch_lo);
let pitch_hi = Fixed::from_bits(pitch_hi);
let flags = ok!(SoundFlags::from_bits(flags), "bad SoundFlags")?;
let volume = Volume::from_repr(volume)?;
if index == u16::max_value() {
return Ok(None);