Skip to content

Instantly share code, notes, and snippets.

@eddyb
Forked from dobkeratops/gist:9686969
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddyb/9687031 to your computer and use it in GitHub Desktop.
Save eddyb/9687031 to your computer and use it in GitHub Desktop.
struct Blob<HEADER> {
data:~[u8],
}
impl<T> std::ops::Deref<T> for Blob<T> {
fn deref<'s>(&'s self)->&'s T {
//assert!(self.num_bytes() > std::intrinsics::size_of::<T>());
unsafe { &*(&self.data[0] as *u8 as *T)
}
}
}
impl<T> Blob<T> {
fn num_bytes(&self) -> uint { self.data.len() }
fn read(path:&Path)->Blob<T> {
let data=
match io::File::open(path).read_to_end() {
Ok(data)=>{
println!("read {} {} bytes", /*path_to_str*/path.as_str().unwrap_or(""), data.len());
data
},
Err(E)=>{
println!("failed to read {}", path.as_str().unwrap_or(""));
//~[0,..intrinsics::size_of::<Header>()] // still returns an empty object, hmmm.
//vec::from_elem(0,intrinsics::size_of::<Header>())
~[]
}
};
Blob::<T> {data:data}
}
}
struct DEntry<T> {
offset:u32,
size:u32
}
struct BspHeader {
version: u32,
entities: DEntry<Entity, [u8, ..(4 + 8 * 0)]>,
planes: DEntry<Plane, [u8, ..(4 + 8 * 1)]>,
miptex: DEntry<MipTex, [u8, ..(4 + 8 * 2)]>,
vertices: DEntry<Vec3f, [u8, ..(4 + 8 * 3)]>,
visilist: DEntry<VisiLis, [u8, ..(4 + 8 * 4)]t>,
nodes: DEntry<BspNodes, [u8, ..(4 + 8 * 5)]>,
texinfo: DEntry<TextureInfo,[u8, ..(4 + 8 * 6)]>,
faces: DEntry<Faces, [u8, ..(4 + 8 * 7)]>,
lightmaps: DEntry<LightMap, [u8, ..(4 + 8 * 8)]>,
clipnodes: DEntry<ClipNode, [u8, ..(4 + 8 * 9)]>,
leaves: DEntry<BspLeaf, [u8, ..(4 + 8 * 10)]>,
lface: DEntry<Face, [u8, ..(4 + 8 * 11)]>,
edges: DEntry<Edge, [u8, ..(4 + 8 * 12)]>,
ledges: DEntry<Edge, [u8, ..(4 + 8 * 13)]>,
modesl: DEntry<Model, [u8, ..(4 + 8 * 14)]>
}
/*impl BspIdTech2 {
fn header<'a>(&'a self)->&'a BspHeader {
unsafe {&*(&self.data[0] as *u8 as *BspHeader)}
}
}
*/
struct SubElem<BASE,ELEM> {
base:&BASE,
}
trait HasDEntries {
fn get<T>(&self, d:&DEntry<T>, i:uint/*=0*/)->&T {
unsafe {
&*
(intrinsics::offset::<T>(
(intrinsics::offset::<u8>(self as *Self as *u8, d.offset as int) as *T),
i as int))
}
}
fn len<T>(&self, d:&DEntry<T>)->uint {
unsafe {
d.size as uint / intrinsics::size_of::<T>()
}
}
}
impl HasDEntries for BspHeader {}
impl BspHeader {
fn dump_vertices(&self) {
let mut i=0u;
while i<self.len(&self.edges) {
println!("vertex{}:{}",
i, *self.get(&self.vertices,i));i+=1;}
}
fn dump(&self) {
println!("vertices: {:u}", self.len(&self.vertices));
self.dump_vertices();
}
fn draw_edges(&self) {
let mut i=0u;
while i < self.len(&self.edges) {
let e= self.get(&self.edges,i);
let v0 = self.get(&self.vertices,e.vertex0 as uint);
let v1 = self.get(&self.vertices,e.vertex1 as uint);
let scale=1.0/3000.0;
draw_line_v3_iso(&v0.vscale(scale),&v1.vscale(scale));
i+=1;
}
}
}
struct Entity;
struct Plane;
struct MipTex;
struct VisiList;
struct BspNodes;
struct TextureInfo;
struct Faces;
struct LightMap;
struct ClipNode;
struct BspLeaf;
struct Edge {
vertex0:u16,vertex1:u16
}
struct Model;
struct Face {
plane_id:u16,
side:u16,
ledge_id:i32,
ledge_num:u16,
texinfo_id:u16,
typelight:u8,
baselight:u8,
light:[u8,..2],
lightmap:i32
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment