Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

sidedefs.cpp

Go to the documentation of this file.
00001 // libdoomwad: manipulates Doom wad files.
00002 // Copyright (C) 2005  John Gaughan
00003 //
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 //
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 //
00018 // This library is distributed with the full text of the LGPL. Please see
00019 // the accompanying file named COPYING.
00020 // 
00021 // You may contact the author at john@johngaughan.net
00022 
00028 // C++ required.
00029 #if !defined __cplusplus
00030 #error C++ compiler required
00031 #endif
00032 
00033 // C++ headers
00034 #include <exception>
00035 #include <sstream>
00036 #include <stdexcept>
00037 #include <string>
00038 
00039 // Doom headers
00040 #include "global.hpp"
00041 #include "lump.hpp"
00042 #include "sidedefs.hpp"
00043 #include "wadentry.hpp"
00044 
00045 using namespace Doomwad;
00046 
00047 const size_t      Sidedef::LENGTH     = 0x0000001E;
00048 const std::string Sidedef::NO_TEXTURE = "-";
00049 const uint16      Sidedef::NO_SECTOR  = 0xFFFF;
00050 
00051 const std::string Sidedefs::NAME = "SIDEDEFS";
00052 
00065 Sidedef::Sidedef (int16 _x, int16 _y, const std::string &_upper, const std::string &_lower, const std::string &_middle, uint16 _sector) throw () : x_offset (_x), y_offset (_y), upper (_upper), lower (_lower), middle (_middle), sector (_sector)
00066 {
00067 }
00068 
00072 Sidedef::~Sidedef (void) throw ()
00073 {
00074 }
00075 
00076 size_t Sidedef::getLength (void) const throw ()
00077 {
00078   return LENGTH;
00079 }
00080 
00081 bool Sidedef::write (Lump &lump, Lump::size_type i) const throw ()
00082 {
00083   try
00084   {
00085     lump.setInt16 (this->x_offset, i);
00086     lump.setInt16 (this->y_offset, i + 2);
00087     lump.setDString (this->upper, 4);
00088     lump.setDString (this->lower, 12);
00089     lump.setDString (this->middle, 20);
00090     lump.setUInt16 (this->sector, i + 28);
00091   }
00092   catch (std::range_error &e)
00093   {
00094     return false;
00095   }
00096   return true;
00097 }
00098 
00099 bool Sidedef::read (const Lump &lump, Lump::size_type i) throw ()
00100 {
00101   try
00102   {
00103     this->x_offset = lump.getInt16 (i);
00104     this->y_offset = lump.getInt16 (i + 2);
00105     this->upper    = lump.getDString (i + 4);
00106     this->lower    = lump.getDString (i + 12);
00107     this->middle   = lump.getDString (i + 20);
00108     this->sector   = lump.getUInt16 (i + 28);
00109   }
00110   catch (std::range_error &e)
00111   {
00112     return false;
00113   }
00114   return true;
00115 }
00116 
00117 std::string Sidedef::toString (void) const throw ()
00118 {
00119   std::ostringstream str;
00120   char buffer[9];
00121   buffer[8] = 0;
00122 
00123   str << '(' << x_offset << ',' << y_offset << ','
00124       << upper << ',' << lower << ','
00125       << middle << ',' << sector << ')';
00126 
00127   return str.str ();
00128 }
00129 
00133 Sidedefs::Sidedefs (void) throw ()
00134 {
00135   return;
00136 }
00137 
00147 Sidedefs::Sidedefs (const Lump &lump) throw ()
00148 {
00149   this->setFromLump (lump);
00150   return;
00151 }
00152 
00158 Sidedefs::~Sidedefs (void) throw ()
00159 {
00160   return;
00161 }
00162 
00163 bool Sidedefs::setFromLump (const Lump &lump) throw ()
00164 {
00165   const size_t q = lump.size ();
00166   if (q % Sidedef::LENGTH != 0) return false;
00167 
00168   Sidedef s;
00169   this->clear ();
00170 
00171   for (size_t i = 0; i < q; i += Sidedef::LENGTH)
00172   {
00173     s.read (lump, i);
00174     this->push_back (s);
00175   }
00176 
00177   return true;
00178 }
00179 
00180 Lump Sidedefs::toLump (void) const throw ()
00181 {
00182   Lump lump (NAME, this->size () * Sidedef::LENGTH);
00183   const_iterator iter = this->begin ();
00184   for (size_t i = 0; iter != this->end (); i += Sidedef::LENGTH, ++iter)
00185   {
00186     iter->write (lump, i);
00187   }
00188   return lump;
00189 }
00190 
00191 std::string Sidedefs::toString (void) const throw ()
00192 {
00193   std::ostringstream str;
00194   for (const_iterator iter = this->begin (); iter != this->end (); ++iter)
00195     str << iter->toString () << '\n';
00196   return str.str ();
00197 }

Generated on Fri Jun 10 19:38:51 2005 for libdoomwad by  doxygen 1.4.0