MoviePlayer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <cstdint>
  17. #include <string>
  18. #include <vector>
  19. #include <OgreHardwareVertexBuffer.h>
  20. #include <OgreMaterial.h>
  21. #include <OgreRenderOperation.h>
  22. #include <OgreRenderQueueListener.h>
  23. #include <OgreRenderSystem.h>
  24. #include <OgreSceneManager.h>
  25. #include <OgreTexture.h>
  26. #include "MovieCameraFrame.h"
  27. #include "MovieState.h"
  28. #include "ScriptManager.h"
  29. /**
  30. * Plays the movies that the installer converted: a folder of JPEG frames and an ogg file with the
  31. * sound, in data/video/NAME/.
  32. *
  33. * The movie is drawn over the field and below the interface and the screen fades.
  34. */
  35. class MoviePlayer : public Ogre::RenderQueueListener{
  36. public:
  37. /**
  38. * Constructor.
  39. */
  40. MoviePlayer();
  41. /**
  42. * Destructor.
  43. */
  44. virtual ~MoviePlayer();
  45. /**
  46. * Run each frame. Moves the movie forward.
  47. *
  48. * @param[in] delta Seconds since the last frame.
  49. */
  50. void Update(const float delta);
  51. /**
  52. * Chooses the movie to play next, like the PMVIE opcode.
  53. *
  54. * @param[in] name Name of the movie, without extension.
  55. * @param[in] field_scale Scale factor of the field, to place the models over the movie.
  56. */
  57. void ScriptSet(const char* name, const float field_scale);
  58. /**
  59. * Starts the chosen movie, like the MOVIE opcode. The script that starts it waits until
  60. * the movie ends. The other scripts go on while it's played.
  61. *
  62. * @return 1 if the script doesn't have to wait, and -1 if it does.
  63. */
  64. int ScriptPlay();
  65. /**
  66. * Stops the movie.
  67. */
  68. void ScriptStop();
  69. /**
  70. * Retrieves the frame being shown, like the MVIEF opcode.
  71. *
  72. * @return The frame. If the chosen movie isn't installed, a number high enough to be
  73. * after any frame the scripts wait for.
  74. */
  75. int ScriptGetFrame() const;
  76. /**
  77. * Makes the next movies the background of the field (MVCAM): the models are drawn over them
  78. * even if the movie has no camera data (the elevator of junair).
  79. *
  80. * @param[in] use True while the models go over the movies.
  81. */
  82. void ScriptUseMovieCamera(const bool use);
  83. /**
  84. * @return True while a movie is being played.
  85. */
  86. bool ScriptIsPlaying() const;
  87. /**
  88. * Checks if the movie is drawn behind the field, which is drawn over it with the camera of
  89. * the movie. If not, the movie covers the field.
  90. *
  91. * @return True if a movie with a camera is being played.
  92. */
  93. bool IsDrawnBehind() const;
  94. /**
  95. * Checks if the movie covers the whole screen, over the field.
  96. *
  97. * The movies with no camera are drawn over everything of the field, and the fades of the
  98. * field (the scenes black the screen before a movie starts) must not hide them.
  99. *
  100. * @return True if a movie is playing over the field.
  101. */
  102. bool IsDrawnOver() const;
  103. /**
  104. * Draws the movie. If it has a camera it's drawn behind the models, and the models are
  105. * only drawn in the frames that have a camera. If not, it's drawn over the field and below
  106. * the interface.
  107. */
  108. void renderQueueStarted(
  109. Ogre::uint8 queue_group_id, const Ogre::String& invocation, bool& skip_this_invocation
  110. ) override;
  111. private:
  112. /**
  113. * Reads the information of the chosen movie.
  114. *
  115. * @param[out] frame_count Number of frames.
  116. * @param[out] frames_per_second Frames per second.
  117. * @param[out] camera_frame First frame with a camera for the models, or -1.
  118. * @param[out] release_frame Frame at which the script that plays the movie goes on, or -1
  119. * for the end.
  120. * @return True if the movie is installed.
  121. */
  122. bool ReadInfo(
  123. int& frame_count, float& frames_per_second, int& camera_frame, int& release_frame
  124. ) const;
  125. /**
  126. * Content of the camera file of the movie. Empty if it hasn't got one.
  127. */
  128. std::vector<uint8_t> camera_data_;
  129. /**
  130. * Scale factor of the field.
  131. */
  132. float field_scale_ = 1.0f;
  133. /**
  134. * Whether the current frame of the movie has a camera.
  135. */
  136. bool camera_valid_ = false;
  137. /**
  138. * The script (MVCAM) wants the models over the movie.
  139. */
  140. bool movie_camera_ = false;
  141. /**
  142. * Whether the movie camera is in use.
  143. */
  144. bool camera_in_use_ = false;
  145. /**
  146. * Frame at which the script that played the movie goes on, or -1 for the end of the movie.
  147. */
  148. int release_frame_ = -1;
  149. /**
  150. * Places the camera of the field in the one of the current frame of the movie, or puts it
  151. * back if the movie has ended.
  152. */
  153. void UpdateCamera();
  154. /**
  155. * Hides or shows everything the viewport draws as an object of the scene, the models.
  156. *
  157. * @param[in] hidden True to hide them.
  158. */
  159. void SetModelsHidden(const bool hidden);
  160. /**
  161. * Whether the models are hidden because the frame has no camera.
  162. */
  163. bool models_hidden_ = false;
  164. /**
  165. * Draws the quad with the current frame.
  166. */
  167. void DrawQuad();
  168. /**
  169. * Loads a frame in the texture.
  170. *
  171. * @param[in] frame The frame to load.
  172. */
  173. void LoadFrame(const int frame);
  174. /**
  175. * Writes the corners of the screen quad.
  176. */
  177. void WriteQuad();
  178. /**
  179. * The scripts that wait for the end of the movie.
  180. */
  181. std::vector<ScriptId> waiting_;
  182. /**
  183. * Resumes the scripts that wait for the end of the movie.
  184. */
  185. void ReleaseWaiting();
  186. /**
  187. * The clock of the movie.
  188. */
  189. MovieState state_;
  190. /**
  191. * Name of the chosen movie.
  192. */
  193. std::string name_;
  194. /**
  195. * Frame in the texture, or -1.
  196. */
  197. int loaded_frame_ = -1;
  198. /**
  199. * Scene manager.
  200. */
  201. Ogre::SceneManager* scene_manager_;
  202. /**
  203. * Render system.
  204. */
  205. Ogre::RenderSystem* render_system_;
  206. /**
  207. * Texture the frames are copied to.
  208. */
  209. Ogre::TexturePtr texture_;
  210. /**
  211. * Material of the quad.
  212. */
  213. Ogre::MaterialPtr material_;
  214. /**
  215. * Render operation of the quad.
  216. */
  217. Ogre::RenderOperation render_op_;
  218. /**
  219. * Vertex buffer of the quad.
  220. */
  221. Ogre::HardwareVertexBufferSharedPtr vertex_buffer_;
  222. };