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

animated.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 #include <vector>
00039 
00040 // Doom headers
00041 #include "global.hpp"
00042 #include "lump.hpp"
00043 #include "animated.hpp"
00044 #include "wadentry.hpp"
00045 
00046 using namespace Doomwad;
00047 
00048 const Doomwad::byte Animation::FLAT = 0x00;
00049 const Doomwad::byte Animation::TEXTURE = 0x01;
00050 const Doomwad::byte Animation::TERMINATE = 0xFF;
00051 const Doomwad::byte Animation::DEFAULT_TYPE = Animation::TEXTURE;
00052 const uint32 Animation::DEFAULT_SPEED = 0x00000008;
00053 const size_t Animation::LENGTH = 0x00000017;
00054 
00055 const std::string Animated::NAME = "ANIMATED";
00056 
00063 Animation::Animation (void) throw () : type (TEXTURE), speed (DEFAULT_SPEED)
00064 {
00065   return;
00066 }
00067 
00081 Animation::Animation (const std::string &_first, const std::string &_last, byte _type, uint32 _speed) : type (_type), last (_last), first (_first), speed (_speed)
00082 {
00083   return;
00084 }
00085 
00089 Animation::~Animation (void) throw ()
00090 {
00091   return;
00092 }
00093 
00094 size_t Animation::getLength (void) const throw ()
00095 {
00096   return LENGTH;
00097 }
00098 
00099 bool Animation::write (Lump &lump, Lump::size_type i) const throw ()
00100 {
00101   try
00102   {
00103     lump.setByte   (this->type,  i);
00104     lump.setString (this->last,  i + 1);
00105     lump.setString (this->first, i + 10);
00106     lump.setUInt32 (speed,       i + 19);
00107   }
00108   catch (std::range_error &e)
00109   {
00110     return false;
00111   }
00112   return true;
00113 }
00114 
00115 bool Animation::read (const Lump &lump, Lump::size_type i) throw ()
00116 {
00117   try
00118   {
00119     this->type  = lump.getByte   (i);
00120     this->last  = lump.getString (i + 1);
00121     this->first = lump.getString (i + 10);
00122     this->speed = lump.getUInt32 (i + 19);
00123   }
00124   catch (std::range_error &e)
00125   {
00126     return false;
00127   }
00128   return true;
00129 }
00130 
00131 std::string Animation::toString (void) const throw ()
00132 {
00133   std::ostringstream str;
00134   str << (type == 1 ? "Texture:" : "Flat:") << last << ":" << first << ":" << speed;
00135   return str.str ();
00136 }
00137 
00141 Animated::Animated (void) throw ()
00142 {
00143   return;
00144 }
00145 
00154 Animated::Animated (const Lump &lump) throw ()
00155 {
00156   this->setFromLump (lump);
00157   return;
00158 }
00159 
00163 Animated::~Animated (void) throw ()
00164 {
00165   return;
00166 }
00167 
00168 bool Animated::setFromLump (const Lump &lump) throw ()
00169 {
00170   size_t i = 0;
00171   Animation temp;
00172 
00173   // Lump size must be a multiple of 23 bytes.
00174   if (lump.size () % Animation::LENGTH != 0) return false;
00175 
00176   // Loop until the data runs out or we find an end marker.
00177   this->clear ();
00178   for (i = 0; (i < lump.size ()) && (lump[i] != Animation::TERMINATE); i += Animation::LENGTH)
00179   {
00180     temp.read (lump, i);
00181     this->insert (this->end (), temp);
00182   }
00183 
00184   return true;
00185 }
00186 
00187 Lump Animated::toLump (void) const throw ()
00188 {
00189   Lump l (NAME, (this->size () + 1) * Animation::LENGTH);
00190   Animation temp ("", "", Animation::TERMINATE, 0);
00191   Lump::size_type p = 0;
00192 
00193   // Loop over this object, creating new records and adding them to the lump.
00194   for (const_iterator i = this->begin (); i != this->end (); ++i, p += Animation::LENGTH)
00195     i->write (l, p);
00196 
00197   // Add the dummy end record.
00198   temp.write (l, p);
00199 
00200   return l;
00201 }
00202 
00203 std::string Animated::toString (void) const throw ()
00204 {
00205   std::ostringstream str;
00206   bool first = true;
00207   for (const_iterator i = this->begin (); i != this->end (); ++i, first = false)
00208   {
00209     if (!first) str << '\n';
00210     str << i->toString ();
00211   }
00212   return str.str ();
00213 }

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