Panda3D
recorderFrame.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 recorderFrame.cxx
10  * @author drose
11  * @date 2004-01-28
12  */
13 
14 #include "recorderFrame.h"
15 #include "recorderTable.h"
16 #include "bamReader.h"
17 #include "bamWriter.h"
18 #include "config_recorder.h"
19 
20 TypeHandle RecorderFrame::_type_handle;
21 
22 /**
23  * Once the raw data has been read in from the session file, and the table has
24  * been decoded, decode the raw data and call play_frame on each recorder.
25  */
26 void RecorderFrame::
27 play_frame(BamReader *manager) {
28  DatagramIterator scan(_data, _data_pos);
29  _table->play_frame(scan, manager);
30 
31  // We expect to use up all of the data in the datagram.
32  nassertv(scan.get_remaining_size() == 0);
33 }
34 
35 /**
36  * Tells the BamReader how to create objects of type Lens.
37  */
38 void RecorderFrame::
40  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
41 }
42 
43 /**
44  * Writes the contents of this object to the datagram for shipping out to a
45  * Bam file.
46  */
47 void RecorderFrame::
49  TypedWritable::write_datagram(manager, dg);
50  dg.add_float64(_timestamp);
51  dg.add_uint32(_frame);
52 
53  // Write the table out if it has changed.
54  dg.add_bool(_table_changed);
55  if (_table_changed) {
56  // As a kludge, we create a new table pointer to write out. Otherwise,
57  // the pointer won't change and it may not write out the changes. Later,
58  // we need to add a facility to the bam writer to detect when a
59  // TypedWritable has changed and should be rewritten.
60  _local_table = *_table;
61  manager->write_pointer(dg, &_local_table);
62  }
63 
64  _table->record_frame(manager, dg);
65 }
66 
67 /**
68  * Receives an array of pointers, one for each time manager->read_pointer()
69  * was called in fillin(). Returns the number of pointers processed.
70  *
71  * This is the callback function that is made by the BamReader at some later
72  * point, after all of the required pointers have been filled in. It is
73  * necessary because there might be forward references in a bam file; when we
74  * call read_pointer() in fillin(), the object may not have been read from the
75  * file yet, so we do not have a pointer available at that time. Thus,
76  * instead of returning a pointer, read_pointer() simply reserves a later
77  * callback. This function provides that callback. The calling object is
78  * responsible for keeping track of the number of times it called
79  * read_pointer() and extracting the same number of pointers out of the
80  * supplied vector, and storing them appropriately within the object.
81  */
84  int pi = TypedWritable::complete_pointers(p_list, manager);
85 
86  if (_table_changed) {
87  _table = DCAST(RecorderTable, p_list[pi++]);
88  }
89 
90  return pi;
91 }
92 
93 /**
94  * This function is called by the BamReader's factory when a new object of
95  * type Lens is encountered in the Bam file. It should create the Lens and
96  * extract its information from the file.
97  */
98 TypedWritable *RecorderFrame::
99 make_from_bam(const FactoryParams &params) {
100  RecorderFrame *frame = new RecorderFrame;
101  DatagramIterator scan;
102  BamReader *manager;
103 
104  parse_params(params, scan, manager);
105  frame->fillin(scan, manager);
106 
107  return frame;
108 }
109 
110 /**
111  * This internal function is called by make_from_bam to read in all of the
112  * relevant data from the BamFile for the new RecorderFrame.
113  */
114 void RecorderFrame::
115 fillin(DatagramIterator &scan, BamReader *manager) {
116  TypedWritable::fillin(scan, manager);
117 
118  _timestamp = scan.get_float64();
119  _frame = scan.get_uint32();
120  _table_changed = scan.get_bool();
121  _table = nullptr;
122  if (_table_changed) {
123  manager->read_pointer(scan);
124  }
125 
126  // We can't decode the data in the frame until we have (a) gotten back the
127  // table pointer, or (b) been told who our owning RecorderController is. So
128  // we'll just save the raw data for now and come back to it.
129  _data = scan.get_datagram();
130  _data_pos = scan.get_current_index();
131 }
static void register_with_read_factory()
Tells the BamReader how to create objects of type Lens.
bool get_bool()
Extracts a boolean value.
void add_float64(PN_float64 value)
Adds a 64-bit floating-point number to the datagram.
Definition: datagram.I:123
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
const Datagram & get_datagram() const
Return the datagram of this iterator.
void record_frame(BamWriter *manager, Datagram &dg)
Calls record_frame on all recorders.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void play_frame(DatagramIterator &scan, BamReader *manager)
Calls play_frame on all recorders.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
size_t get_remaining_size() const
Return the bytes left in the datagram.
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
Definition: datagram.I:94
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
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
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:34
void play_frame(BamReader *manager)
Once the raw data has been read in from the session file, and the table has been decoded,...
size_t get_current_index() const
Returns the current position within the datagram of the next piece of data to extract.
This object is used by the RecorderController to write (and read) a record of the set of recorders in...
Definition: recorderTable.h:32
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
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
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PN_float64 get_float64()
Extracts a 64-bit floating-point number.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:610
This object represents one frame of data in the recorded session file.
Definition: recorderFrame.h:32
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:317