From d30f64650a323f84d7ea554b7e0d3e4728cc1b47 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 10 Jul 2012 04:21:47 -0700 Subject: [PATCH] Make the read_* methods private and remove the generic load() methods --- components/nif/data.hpp | 12 ++-- components/nif/effect.hpp | 2 +- components/nif/extra.hpp | 11 +-- components/nif/nif_file.cpp | 2 +- components/nif/nif_file.hpp | 140 +++++++++++++++++------------------- components/nif/node.hpp | 26 +++---- 6 files changed, 89 insertions(+), 104 deletions(-) diff --git a/components/nif/data.hpp b/components/nif/data.hpp index e77fd6239..118e21e54 100644 --- a/components/nif/data.hpp +++ b/components/nif/data.hpp @@ -105,16 +105,16 @@ public: int verts = nif->getUShort(); if(nif->getInt()) - nif->load(vertices, verts*3); + nif->getFloats(vertices, verts*3); if(nif->getInt()) - nif->load(normals, verts*3); + nif->getFloats(normals, verts*3); center = nif->getVector(); radius = nif->getFloat(); if(nif->getInt()) - nif->load(colors, verts*4); + nif->getFloats(colors, verts*4); // Only the first 6 bits are used as a count. I think the rest are // flags of some sort. @@ -122,7 +122,7 @@ public: uvs &= 0x3f; if(nif->getInt()) - nif->load(uvlist, uvs*verts*2); + nif->getFloats(uvlist, uvs*verts*2); } }; @@ -142,7 +142,7 @@ public: // We have three times as many vertices as triangles, so this // is always equal to tris*3. int cnt = nif->getInt(); - nif->load(triangles, cnt); + nif->getShorts(triangles, cnt); } // Read the match list, which lists the vertices that are equal to @@ -153,7 +153,7 @@ public: { // Number of vertices matching vertex 'i' int num = nif->getUShort(); - nif->skip(num*sizeof(short)); + nif->skip(num * sizeof(short)); } } }; diff --git a/components/nif/effect.hpp b/components/nif/effect.hpp index 30877b48c..1a2ecace8 100644 --- a/components/nif/effect.hpp +++ b/components/nif/effect.hpp @@ -44,7 +44,7 @@ struct NiLight : Effect void read(NIFFile *nif) { - nif->load(dimmer); + dimmer = nif->getFloat(); ambient = nif->getVector(); diffuse = nif->getVector(); specular = nif->getVector(); diff --git a/components/nif/extra.hpp b/components/nif/extra.hpp index 7659bb3d2..35781dbf5 100644 --- a/components/nif/extra.hpp +++ b/components/nif/extra.hpp @@ -47,21 +47,16 @@ public: class NiVertWeightsExtraData : public Extra { public: - std::vector weights; - void read(NIFFile *nif) { Extra::read(nif); - int i; - unsigned short s; - // We should have s*4+2 == i, for some reason. Might simply be the // size of the rest of the record, unhelpful as that may be. - nif->load(i); + /*int i =*/ nif->getInt(); + int s = nif->getUShort(); - nif->load(s); // number of vertices - nif->load(weights, s); // vertex weights I guess + nif->skip(s * sizeof(float)); // vertex weights I guess } }; diff --git a/components/nif/nif_file.cpp b/components/nif/nif_file.cpp index 5b88b45fe..36badbf0d 100644 --- a/components/nif/nif_file.cpp +++ b/components/nif/nif_file.cpp @@ -46,7 +46,7 @@ using namespace Misc; void NIFFile::parse() { // Check the header string - std::string head = read_string(40); + std::string head = getString(40); if(head.compare(0, 22, "NetImmerse File Format") != 0) fail("Invalid NIF header"); diff --git a/components/nif/nif_file.hpp b/components/nif/nif_file.hpp index 2b46f84f3..0218795e0 100644 --- a/components/nif/nif_file.hpp +++ b/components/nif/nif_file.hpp @@ -62,6 +62,33 @@ class NIFFile /// Parse the file void parse(); + uint8_t read_byte() + { + uint8_t byte; + if(inp->read(&byte, 1) != 1) return 0; + return byte; + } + uint16_t read_le16() + { + uint8_t buffer[2]; + if(inp->read(buffer, 2) != 2) return 0; + return buffer[0] | (buffer[1]<<8); + } + uint32_t read_le32() + { + uint8_t buffer[4]; + if(inp->read(buffer, 4) != 4) return 0; + return buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24); + } + float read_le32f() + { + union { + int i; + float f; + } u = { read_le32() }; + return u.f; + } + public: /// Used for error handling void fail(const std::string &msg) @@ -102,91 +129,34 @@ public: void skip(size_t size) { inp->skip(size); } - uint32_t read_le32() - { - uint8_t buffer[4]; - if(inp->read(buffer, 4) != 4) return 0; - return buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24); - } - uint16_t read_le16() - { - uint8_t buffer[2]; - if(inp->read(buffer, 2) != 2) return 0; - return buffer[0] | (buffer[1]<<8); - } - uint8_t read_byte() - { - uint8_t byte; - if(inp->read(&byte, 1) != 1) return 0; - return byte; - } - std::string read_string(size_t length) - { - std::string str; - str.resize(length); - if(inp->read(&str[0], length) != length) - return std::string(); - return str.substr(0, str.find('\0')); - } - - - char& load(char &c) { c = read_byte(); return c; } - unsigned char& load(unsigned char &c) { c = read_byte(); return c; } - short& load(short &s) { s = read_le16(); return s; } - unsigned short& load(unsigned short &s) { s = read_le16(); return s; } - int& load(int &i) { i = read_le32(); return i; } - unsigned int& load(unsigned int &i) { i = read_le32(); return i; } - float& load(float &f) - { - union { - int i; - float f; - } u = { read_le32() }; - f = u.f; - return f; - } - - template - T* load(T (&a)[N]) - { - for(size_t i = 0;i < N;i++) - load(a[i]); - return a; - } - - template - std::vector& load(std::vector &v, size_t size) - { - v.resize(size); - for(size_t i = 0;i < size;i++) - load(v[i]); - return v; - } - - - char getChar() { char c; return load(c); } - unsigned short getUShort() { unsigned short s; return load(s); } - int getInt() { int i; return load(i); } - float getFloat() { float f; return load(f); } + char getChar() { return read_byte(); } + short getShort() { return read_le16(); } + unsigned short getUShort() { return read_le16(); } + int getInt() { return read_le32(); } + float getFloat() { return read_le32f(); } Ogre::Vector3 getVector() { float a[3]; - load(a); + for(size_t i = 0;i < 3;i++) + a[i] = getFloat(); return Ogre::Vector3(a); } Ogre::Vector4 getVector4() { float a[4]; - load(a); + for(size_t i = 0;i < 4;i++) + a[i] = getFloat(); return Ogre::Vector4(a); } Ogre::Matrix3 getMatrix() { - float a[3*3]; - load(a); - return Ogre::Matrix3(Ogre::Real(a[0]), Ogre::Real(a[1]), Ogre::Real(a[2]), - Ogre::Real(a[3]), Ogre::Real(a[4]), Ogre::Real(a[5]), - Ogre::Real(a[6]), Ogre::Real(a[7]), Ogre::Real(a[8])); + Ogre::Real a[3][3]; + for(size_t i = 0;i < 3;i++) + { + for(size_t j = 0;j < 3;j++) + a[i][j] = Ogre::Real(getFloat()); + } + return Ogre::Matrix3(a); } Transformation getTrafo() { @@ -198,11 +168,31 @@ public: return t; } - + std::string getString(size_t length) + { + std::string str; + str.resize(length); + if(inp->read(&str[0], length) != length) + return std::string(); + return str.substr(0, str.find('\0')); + } std::string getString() { size_t size = read_le32(); - return read_string(size); + return getString(size); + } + + void getShorts(std::vector &vec, size_t size) + { + vec.resize(size); + for(size_t i = 0;i < vec.size();i++) + vec[i] = getShort(); + } + void getFloats(std::vector &vec, size_t size) + { + vec.resize(size); + for(size_t i = 0;i < vec.size();i++) + vec[i] = getFloat(); } }; diff --git a/components/nif/node.hpp b/components/nif/node.hpp index f86ea5af9..240dbe540 100644 --- a/components/nif/node.hpp +++ b/components/nif/node.hpp @@ -221,19 +221,19 @@ struct NiCamera : Node void read(NIFFile *nif) { - nif->load(left); - nif->load(right); - nif->load(top); - nif->load(bottom); - nif->load(nearDist); - nif->load(farDist); - - nif->load(vleft); - nif->load(vright); - nif->load(vtop); - nif->load(vbottom); - - nif->load(LOD); + left = nif->getFloat(); + right = nif->getFloat(); + top = nif->getFloat(); + bottom = nif->getFloat(); + nearDist = nif->getFloat(); + farDist = nif->getFloat(); + + vleft = nif->getFloat(); + vright = nif->getFloat(); + vtop = nif->getFloat(); + vbottom = nif->getFloat(); + + LOD = nif->getFloat(); } }; Camera cam;