Clean up some header files
[lsnes.git] / movie.hpp
blob61844b279fc50f93a149794cd52e338f44c2791b
1 #ifndef _movie__hpp__included__
2 #define _movie__hpp__included__
4 #include <string>
5 #include <cstdint>
6 #include <stdexcept>
7 #include "controllerdata.hpp"
9 /**
10 * \brief Movie being played back or recorded
12 class movie
14 public:
15 /**
16 * \brief Construct new empty movie.
17 * \throws std::bad_alloc Not enough memory.
19 movie() throw(std::bad_alloc);
21 /**
22 * \brief Is the movie in readonly mode?
24 * \return True if in read-only mode, false if in read-write mode.
26 bool readonly_mode() throw();
28 /**
29 * \brief Switch between modes
31 * Switches movie to read-only or read-write mode. If switching to read-write mode, the movie is truncated.
33 * \param enable If true, switch to read-only mode, else to read-write mode.
34 * \throws std::bad_alloc Not enough memory.
36 void readonly_mode(bool enable) throw(std::bad_alloc);
38 /**
39 * \brief Get movie rerecord count
41 * Returns the movie rerecord count (this is not the same thing as global rerecord count).
43 * \return The movie rerecord count
44 * \throws std::bad_alloc Not enough memory.
46 std::string rerecord_count() throw(std::bad_alloc);
48 /**
49 * \brief Set movie rerecord count
51 * Sets the movie rerecord count (this is not the same thing as global rerecord count).
53 * \param count The new rerecord count
54 * \throws std::bad_alloc Not enough memory.
56 void rerecord_count(const std::string& count) throw(std::bad_alloc);
58 /**
59 * \brief Read project ID
60 * \return The project ID
61 * \throws std::bad_alloc Not enough memory.
63 std::string project_id() throw(std::bad_alloc);
65 /**
66 * \brief Set project ID
67 * \param id New project ID.
68 * \throws std::bad_alloc Not enough memory.
70 void project_id(const std::string& id) throw(std::bad_alloc);
72 /**
73 * \brief Get number of frames in movie
74 * \return The number of frames.
76 uint64_t get_frame_count() throw();
78 /**
79 * \brief Get number of currnet frame in movie
81 * The first frame in movie is 1. 0 is "before first frame" value.
83 * \return The number of frame
85 uint64_t get_current_frame() throw();
87 /**
88 * \brief Get number of lag frames so far
89 * \return The number of lag frames.
91 uint64_t get_lag_frames() throw();
93 /**
94 * \brief Advance to next frame.
96 * This function advances to next frame in movie, discarding subframes not used. If the frame is lag frame, it is
97 * counted as lag frame and subframe entry for it is made (if in readwrite mode).
99 * \throw std::bad_alloc Not enough memory.
101 void next_frame() throw(std::bad_alloc);
104 * \brief Get data ready flag for control index
106 * Reads the data ready flag. On new frame, all data ready flags are unset. On reading control, its data ready
107 * flag is unset.
109 * \param controlindex The index of control to read it for.
110 * \return The read value.
111 * \throws std::logic_error Invalid control index.
113 bool get_DRDY(unsigned controlindex) throw(std::logic_error);
116 * \brief Get data ready flag for given (port,controller,index) index
118 * Reads the data ready flag. On new frame, all data ready flags are unset. On reading control, its data ready
119 * flag is unset.
121 * This differs from get_DRDY(unsigned) in that this takes (port, controller,index) tuple.
123 * \param port The port controller is connected to (0 or 1)
124 * \param controller The controller number within port (0 to 3)
125 * \param index The index of control in controller (0 to 11)
126 * \return The read value.
127 * \throws std::logic_error Invalid control index.
129 bool get_DRDY(unsigned port, unsigned controller, unsigned index) throw(std::logic_error);
132 * \brief Set all data ready flags
134 void set_all_DRDY() throw();
137 * \brief Poll next value for given control index
139 * Poll a control. Note that index 0 (sync flag) always reads as released.
141 * \param controlindex The index
142 * \return The read value
143 * \throws std::bad_alloc Not enough memory.
144 * \throws std::logic_error Invalid control index or before movie start.
146 short next_input(unsigned controlindex) throw(std::bad_alloc, std::logic_error);
149 * \brief Poll next value for given (port, controller, index) index
151 * Poll a control.
153 * \param port The port controller is connected to (0 or 1)
154 * \param controller The controller number within port (0 to 3)
155 * \param index The index of control in controller (0 to 11)
156 * \return The read value
157 * \throws std::bad_alloc Not enough memory.
158 * \throws std::logic_error Invalid port, controller or index or before movie start.
160 short next_input(unsigned port, unsigned controller, unsigned index) throw(std::bad_alloc, std::logic_error);
163 * \brief Set values of current controls
165 * Set current control values. These are read in readwrite mode.
167 * \param controls The new controls.
169 void set_controls(controls_t controls) throw();
172 * \brief Get values of current controls
174 * Get current control values in effect.
176 * \return Controls
178 controls_t get_controls() throw();
181 * \brief Load a movie.
183 * Loads a movie plus some other parameters. The playback pointer is positioned to start of movie and readonly
184 * mode is enabled.
186 * \param rerecs Movie rerecord count.
187 * \param project_id Project ID of movie.
188 * \param input The input track.
189 * \throws std::bad_alloc Not enough memory.
190 * \throws std::runtime_error Bad movie data.
192 void load(const std::string& rerecs, const std::string& project_id, const std::vector<controls_t>& input)
193 throw(std::bad_alloc, std::runtime_error);
196 * \brief Save a movie.
198 * Saves the movie data.
200 * \return The movie data.
201 * \throws std::bad_alloc Not enough memory.
203 std::vector<controls_t> save() throw(std::bad_alloc);
206 * \brief Save the state of movie code
208 * This method serializes the state of movie code.
210 * \return The serialized state.
211 * \throws std::bad_alloc Not enough memory.
213 std::vector<uint8_t> save_state() throw(std::bad_alloc);
216 * \brief Restore the state of movie code
218 * Given previous serialized state from this movie, restore the state.
220 * \param state The state to restore.
221 * \param ro If true, restore in readonly mode, otherwise in readwrite mode.
222 * \throw std::bad_alloc Not enough memory.
223 * \throw std::runtime_error State is not from this movie or states is corrupt.
225 size_t restore_state(const std::vector<uint8_t>& state, bool ro) throw(std::bad_alloc, std::runtime_error);
228 * \brief Get reset status for current frame.
230 * \return -1 if current frame doesn't have a reset. Otherwise number of cycles to wait for delayed reset (0 is
231 * immediate reset).
233 long get_reset_status() throw();
236 * \brief Commit a reset.
238 void commit_reset(long delay) throw(std::bad_alloc);
241 * \brief Get how manyth poll in the frame next poll would be.
243 * \return Poll number.
245 unsigned next_poll_number();
247 uint64_t frame_subframes(uint64_t frame);
248 controls_t read_subframe(uint64_t frame, uint64_t subframe);
249 private:
250 //TRUE if readonly mode is active.
251 bool readonly;
252 //Movie (not global!) rerecord count.
253 std::string rerecords;
254 //Project ID.
255 std::string _project_id;
256 //The actual controller data.
257 std::vector<controls_t> movie_data;
258 //Current frame + 1 (0 before next_frame() has been called.
259 uint64_t current_frame;
260 //First subframe in current frame (movie_data.size() if no subframes have been stored).
261 uint64_t current_frame_first_subframe;
262 //How many times has each control been polled (bit 31 is data ready bit)?
263 uint32_t pollcounters[TOTAL_CONTROLS];
264 //Current state of buttons.
265 controls_t current_controls;
266 //Number of known lag frames.
267 uint64_t lag_frames;
268 //Number of frames in movie.
269 uint64_t frames_in_movie;
270 //Cached subframes.
271 void clear_caches();
272 uint64_t cached_frame;
273 uint64_t cached_subframe;
274 //Count present subframes in frame starting from first_subframe (returns 0 if out of movie).
275 uint32_t count_changes(uint64_t first_subframe);
279 * \brief Class encapsulating bridge logic between bsnes interface and movie code.
281 class movie_logic
283 public:
285 * \brief Create new bridge.
287 * \param m The movie to manipulate.
289 movie_logic(movie& m) throw();
292 * \brief Destructor.
294 virtual ~movie_logic() throw();
297 * \brief Get the movie instance associated.
299 movie& get_movie() throw();
302 * \brief Notify about new frame starting.
304 * \return Reset status for the new frame.
306 long new_frame_starting(bool dont_poll) throw(std::bad_alloc, std::runtime_error);
309 * \brief Poll for input.
311 * \return Value for polled input.
313 short input_poll(bool port, unsigned dev, unsigned id) throw(std::bad_alloc, std::runtime_error);
316 * \brief Called when movie code needs new controls snapshot.
318 * \param subframe True if this is for subframe update, false if for frame update.
320 virtual controls_t update_controls(bool subframe) throw(std::bad_alloc, std::runtime_error) = 0;
321 private:
322 movie& mov;
327 * \brief Global rerecord count.
329 extern std::string global_rerecord_count;
331 #endif