MoviePlayer.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. * @return True while a movie is being played.
  78. */
  79. bool ScriptIsPlaying() const;
  80. /**
  81. * Checks if the movie is drawn behind the field, which is drawn over it with the camera of
  82. * the movie. If not, the movie covers the field.
  83. *
  84. * @return True if a movie with a camera is being played.
  85. */
  86. bool IsDrawnBehind() const;
  87. /**
  88. * Draws the movie. If it has a camera it's drawn behind the models, and the models are
  89. * only drawn in the frames that have a camera. If not, it's drawn over the field and below
  90. * the interface.
  91. */
  92. void renderQueueStarted(
  93. Ogre::uint8 queue_group_id, const Ogre::String& invocation, bool& skip_this_invocation
  94. ) override;
  95. private:
  96. /**
  97. * Reads the information of the chosen movie.
  98. *
  99. * @param[out] frame_count Number of frames.
  100. * @param[out] frames_per_second Frames per second.
  101. * @param[out] camera_frame First frame with a camera for the models, or -1.
  102. * @param[out] release_frame Frame at which the script that plays the movie goes on, or -1
  103. * for the end.
  104. * @return True if the movie is installed.
  105. */
  106. bool ReadInfo(
  107. int& frame_count, float& frames_per_second, int& camera_frame, int& release_frame
  108. ) const;
  109. /**
  110. * Content of the camera file of the movie. Empty if it hasn't got one.
  111. */
  112. std::vector<uint8_t> camera_data_;
  113. /**
  114. * Scale factor of the field.
  115. */
  116. float field_scale_ = 1.0f;
  117. /**
  118. * Whether the current frame of the movie has a camera.
  119. */
  120. bool camera_valid_ = false;
  121. /**
  122. * Whether the movie camera is in use.
  123. */
  124. bool camera_in_use_ = false;
  125. /**
  126. * Frame at which the script that played the movie goes on, or -1 for the end of the movie.
  127. */
  128. int release_frame_ = -1;
  129. /**
  130. * Places the camera of the field in the one of the current frame of the movie, or puts it
  131. * back if the movie has ended.
  132. */
  133. void UpdateCamera();
  134. /**
  135. * Hides or shows everything the viewport draws as an object of the scene, the models.
  136. *
  137. * @param[in] hidden True to hide them.
  138. */
  139. void SetModelsHidden(const bool hidden);
  140. /**
  141. * Whether the models are hidden because the frame has no camera.
  142. */
  143. bool models_hidden_ = false;
  144. /**
  145. * Draws the quad with the current frame.
  146. */
  147. void DrawQuad();
  148. /**
  149. * Loads a frame in the texture.
  150. *
  151. * @param[in] frame The frame to load.
  152. */
  153. void LoadFrame(const int frame);
  154. /**
  155. * Writes the corners of the screen quad.
  156. */
  157. void WriteQuad();
  158. /**
  159. * The scripts that wait for the end of the movie.
  160. */
  161. std::vector<ScriptId> waiting_;
  162. /**
  163. * Resumes the scripts that wait for the end of the movie.
  164. */
  165. void ReleaseWaiting();
  166. /**
  167. * The clock of the movie.
  168. */
  169. MovieState state_;
  170. /**
  171. * Name of the chosen movie.
  172. */
  173. std::string name_;
  174. /**
  175. * Frame in the texture, or -1.
  176. */
  177. int loaded_frame_ = -1;
  178. /**
  179. * Scene manager.
  180. */
  181. Ogre::SceneManager* scene_manager_;
  182. /**
  183. * Render system.
  184. */
  185. Ogre::RenderSystem* render_system_;
  186. /**
  187. * Texture the frames are copied to.
  188. */
  189. Ogre::TexturePtr texture_;
  190. /**
  191. * Material of the quad.
  192. */
  193. Ogre::MaterialPtr material_;
  194. /**
  195. * Render operation of the quad.
  196. */
  197. Ogre::RenderOperation render_op_;
  198. /**
  199. * Vertex buffer of the quad.
  200. */
  201. Ogre::HardwareVertexBufferSharedPtr vertex_buffer_;
  202. };