WindowVideoManager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: WindowVideoManager.cpp /////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Electronic Arts Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // created: Apr 2002
  34. //
  35. // Filename: WindowVideoManager.cpp
  36. //
  37. // author: Chris Huybregts
  38. //
  39. // purpose: Every window is setup to be able to draw a movie. The
  40. // WindowVideoManager will take care of setting up the window,
  41. // creating/destroying the buffers, and the ability to pause/stop
  42. // movies.
  43. //
  44. // To Use: Create a manager and initialize it. Make sure the manager's Update
  45. // is called every frame or how ever often it needs to be updated.
  46. // Call reset if the manager needs to be cleared.
  47. // Play a movie in a window by passing a window pointer, a movie name,
  48. // and the playtype.
  49. // If a user trys to play a two different movies on the same window,
  50. // only the last one added will play.
  51. // It's important when destroying a window, that window is removed
  52. // from the manager (Or call Reset if you know no other windows are
  53. // playing a movie).
  54. //
  55. //-----------------------------------------------------------------------------
  56. ///////////////////////////////////////////////////////////////////////////////
  57. //-----------------------------------------------------------------------------
  58. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  59. //-----------------------------------------------------------------------------
  60. //-----------------------------------------------------------------------------
  61. // USER INCLUDES //////////////////////////////////////////////////////////////
  62. //-----------------------------------------------------------------------------
  63. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  64. #include "GameClient/WindowVideoManager.h"
  65. #include "GameClient/GameWindow.h"
  66. #include "GameClient/VideoPlayer.h"
  67. #include "GameClient/Display.h"
  68. //-----------------------------------------------------------------------------
  69. // DEFINES ////////////////////////////////////////////////////////////////////
  70. //-----------------------------------------------------------------------------
  71. //-----------------------------------------------------------------------------
  72. // WindowVideo PUBLIC FUNCTIONS ///////////////////////////////////////////////
  73. //-----------------------------------------------------------------------------
  74. WindowVideo::WindowVideo( void )
  75. {
  76. m_playType = WINDOW_PLAY_MOVIE_ONCE;
  77. m_win = NULL;
  78. m_videoBuffer = NULL;
  79. m_videoStream = NULL;
  80. m_movieName.clear();
  81. m_state = WINDOW_VIDEO_STATE_STOP;
  82. }
  83. WindowVideo::~WindowVideo( void )
  84. {
  85. // Don't Delete the window, only set it's video buffer to NULL
  86. if(m_win)
  87. m_win->winGetInstanceData()->setVideoBuffer( NULL );
  88. m_win = NULL;
  89. delete m_videoBuffer;
  90. m_videoBuffer = NULL;
  91. if ( m_videoStream )
  92. m_videoStream->close();
  93. m_videoStream = NULL;
  94. }
  95. void WindowVideo::init( GameWindow *win, AsciiString movieName,
  96. WindowVideoPlayType playType,
  97. VideoBuffer *videoBuffer, VideoStreamInterface *videoStream)
  98. {
  99. m_win = win;
  100. m_movieName = movieName;
  101. m_playType = playType;
  102. m_videoBuffer = videoBuffer;
  103. m_videoStream = videoStream;
  104. m_state = WINDOW_VIDEO_STATE_PLAY;
  105. if(m_win)
  106. m_win->winGetInstanceData()->setVideoBuffer( m_videoBuffer );
  107. }
  108. void WindowVideo::setWindowState( WindowVideoStates state )
  109. {
  110. m_state = state;
  111. if(m_state == WINDOW_VIDEO_STATE_STOP && m_win)
  112. m_win->winGetInstanceData()->setVideoBuffer( NULL );
  113. if((m_state == WINDOW_VIDEO_STATE_PLAY || m_state == WINDOW_VIDEO_STATE_PAUSE )&& m_win)
  114. m_win->winGetInstanceData()->setVideoBuffer( m_videoBuffer );
  115. }
  116. //-----------------------------------------------------------------------------
  117. // WindowVideoManager PUBLIC FUNCTIONS ////////////////////////////////////////
  118. //-----------------------------------------------------------------------------
  119. WindowVideoManager::WindowVideoManager( void )
  120. {
  121. WindowVideoMap::iterator it = m_playingVideos.begin();
  122. while(it != m_playingVideos.end())
  123. {
  124. WindowVideo *winVid = it->second;
  125. if(winVid)
  126. delete winVid;
  127. it++;
  128. }
  129. m_playingVideos.clear();
  130. m_stopAllMovies = FALSE;
  131. m_pauseAllMovies = FALSE;
  132. }
  133. WindowVideoManager::~WindowVideoManager( void )
  134. {
  135. WindowVideoMap::iterator it = m_playingVideos.begin();
  136. while(it != m_playingVideos.end())
  137. {
  138. WindowVideo *winVid = it->second;
  139. if(winVid)
  140. delete winVid;
  141. it++;
  142. }
  143. m_playingVideos.clear();
  144. }
  145. void WindowVideoManager::init( void )
  146. {
  147. m_playingVideos.clear();
  148. m_stopAllMovies = FALSE;
  149. m_pauseAllMovies = FALSE;
  150. }
  151. void WindowVideoManager::reset( void )
  152. {
  153. WindowVideoMap::iterator it = m_playingVideos.begin();
  154. while(it != m_playingVideos.end())
  155. {
  156. WindowVideo *winVid = it->second;
  157. if(winVid)
  158. delete winVid;
  159. it++;
  160. }
  161. m_playingVideos.clear();
  162. m_stopAllMovies = FALSE;
  163. m_pauseAllMovies = FALSE;
  164. }
  165. void WindowVideoManager::update( void )
  166. {
  167. WindowVideoMap::iterator it = m_playingVideos.begin();
  168. if(m_pauseAllMovies || m_stopAllMovies)
  169. return;
  170. //Iterate through the maps
  171. while(it != m_playingVideos.end())
  172. {
  173. WindowVideo *winVid = it->second;
  174. if(!winVid)
  175. {
  176. DEBUG_CRASH(("There's No WindowVideo in the m_playignVideos list"));
  177. return;
  178. }
  179. GameWindow *win = winVid->getWin();
  180. if(winVid->getState() == WINDOW_VIDEO_STATE_HIDDEN && (win->winIsHidden() == FALSE))
  181. {
  182. resumeMovie(win);
  183. }
  184. if(winVid->getState() == WINDOW_VIDEO_STATE_PLAY && win->winIsHidden())
  185. {
  186. hideMovie(win);
  187. }
  188. // Only advance the frame if we're playing
  189. if(winVid->getState() != WINDOW_VIDEO_STATE_PLAY)
  190. {
  191. it++;
  192. continue;
  193. }
  194. // Get the Stream and the buffer to update for each animation
  195. VideoStreamInterface *videoStream = winVid->getVideoStream();
  196. VideoBuffer *videoBuffer = winVid->getVideoBuffer();
  197. if ( videoStream && videoBuffer )
  198. {
  199. if ( videoStream->isFrameReady())
  200. {
  201. videoStream->frameDecompress();
  202. videoStream->frameRender( videoBuffer );
  203. videoStream->frameNext();
  204. // If we reach frame Index of 0, we might have to pause, or loop.
  205. if ( videoStream->frameIndex() == 0 )
  206. {
  207. if(winVid->getPlayType() == WINDOW_PLAY_MOVIE_ONCE)
  208. stopMovie(win);
  209. else if (winVid->getPlayType() == WINDOW_PLAY_MOVIE_SHOW_LAST_FRAME)
  210. pauseMovie(win);
  211. }
  212. }
  213. }
  214. it++;
  215. }
  216. }
  217. void WindowVideoManager::playMovie( GameWindow *win, AsciiString movieName, WindowVideoPlayType playType )
  218. {
  219. // if we already have a movie playing for that window, kill it.
  220. stopAndRemoveMovie( win );
  221. // create the new stream
  222. VideoStreamInterface *videoStream = TheVideoPlayer->open( movieName );
  223. if ( videoStream == NULL )
  224. {
  225. return;
  226. }
  227. // Create the new buffer
  228. VideoBuffer *videoBuffer = TheDisplay->createVideoBuffer();
  229. if ( videoBuffer == NULL ||
  230. !videoBuffer->allocate( videoStream->width(),
  231. videoStream->height())
  232. )
  233. {
  234. // If we failed to create the buffer...
  235. delete videoBuffer;
  236. videoBuffer = NULL;
  237. if ( videoStream )
  238. videoStream->close();
  239. videoStream = NULL;
  240. return;
  241. }
  242. // now that we have everything, create the new WindowVideo Structure
  243. WindowVideo *winVid = NEW WindowVideo;
  244. // init it.
  245. winVid->init( win, movieName,playType,videoBuffer,videoStream);
  246. // add it to our map.
  247. m_playingVideos[win] = winVid;
  248. m_pauseAllMovies = FALSE;
  249. m_stopAllMovies = FALSE;
  250. }
  251. void WindowVideoManager::pauseMovie( GameWindow *win )
  252. {
  253. WindowVideoMap::iterator it = m_playingVideos.find(win);
  254. if(it != m_playingVideos.end())
  255. {
  256. WindowVideo *winVid = it->second;
  257. if(winVid)
  258. winVid->setWindowState(WINDOW_VIDEO_STATE_PAUSE);
  259. }
  260. }
  261. void WindowVideoManager::hideMovie( GameWindow *win )
  262. {
  263. WindowVideoMap::iterator it = m_playingVideos.find(win);
  264. if(it != m_playingVideos.end())
  265. {
  266. WindowVideo *winVid = it->second;
  267. if(winVid)
  268. winVid->setWindowState(WINDOW_VIDEO_STATE_HIDDEN);
  269. }
  270. }
  271. void WindowVideoManager::resumeMovie( GameWindow *win )
  272. {
  273. WindowVideoMap::iterator it = m_playingVideos.find(win);
  274. if(it != m_playingVideos.end())
  275. {
  276. WindowVideo *winVid = it->second;
  277. if(winVid)
  278. winVid->setWindowState(WINDOW_VIDEO_STATE_PLAY);
  279. }
  280. m_pauseAllMovies = FALSE;
  281. m_stopAllMovies = FALSE;
  282. }
  283. void WindowVideoManager::stopMovie( GameWindow *win )
  284. {
  285. WindowVideoMap::iterator it = m_playingVideos.find(win);
  286. if(it != m_playingVideos.end())
  287. {
  288. WindowVideo *winVid = it->second;
  289. if(winVid)
  290. winVid->setWindowState(WINDOW_VIDEO_STATE_STOP);
  291. }
  292. }
  293. void WindowVideoManager::stopAndRemoveMovie( GameWindow *win )
  294. {
  295. WindowVideoMap::iterator it = m_playingVideos.find(win);
  296. if(it != m_playingVideos.end())
  297. {
  298. WindowVideo *winVid = it->second;
  299. if(winVid)
  300. delete winVid;
  301. winVid = NULL;
  302. m_playingVideos.erase(it);
  303. }
  304. }
  305. void WindowVideoManager::stopAllMovies( void )
  306. {
  307. WindowVideoMap::iterator it = m_playingVideos.begin();
  308. //Iterate through the maps
  309. while(it != m_playingVideos.end())
  310. {
  311. WindowVideo *winVid = it->second;
  312. if(winVid)
  313. winVid->setWindowState(WINDOW_VIDEO_STATE_STOP);
  314. it++;
  315. }
  316. m_stopAllMovies = TRUE;
  317. m_pauseAllMovies = FALSE;
  318. }
  319. void WindowVideoManager::pauseAllMovies( void )
  320. {
  321. WindowVideoMap::iterator it = m_playingVideos.begin();
  322. //Iterate through the maps
  323. while(it != m_playingVideos.end())
  324. {
  325. WindowVideo *winVid = it->second;
  326. if(winVid)
  327. winVid->setWindowState(WINDOW_VIDEO_STATE_PAUSE);
  328. it++;
  329. }
  330. m_pauseAllMovies = TRUE;
  331. m_stopAllMovies = FALSE;
  332. }
  333. void WindowVideoManager::resumeAllMovies( void )
  334. {
  335. WindowVideoMap::iterator it = m_playingVideos.begin();
  336. //Iterate through the maps
  337. while(it != m_playingVideos.end())
  338. {
  339. WindowVideo *winVid = it->second;
  340. if(winVid)
  341. winVid->setWindowState(WINDOW_VIDEO_STATE_PLAY);
  342. it++;
  343. }
  344. m_stopAllMovies = FALSE;
  345. m_pauseAllMovies = FALSE;
  346. }
  347. Int WindowVideoManager::getWinState( GameWindow *win )
  348. {
  349. WindowVideoMap::iterator it = m_playingVideos.find(win);
  350. if(it != m_playingVideos.end())
  351. {
  352. WindowVideo *winVid = it->second;
  353. if(winVid)
  354. return winVid->getState();
  355. }
  356. return WINDOW_VIDEO_STATE_STOP;
  357. }
  358. //-----------------------------------------------------------------------------
  359. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  360. //-----------------------------------------------------------------------------