Panda3D

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