00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028
00029 #if !defined __cplusplus
00030 #error C++ compiler required
00031 #endif
00032
00033
00034 #include <exception>
00035 #include <sstream>
00036 #include <stdexcept>
00037 #include <string>
00038 #include <vector>
00039
00040
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
00174 if (lump.size () % Animation::LENGTH != 0) return false;
00175
00176
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
00194 for (const_iterator i = this->begin (); i != this->end (); ++i, p += Animation::LENGTH)
00195 i->write (l, p);
00196
00197
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 }