Panda3D
recorderController.I
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 recorderController.I
10  * @author drose
11  * @date 2004-01-24
12  */
13 
14 /**
15  * Returns the time (and date) at which the current session was originally
16  * recorded (or, in recording mode, the time at which the current session
17  * began).
18  */
19 INLINE time_t RecorderController::
20 get_start_time() const {
21  return _header._start_time;
22 }
23 
24 /**
25  * Indicates an arbitrary number to be recorded in the session file as a
26  * random seed, should the application wish to take advantage of it. This
27  * must be set before begin_record() is called.
28  */
29 INLINE void RecorderController::
30 set_random_seed(int random_seed) {
31  _header._random_seed = random_seed;
32 }
33 
34 /**
35  * Returns the random seed that was set by a previous call to
36  * set_random_seed(), or the number read from the session file after
37  * begin_playback() has been called.
38  */
39 INLINE int RecorderController::
40 get_random_seed() const {
41  return _header._random_seed;
42 }
43 
44 /**
45  * Returns true if the controller has been opened for output, false otherwise.
46  */
47 INLINE bool RecorderController::
48 is_recording() const {
49  return (_writer != nullptr);
50 }
51 
52 /**
53  * Returns true if the controller has been opened for input, false otherwise.
54  */
55 INLINE bool RecorderController::
56 is_playing() const {
57  return (_reader != nullptr);
58 }
59 
60 /**
61  * Returns true if the controller has been opened for either input or output,
62  * false otherwise.
63  */
64 INLINE bool RecorderController::
65 is_open() const {
66  return is_recording() || is_playing();
67 }
68 
69 /**
70  * Returns the filename that was passed to the most recent call to
71  * begin_record() or begin_playback().
72  */
73 INLINE const Filename &RecorderController::
74 get_filename() const {
75  return _filename;
76 }
77 
78 /**
79  * Returns true if the controller has been opened for input or output output
80  * and there is an error on the stream, or false if the controller is closed
81  * or if there is no problem.
82  */
83 INLINE bool RecorderController::
85  return _dout.is_error() || _din.is_error();
86 }
87 
88 /**
89  * Returns the delta offset between the actual frame time and the frame time
90  * written to the log. This is essentially the time at which the recording
91  * (or playback) started.
92  */
93 INLINE double RecorderController::
95  return _clock_offset;
96 }
97 
98 /**
99  * Returns the delta offset between the actual frame count and the frame count
100  * written to the log. This is essentially the frame number at which the
101  * recording (or playback) started.
102  */
103 INLINE int RecorderController::
105  return _frame_offset;
106 }
107 
108 
109 /**
110  * Adds the named recorder to the set of recorders that are in use.
111  *
112  * If the controller is in recording mode, the named recorder will begin
113  * recording its status to the session file. If the controller is in playback
114  * mode and the name and type matches a recorder in the session file, the
115  * recorder will begin receiving data.
116  */
117 INLINE void RecorderController::
118 add_recorder(const std::string &name, RecorderBase *recorder) {
119  _user_table->add_recorder(name, recorder);
120  _user_table_modified = true;
121 
122  // We can only add the state flag immediately if we are in recording mode.
123  // In playback mode, we're not sure yet whether the new recorder state will
124  // actually be playing (we won't know until we merge the tables in
125  // play_frame()).
126  if (is_recording()) {
127  recorder->_flags |= RecorderBase::F_recording;
128  }
129 }
130 
131 /**
132  * Returns true if the named recorder has been added to the table by a
133  * previous call to add_recorder(), false otherwise.
134  *
135  * If the controller is in playback mode, this will also return false for a
136  * recorder that was found in the session file but was never explicitly added
137  * via add_recorder(); see get_recorder().
138  */
139 INLINE bool RecorderController::
140 has_recorder(const std::string &name) const {
141  return (_user_table->get_recorder(name) != nullptr);
142 }
143 
144 /**
145  * Returns the recorder with the indicated name, or NULL if there is no such
146  * recorder.
147  *
148  * If the controller is in playback mode, this may return the recorder
149  * matching the indicated name as read from the session file, even if it was
150  * never added to the table by the user. In this case, has_recorder() may
151  * return false, but get_recorder() will return a non-NULL value.
152  */
154 get_recorder(const std::string &name) const {
155  RecorderBase *recorder = _user_table->get_recorder(name);
156  if (is_playing() && recorder == nullptr) {
157  recorder = _active_table->get_recorder(name);
158  }
159  return recorder;
160 }
161 
162 /**
163  * Removes the named recorder from the table. Returns true if successful,
164  * false if there was no such recorder.
165  *
166  * If the controller is in recording mode, the named recorder will stop
167  * recording. If the controller is in playback mode, the named recorder will
168  * disassociate itself from the session file (but if the session file still
169  * has data for this name, a default recorder will take its place to decode
170  * the data from the session file).
171  */
172 INLINE bool RecorderController::
173 remove_recorder(const std::string &name) {
174  // If we are playing or recording, immediately remove the state flag from
175  // the recorder. (When we are playing, the state flag will get removed
176  // automatically at the next call to play_frame(), but we might as well be
177  // aggressive and remove it now. When we are recording, we have to remove
178  // it now.)
179  if (is_recording() || is_playing()) {
180  RecorderBase *recorder = _user_table->get_recorder(name);
181  if (recorder != nullptr) {
182  recorder->_flags &= ~(RecorderBase::F_recording | RecorderBase::F_playing);
183  }
184  }
185  _user_table_modified = true;
186  return _user_table->remove_recorder(name);
187 }
188 
189 /**
190  * Sets the frame_tie flag.
191  *
192  * When this is true, sessions are played back frame-for-frame, based on the
193  * frame count of the recorded session. This gives the most accurate
194  * playback, but the playback rate will vary according to the frame rate of
195  * the playback machine.
196  *
197  * When this is false, sessions are played back at real time, based on the
198  * clock of the recorded session. This may introduce playback discrepencies
199  * if the frames do not fall at exactly the same times as they did in the
200  * original.
201  */
202 INLINE void RecorderController::
203 set_frame_tie(bool frame_tie) {
204  _frame_tie = frame_tie;
205 }
206 
207 /**
208  * See set_frame_tie().
209  */
210 INLINE bool RecorderController::
211 get_frame_tie() const {
212  return _frame_tie;
213 }
214 
215 /**
216  * Returns the global RecorderFactory for generating TypedWritable objects
217  */
220  if (_factory == nullptr) {
221  create_factory();
222  }
223  return _factory;
224 }
225 
226 /**
227  * Creates a new RecorderFactory for generating TypedWritable objects
228  */
229 INLINE void RecorderController::
230 create_factory() {
231  _factory = new RecorderFactory;
232 }
bool has_recorder(const std::string &name) const
Returns true if the named recorder has been added to the table by a previous call to add_recorder(),...
bool is_error()
Returns true if the controller has been opened for input or output output and there is an error on th...
A Factory can be used to create an instance of a particular subclass of some general base class.
Definition: factory.h:34
bool remove_recorder(const std::string &name)
Removes the named recorder from the table.
Definition: recorderTable.I:80
virtual bool is_error()
Returns true if the file has reached an error condition.
void set_frame_tie(bool frame_tie)
Sets the frame_tie flag.
static RecorderFactory * get_factory()
Returns the global RecorderFactory for generating TypedWritable objects.
void set_random_seed(int random_seed)
Indicates an arbitrary number to be recorded in the session file as a random seed,...
bool is_recording() const
Returns true if the controller has been opened for output, false otherwise.
bool is_open() const
Returns true if the controller has been opened for either input or output, false otherwise.
const Filename & get_filename() const
Returns the filename that was passed to the most recent call to begin_record() or begin_playback().
virtual bool is_error()
Returns true if the file has reached an error condition.
int get_random_seed() const
Returns the random seed that was set by a previous call to set_random_seed(), or the number read from...
int get_frame_offset() const
Returns the delta offset between the actual frame count and the frame count written to the log.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
This is the base class to a number of objects that record particular kinds of user input (like a Mous...
Definition: recorderBase.h:46
void add_recorder(const std::string &name, RecorderBase *recorder)
Adds the named recorder to the set of recorders.
Definition: recorderTable.I:48
RecorderBase * get_recorder(const std::string &name) const
Returns the recorder with the indicated name, or NULL if there is no such recorder.
Definition: recorderTable.I:67
bool get_frame_tie() const
See set_frame_tie().
time_t get_start_time() const
Returns the time (and date) at which the current session was originally recorded (or,...
RecorderBase * get_recorder(const std::string &name) const
Returns the recorder with the indicated name, or NULL if there is no such recorder.
bool remove_recorder(const std::string &name)
Removes the named recorder from the table.
double get_clock_offset() const
Returns the delta offset between the actual frame time and the frame time written to the log.
bool is_playing() const
Returns true if the controller has been opened for input, false otherwise.
void add_recorder(const std::string &name, RecorderBase *recorder)
Adds the named recorder to the set of recorders that are in use.