Panda3D
recorderFrame.cxx
1 // Filename: recorderFrame.cxx
2 // Created by: drose (28Jan04)
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 "recorderFrame.h"
16 #include "recorderTable.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "config_recorder.h"
20 
21 TypeHandle RecorderFrame::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: RecorderFrame::play_frame
25 // Access: Public
26 // Description: Once the raw data has been read in from the session
27 // file, and the table has been decoded, decode the raw
28 // data and call play_frame on each recorder.
29 ////////////////////////////////////////////////////////////////////
30 void RecorderFrame::
31 play_frame(BamReader *manager) {
32  DatagramIterator scan(_data, _data_pos);
33  _table->play_frame(scan, manager);
34 
35  // We expect to use up all of the data in the datagram.
36  nassertv(scan.get_remaining_size() == 0);
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: RecorderFrame::register_with_read_factory
41 // Access: Public, Static
42 // Description: Tells the BamReader how to create objects of type
43 // Lens.
44 ////////////////////////////////////////////////////////////////////
45 void RecorderFrame::
47  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: RecorderFrame::write_datagram
52 // Access: Public, Virtual
53 // Description: Writes the contents of this object to the datagram
54 // for shipping out to a Bam file.
55 ////////////////////////////////////////////////////////////////////
56 void RecorderFrame::
58  TypedWritable::write_datagram(manager, dg);
59  dg.add_float64(_timestamp);
60  dg.add_uint32(_frame);
61 
62  // Write the table out if it has changed.
63  dg.add_bool(_table_changed);
64  if (_table_changed) {
65  // As a kludge, we create a new table pointer to write out.
66  // Otherwise, the pointer won't change and it may not write out
67  // the changes. Later, we need to add a facility to the bam
68  // writer to detect when a TypedWritable has changed and should be
69  // rewritten.
70  _local_table = *_table;
71  manager->write_pointer(dg, &_local_table);
72  }
73 
74  _table->record_frame(manager, dg);
75 }
76 
77 ////////////////////////////////////////////////////////////////////
78 // Function: RecorderFrame::complete_pointers
79 // Access: Public, Virtual
80 // Description: Receives an array of pointers, one for each time
81 // manager->read_pointer() was called in fillin().
82 // Returns the number of pointers processed.
83 //
84 // This is the callback function that is made by the
85 // BamReader at some later point, after all of the
86 // required pointers have been filled in. It is
87 // necessary because there might be forward references
88 // in a bam file; when we call read_pointer() in
89 // fillin(), the object may not have been read from the
90 // file yet, so we do not have a pointer available at
91 // that time. Thus, instead of returning a pointer,
92 // read_pointer() simply reserves a later callback.
93 // This function provides that callback. The calling
94 // object is responsible for keeping track of the number
95 // of times it called read_pointer() and extracting the
96 // same number of pointers out of the supplied vector,
97 // and storing them appropriately within the object.
98 ////////////////////////////////////////////////////////////////////
101  int pi = TypedWritable::complete_pointers(p_list, manager);
102 
103  if (_table_changed) {
104  _table = DCAST(RecorderTable, p_list[pi++]);
105  }
106 
107  return pi;
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: RecorderFrame::make_from_bam
112 // Access: Protected, Static
113 // Description: This function is called by the BamReader's factory
114 // when a new object of type Lens is encountered
115 // in the Bam file. It should create the Lens
116 // and extract its information from the file.
117 ////////////////////////////////////////////////////////////////////
118 TypedWritable *RecorderFrame::
119 make_from_bam(const FactoryParams &params) {
120  RecorderFrame *frame = new RecorderFrame;
121  DatagramIterator scan;
122  BamReader *manager;
123 
124  parse_params(params, scan, manager);
125  frame->fillin(scan, manager);
126 
127  return frame;
128 }
129 
130 ////////////////////////////////////////////////////////////////////
131 // Function: RecorderFrame::fillin
132 // Access: Protected
133 // Description: This internal function is called by make_from_bam to
134 // read in all of the relevant data from the BamFile for
135 // the new RecorderFrame.
136 ////////////////////////////////////////////////////////////////////
137 void RecorderFrame::
138 fillin(DatagramIterator &scan, BamReader *manager) {
139  TypedWritable::fillin(scan, manager);
140 
141  _timestamp = scan.get_float64();
142  _frame = scan.get_uint32();
143  _table_changed = scan.get_bool();
144  _table = (RecorderTable *)NULL;
145  if (_table_changed) {
146  manager->read_pointer(scan);
147  }
148 
149  // We can't decode the data in the frame until we have (a) gotten
150  // back the table pointer, or (b) been told who our owning
151  // RecorderController is. So we'll just save the raw data for now
152  // and come back to it.
153  _data = scan.get_datagram();
154  _data_pos = scan.get_current_index();
155 }
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:228
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
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:37
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:73
PN_uint32 get_uint32()
Extracts an unsigned 32-bit integer.
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.
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_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:118
void play_frame(BamReader *manager)
Once the raw data has been read in from the session file, and the table has been decoded, decode the raw data and call play_frame on each recorder.
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:35
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()...
int get_remaining_size() const
Return the bytes left in the datagram.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin()...
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
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:213
void add_uint32(PN_uint32 value)
Adds an unsigned 32-bit integer to the datagram.
Definition: datagram.I:192
This object represents one frame of data in the recorded session file.
Definition: recorderFrame.h:35
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:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:279
void read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:658