Panda3D
 All Classes Functions Variables Enumerations
animPreloadTable.cxx
1 // Filename: animPreloadTable.cxx
2 // Created by: drose (05Aug08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "animPreloadTable.h"
16 
17 #include "indent.h"
18 #include "datagram.h"
19 #include "datagramIterator.h"
20 #include "bamReader.h"
21 #include "bamWriter.h"
22 
23 TypeHandle AnimPreloadTable::_type_handle;
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: AnimPreloadTable::make_cow_copy
27 // Access: Protected, Virtual
28 // Description: Required to implement CopyOnWriteObject.
29 ////////////////////////////////////////////////////////////////////
31 make_cow_copy() {
32  return new AnimPreloadTable(*this);
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: AnimPreloadTable::Constructor
37 // Access: Published
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 AnimPreloadTable::
41 AnimPreloadTable() {
42  _needs_sort = false;
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: AnimPreloadTable::Destructor
47 // Access: Published, Virtual
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 AnimPreloadTable::
51 ~AnimPreloadTable() {
52 }
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function: AnimPreloadTable::get_num_anims
56 // Access: Published
57 // Description: Returns the number of animation records in the table.
58 ////////////////////////////////////////////////////////////////////
60 get_num_anims() const {
61  return (int)_anims.size();
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: AnimPreloadTable::find_anim
66 // Access: Published
67 // Description: Returns the index number in the table of the
68 // animation record with the indicated name, or -1 if
69 // the name is not present. By convention, the basename
70 // is the filename of the egg or bam file, without the
71 // directory part and without the extension. That is,
72 // it is Filename::get_basename_wo_extension().
73 ////////////////////////////////////////////////////////////////////
75 find_anim(const string &basename) const {
76  consider_sort();
77  AnimRecord record;
78  record._basename = basename;
79  Anims::const_iterator ai = _anims.find(record);
80  if (ai != _anims.end()) {
81  return int(ai - _anims.begin());
82  }
83  return -1;
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: AnimPreloadTable::clear_anims
88 // Access: Published
89 // Description: Removes all animation records from the table.
90 ////////////////////////////////////////////////////////////////////
93  _anims.clear();
94  _needs_sort = false;
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: AnimPreloadTable::remove_anim
99 // Access: Published
100 // Description: Removes the nth animation records from the table.
101 // This renumbers indexes for following animations.
102 ////////////////////////////////////////////////////////////////////
104 remove_anim(int n) {
105  nassertv(n >= 0 && n < (int)_anims.size());
106  _anims.erase(_anims.begin() + n);
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: AnimPreloadTable::add_anim
111 // Access: Published
112 // Description: Adds a new animation record to the table. If there
113 // is already a record of this name, no operation is
114 // performed (the original record is unchanged). See
115 // find_anim(). This will invalidate existing index
116 // numbers.
117 ////////////////////////////////////////////////////////////////////
119 add_anim(const string &basename, PN_stdfloat base_frame_rate, int num_frames) {
120  AnimRecord record;
121  record._basename = basename;
122  record._base_frame_rate = base_frame_rate;
123  record._num_frames = num_frames;
124 
125  _anims.push_back(record);
126  _needs_sort = true;
127 }
128 
129 ////////////////////////////////////////////////////////////////////
130 // Function: AnimPreloadTable::add_anims_from
131 // Access: Published
132 // Description: Copies the animation records from the other table
133 // into this one. If a given record name exists in both
134 // tables, the record in this one supercedes.
135 ////////////////////////////////////////////////////////////////////
138  _anims.reserve(_anims.size() + other->_anims.size());
139  Anims::const_iterator ai;
140  for (ai = other->_anims.begin(); ai != other->_anims.end(); ++ai) {
141  _anims.push_back(*ai);
142  }
143  _needs_sort = true;
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: AnimPreloadTable::output
148 // Access: Published
149 // Description:
150 ////////////////////////////////////////////////////////////////////
151 void AnimPreloadTable::
152 output(ostream &out) const {
153  consider_sort();
154  out << "AnimPreloadTable, " << _anims.size() << " animation records.";
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: AnimPreloadTable::write
159 // Access: Published
160 // Description:
161 ////////////////////////////////////////////////////////////////////
162 void AnimPreloadTable::
163 write(ostream &out, int indent_level) const {
164  consider_sort();
165  indent(out, indent_level)
166  << "AnimPreloadTable, " << _anims.size() << " animation records:\n";
167  Anims::const_iterator ai;
168  for (ai = _anims.begin(); ai != _anims.end(); ++ai) {
169  const AnimRecord &record = (*ai);
170  indent(out, indent_level + 2)
171  << record._basename << ": " << record._num_frames << " frames at "
172  << record._base_frame_rate << " fps\n";
173  }
174 }
175 
176 ////////////////////////////////////////////////////////////////////
177 // Function: AnimPreloadTable::register_with_read_factory
178 // Access: Public, Static
179 // Description: Factory method to generate an AnimPreloadTable object
180 ////////////////////////////////////////////////////////////////////
184 }
185 
186 ////////////////////////////////////////////////////////////////////
187 // Function: AnimPreloadTable::write_datagram
188 // Access: Public
189 // Description: Function to write the important information in
190 // the particular object to a Datagram
191 ////////////////////////////////////////////////////////////////////
194  consider_sort();
195 
196  dg.add_uint16(_anims.size());
197  Anims::const_iterator ai;
198  for (ai = _anims.begin(); ai != _anims.end(); ++ai) {
199  const AnimRecord &record = (*ai);
200  dg.add_string(record._basename);
201  dg.add_stdfloat(record._base_frame_rate);
202  dg.add_int32(record._num_frames);
203  }
204 }
205 
206 ////////////////////////////////////////////////////////////////////
207 // Function: AnimPreloadTable::make_from_bam
208 // Access: Protected
209 // Description: Factory method to generate an AnimPreloadTable object
210 ////////////////////////////////////////////////////////////////////
212 make_from_bam(const FactoryParams &params) {
214  DatagramIterator scan;
215  BamReader *manager;
216 
217  parse_params(params, scan, manager);
218  me->fillin(scan, manager);
219  return me;
220 }
221 
222 ////////////////////////////////////////////////////////////////////
223 // Function: AnimPreloadTable::fillin
224 // Access: Protected
225 // Description: Function that reads out of the datagram (or asks
226 // manager to read) all of the data that is needed to
227 // re-create this object and stores it in the appropiate
228 // place
229 ////////////////////////////////////////////////////////////////////
230 void AnimPreloadTable::
231 fillin(DatagramIterator &scan, BamReader *manager) {
232  int num_anims = scan.get_uint16();
233  _anims.reserve(num_anims);
234  for (int i = 0; i < num_anims; ++i) {
235  AnimRecord record;
236  record._basename = scan.get_string();
237  record._base_frame_rate = scan.get_stdfloat();
238  record._num_frames = scan.get_int32();
239  _anims.push_back(record);
240  }
241 }
static TypedWritable * make_from_bam(const FactoryParams &params)
Factory method to generate an AnimPreloadTable object.
This table records data about a list of animations for a particular model, such as number of frames a...
void add_string(const string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:351
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
void clear()
Removes all elements from the ordered vector.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
iterator_0 begin()
Returns the iterator that marks the first element in the ordered vector.
int get_num_anims() const
Returns the number of animation records in the table.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
iterator_0 end()
Returns the iterator that marks the end of the ordered vector.
PN_int32 get_int32()
Extracts a signed 32-bit integer.
void reserve(size_type_0 n)
Informs the vector of a planned change in size; ensures that the capacity of the vector is greater th...
string get_string()
Extracts a variable-length string.
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition: datagram.I:240
void add_anim(const string &basename, PN_stdfloat base_frame_rate, int num_frames)
Adds a new animation record to the table.
void add_anims_from(const AnimPreloadTable *other)
Copies the animation records from the other table into this one.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Function to write the important information in the particular object to a Datagram.
void push_back(const value_type_0 &key)
Adds the new element to the end of the vector without regard for proper sorting.
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
This base class provides basic reference counting, but also can be used with a CopyOnWritePointer to ...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
void remove_anim(int n)
Removes the nth animation records from the table.
size_type_0 size() const
Returns the number of elements in the ordered vector.
void clear_anims()
Removes all animation records from the table.
void add_int32(PN_int32 value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:159
A class to retrieve the individual data elements previously stored in a Datagram. ...
int find_anim(const string &basename) const
Returns the index number in the table of the animation record with the indicated name, or -1 if the name is not present.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
static void register_with_read_factory()
Factory method to generate an AnimPreloadTable object.