Panda3D

animPreloadTable.cxx

00001 // Filename: animPreloadTable.cxx
00002 // Created by:  drose (05Aug08)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "animPreloadTable.h"
00016 
00017 #include "indent.h"
00018 #include "datagram.h"
00019 #include "datagramIterator.h"
00020 #include "bamReader.h"
00021 #include "bamWriter.h"
00022 
00023 TypeHandle AnimPreloadTable::_type_handle;
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: AnimPreloadTable::make_cow_copy
00027 //       Access: Protected, Virtual
00028 //  Description: Required to implement CopyOnWriteObject.
00029 ////////////////////////////////////////////////////////////////////
00030 PT(CopyOnWriteObject) AnimPreloadTable::
00031 make_cow_copy() {
00032   return new AnimPreloadTable(*this);
00033 }
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //     Function: AnimPreloadTable::Constructor
00037 //       Access: Published
00038 //  Description: 
00039 ////////////////////////////////////////////////////////////////////
00040 AnimPreloadTable::
00041 AnimPreloadTable() {
00042   _needs_sort = false;
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: AnimPreloadTable::Destructor
00047 //       Access: Published, Virtual
00048 //  Description: 
00049 ////////////////////////////////////////////////////////////////////
00050 AnimPreloadTable::
00051 ~AnimPreloadTable() {
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: AnimPreloadTable::get_num_anims
00056 //       Access: Published
00057 //  Description: Returns the number of animation records in the table.
00058 ////////////////////////////////////////////////////////////////////
00059 int AnimPreloadTable::
00060 get_num_anims() const {
00061   return (int)_anims.size();
00062 }
00063 
00064 ////////////////////////////////////////////////////////////////////
00065 //     Function: AnimPreloadTable::find_anim
00066 //       Access: Published
00067 //  Description: Returns the index number in the table of the
00068 //               animation record with the indicated name, or -1 if
00069 //               the name is not present.  By convention, the basename
00070 //               is the filename of the egg or bam file, without the
00071 //               directory part and without the extension.  That is,
00072 //               it is Filename::get_basename_wo_extension().
00073 ////////////////////////////////////////////////////////////////////
00074 int AnimPreloadTable::
00075 find_anim(const string &basename) const {
00076   consider_sort();
00077   AnimRecord record;
00078   record._basename = basename;
00079   Anims::const_iterator ai = _anims.find(record);
00080   if (ai != _anims.end()) {
00081     return int(ai - _anims.begin());
00082   }
00083   return -1;
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: AnimPreloadTable::clear_anims
00088 //       Access: Published
00089 //  Description: Removes all animation records from the table.
00090 ////////////////////////////////////////////////////////////////////
00091 void AnimPreloadTable::
00092 clear_anims() {
00093   _anims.clear();
00094   _needs_sort = false;
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: AnimPreloadTable::remove_anim
00099 //       Access: Published
00100 //  Description: Removes the nth animation records from the table.
00101 //               This renumbers indexes for following animations.
00102 ////////////////////////////////////////////////////////////////////
00103 void AnimPreloadTable::
00104 remove_anim(int n) {
00105   nassertv(n >= 0 && n < (int)_anims.size());
00106   _anims.erase(_anims.begin() + n);
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: AnimPreloadTable::add_anim
00111 //       Access: Published
00112 //  Description: Adds a new animation record to the table.  If there
00113 //               is already a record of this name, no operation is
00114 //               performed (the original record is unchanged).  See
00115 //               find_anim().  This will invalidate existing index
00116 //               numbers.
00117 ////////////////////////////////////////////////////////////////////
00118 void AnimPreloadTable::
00119 add_anim(const string &basename, PN_stdfloat base_frame_rate, int num_frames) {
00120   AnimRecord record;
00121   record._basename = basename;
00122   record._base_frame_rate = base_frame_rate;
00123   record._num_frames = num_frames;
00124 
00125   _anims.push_back(record);
00126   _needs_sort = true;
00127 }
00128 
00129 ////////////////////////////////////////////////////////////////////
00130 //     Function: AnimPreloadTable::add_anims_from
00131 //       Access: Published
00132 //  Description: Copies the animation records from the other table
00133 //               into this one.  If a given record name exists in both
00134 //               tables, the record in this one supercedes.
00135 ////////////////////////////////////////////////////////////////////
00136 void AnimPreloadTable::
00137 add_anims_from(const AnimPreloadTable *other) {
00138   _anims.reserve(_anims.size() + other->_anims.size());
00139   Anims::const_iterator ai;
00140   for (ai = other->_anims.begin(); ai != other->_anims.end(); ++ai) {
00141     _anims.push_back(*ai);
00142   }
00143   _needs_sort = true;
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //     Function: AnimPreloadTable::output
00148 //       Access: Published
00149 //  Description: 
00150 ////////////////////////////////////////////////////////////////////
00151 void AnimPreloadTable::
00152 output(ostream &out) const {
00153   consider_sort();
00154   out << "AnimPreloadTable, " << _anims.size() << " animation records.";
00155 }
00156 
00157 ////////////////////////////////////////////////////////////////////
00158 //     Function: AnimPreloadTable::write
00159 //       Access: Published
00160 //  Description: 
00161 ////////////////////////////////////////////////////////////////////
00162 void AnimPreloadTable::
00163 write(ostream &out, int indent_level) const {
00164   consider_sort();
00165   indent(out, indent_level)
00166     << "AnimPreloadTable, " << _anims.size() << " animation records:\n";
00167   Anims::const_iterator ai;
00168   for (ai = _anims.begin(); ai != _anims.end(); ++ai) {
00169     const AnimRecord &record = (*ai);
00170     indent(out, indent_level + 2)
00171       << record._basename << ": " << record._num_frames << " frames at "
00172       << record._base_frame_rate << " fps\n";
00173   }
00174 }
00175 
00176 ////////////////////////////////////////////////////////////////////
00177 //     Function: AnimPreloadTable::register_with_read_factory
00178 //       Access: Public, Static
00179 //  Description: Factory method to generate an AnimPreloadTable object
00180 ////////////////////////////////////////////////////////////////////
00181 void AnimPreloadTable::
00182 register_with_read_factory() {
00183   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00184 }
00185 
00186 ////////////////////////////////////////////////////////////////////
00187 //     Function: AnimPreloadTable::write_datagram
00188 //       Access: Public
00189 //  Description: Function to write the important information in
00190 //               the particular object to a Datagram
00191 ////////////////////////////////////////////////////////////////////
00192 void AnimPreloadTable::
00193 write_datagram(BamWriter *manager, Datagram &dg) {
00194   consider_sort();
00195 
00196   dg.add_uint16(_anims.size());
00197   Anims::const_iterator ai;
00198   for (ai = _anims.begin(); ai != _anims.end(); ++ai) {
00199     const AnimRecord &record = (*ai);
00200     dg.add_string(record._basename);
00201     dg.add_stdfloat(record._base_frame_rate);
00202     dg.add_int32(record._num_frames);
00203   }
00204 }
00205 
00206 ////////////////////////////////////////////////////////////////////
00207 //     Function: AnimPreloadTable::make_from_bam
00208 //       Access: Protected
00209 //  Description: Factory method to generate an AnimPreloadTable object
00210 ////////////////////////////////////////////////////////////////////
00211 TypedWritable *AnimPreloadTable::
00212 make_from_bam(const FactoryParams &params) {
00213   AnimPreloadTable *me = new AnimPreloadTable;
00214   DatagramIterator scan;
00215   BamReader *manager;
00216 
00217   parse_params(params, scan, manager);
00218   me->fillin(scan, manager);
00219   return me;
00220 }
00221 
00222 ////////////////////////////////////////////////////////////////////
00223 //     Function: AnimPreloadTable::fillin
00224 //       Access: Protected
00225 //  Description: Function that reads out of the datagram (or asks
00226 //               manager to read) all of the data that is needed to
00227 //               re-create this object and stores it in the appropiate
00228 //               place
00229 ////////////////////////////////////////////////////////////////////
00230 void AnimPreloadTable::
00231 fillin(DatagramIterator &scan, BamReader *manager) {
00232   int num_anims = scan.get_uint16();
00233   _anims.reserve(num_anims);
00234   for (int i = 0; i < num_anims; ++i) {
00235     AnimRecord record;
00236     record._basename = scan.get_string();
00237     record._base_frame_rate = scan.get_stdfloat();
00238     record._num_frames = scan.get_int32();
00239     _anims.push_back(record);
00240   }
00241 }
 All Classes Functions Variables Enumerations