BinkVideoPlayer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. //----------------------------------------------------------------------------
  24. //
  25. // Westwood Studios Pacific.
  26. //
  27. // Confidential Information
  28. // Copyright (C) 2001 - All Rights Reserved
  29. //
  30. //----------------------------------------------------------------------------
  31. //
  32. // Project: Generals
  33. //
  34. // Module: VideoDevice
  35. //
  36. // File name: BinkDevice.cpp
  37. //
  38. // Created: 10/22/01 TR
  39. //
  40. //----------------------------------------------------------------------------
  41. //----------------------------------------------------------------------------
  42. // Includes
  43. //----------------------------------------------------------------------------
  44. #include "Lib/BaseType.h"
  45. #include "VideoDevice/Bink/BinkVideoPlayer.h"
  46. #include "Common/AudioAffect.h"
  47. #include "Common/GameAudio.h"
  48. #include "Common/GameMemory.h"
  49. #include "Common/GlobalData.h"
  50. #include "Common/Registry.h"
  51. //----------------------------------------------------------------------------
  52. // Externals
  53. //----------------------------------------------------------------------------
  54. //----------------------------------------------------------------------------
  55. // Defines
  56. //----------------------------------------------------------------------------
  57. #define VIDEO_LANG_PATH_FORMAT "Data/%s/Movies/%s.%s"
  58. #define VIDEO_PATH "Data\\Movies"
  59. #define VIDEO_EXT "bik"
  60. //----------------------------------------------------------------------------
  61. // Private Types
  62. //----------------------------------------------------------------------------
  63. //----------------------------------------------------------------------------
  64. // Private Data
  65. //----------------------------------------------------------------------------
  66. //----------------------------------------------------------------------------
  67. // Public Data
  68. //----------------------------------------------------------------------------
  69. //----------------------------------------------------------------------------
  70. // Private Prototypes
  71. //----------------------------------------------------------------------------
  72. //----------------------------------------------------------------------------
  73. // Private Functions
  74. //----------------------------------------------------------------------------
  75. //----------------------------------------------------------------------------
  76. // Public Functions
  77. //----------------------------------------------------------------------------
  78. //============================================================================
  79. // BinkVideoPlayer::BinkVideoPlayer
  80. //============================================================================
  81. BinkVideoPlayer::BinkVideoPlayer()
  82. {
  83. }
  84. //============================================================================
  85. // BinkVideoPlayer::~BinkVideoPlayer
  86. //============================================================================
  87. BinkVideoPlayer::~BinkVideoPlayer()
  88. {
  89. deinit();
  90. }
  91. //============================================================================
  92. // BinkVideoPlayer::init
  93. //============================================================================
  94. void BinkVideoPlayer::init( void )
  95. {
  96. // Need to load the stuff from the ini file.
  97. VideoPlayer::init();
  98. initializeBinkWithMiles();
  99. }
  100. //============================================================================
  101. // BinkVideoPlayer::deinit
  102. //============================================================================
  103. void BinkVideoPlayer::deinit( void )
  104. {
  105. TheAudio->releaseHandleForBink();
  106. VideoPlayer::deinit();
  107. }
  108. //============================================================================
  109. // BinkVideoPlayer::reset
  110. //============================================================================
  111. void BinkVideoPlayer::reset( void )
  112. {
  113. VideoPlayer::reset();
  114. }
  115. //============================================================================
  116. // BinkVideoPlayer::update
  117. //============================================================================
  118. void BinkVideoPlayer::update( void )
  119. {
  120. VideoPlayer::update();
  121. }
  122. //============================================================================
  123. // BinkVideoPlayer::loseFocus
  124. //============================================================================
  125. void BinkVideoPlayer::loseFocus( void )
  126. {
  127. VideoPlayer::loseFocus();
  128. }
  129. //============================================================================
  130. // BinkVideoPlayer::regainFocus
  131. //============================================================================
  132. void BinkVideoPlayer::regainFocus( void )
  133. {
  134. VideoPlayer::regainFocus();
  135. }
  136. //============================================================================
  137. // BinkVideoPlayer::createStream
  138. //============================================================================
  139. VideoStreamInterface* BinkVideoPlayer::createStream( HBINK handle )
  140. {
  141. if ( handle == NULL )
  142. {
  143. return NULL;
  144. }
  145. BinkVideoStream *stream = NEW BinkVideoStream;
  146. if ( stream )
  147. {
  148. stream->m_handle = handle;
  149. stream->m_next = m_firstStream;
  150. stream->m_player = this;
  151. m_firstStream = stream;
  152. // never let volume go to 0, as Bink will interpret that as "play at full volume".
  153. Int mod = (Int) ((TheAudio->getVolume(AudioAffect_Speech) * 0.8f) * 100) + 1;
  154. Int volume = (32768*mod)/100;
  155. DEBUG_LOG(("BinkVideoPlayer::createStream() - About to set volume (%g -> %d -> %d\n",
  156. TheAudio->getVolume(AudioAffect_Speech), mod, volume));
  157. BinkSetVolume( stream->m_handle,0, volume);
  158. DEBUG_LOG(("BinkVideoPlayer::createStream() - set volume\n"));
  159. }
  160. return stream;
  161. }
  162. //============================================================================
  163. // BinkVideoPlayer::open
  164. //============================================================================
  165. VideoStreamInterface* BinkVideoPlayer::open( AsciiString movieTitle )
  166. {
  167. VideoStreamInterface* stream = NULL;
  168. const Video* pVideo = getVideo(movieTitle);
  169. if (pVideo) {
  170. DEBUG_LOG(("BinkVideoPlayer::createStream() - About to open bink file\n"));
  171. if (TheGlobalData->m_modDir.isNotEmpty())
  172. {
  173. char filePath[ _MAX_PATH ];
  174. sprintf( filePath, "%s%s\\%s.%s", TheGlobalData->m_modDir.str(), VIDEO_PATH, pVideo->m_filename.str(), VIDEO_EXT );
  175. HBINK handle = BinkOpen(filePath , BINKPRELOADALL );
  176. DEBUG_ASSERTLOG(!handle, ("opened bink file %s\n", filePath));
  177. if (handle)
  178. {
  179. return createStream( handle );
  180. }
  181. }
  182. char localizedFilePath[ _MAX_PATH ];
  183. sprintf( localizedFilePath, VIDEO_LANG_PATH_FORMAT, GetRegistryLanguage().str(), pVideo->m_filename.str(), VIDEO_EXT );
  184. HBINK handle = BinkOpen(localizedFilePath , BINKPRELOADALL );
  185. DEBUG_ASSERTLOG(!handle, ("opened localized bink file %s\n", localizedFilePath));
  186. if (!handle)
  187. {
  188. char filePath[ _MAX_PATH ];
  189. sprintf( filePath, "%s\\%s.%s", VIDEO_PATH, pVideo->m_filename.str(), VIDEO_EXT );
  190. handle = BinkOpen(filePath , BINKPRELOADALL );
  191. DEBUG_ASSERTLOG(!handle, ("opened bink file %s\n", localizedFilePath));
  192. }
  193. DEBUG_LOG(("BinkVideoPlayer::createStream() - About to create stream\n"));
  194. stream = createStream( handle );
  195. }
  196. return stream;
  197. }
  198. //============================================================================
  199. // BinkVideoPlayer::load
  200. //============================================================================
  201. VideoStreamInterface* BinkVideoPlayer::load( AsciiString movieTitle )
  202. {
  203. return open(movieTitle); // load() used to have the same body as open(), so I'm combining them. Munkee.
  204. }
  205. //============================================================================
  206. //============================================================================
  207. void BinkVideoPlayer::notifyVideoPlayerOfNewProvider( Bool nowHasValid )
  208. {
  209. if (!nowHasValid) {
  210. TheAudio->releaseHandleForBink();
  211. BinkSetSoundTrack(0, 0);
  212. } else {
  213. initializeBinkWithMiles();
  214. }
  215. }
  216. //============================================================================
  217. //============================================================================
  218. void BinkVideoPlayer::initializeBinkWithMiles()
  219. {
  220. Int retVal = 0;
  221. void *driver = TheAudio->getHandleForBink();
  222. if ( driver )
  223. {
  224. retVal = BinkSoundUseDirectSound(driver);
  225. }
  226. if( !driver || retVal == 0)
  227. {
  228. BinkSetSoundTrack ( 0,0 );
  229. }
  230. }
  231. //============================================================================
  232. // BinkVideoStream::BinkVideoStream
  233. //============================================================================
  234. BinkVideoStream::BinkVideoStream()
  235. : m_handle(NULL)
  236. {
  237. }
  238. //============================================================================
  239. // BinkVideoStream::~BinkVideoStream
  240. //============================================================================
  241. BinkVideoStream::~BinkVideoStream()
  242. {
  243. if ( m_handle != NULL )
  244. {
  245. BinkClose( m_handle );
  246. m_handle = NULL;
  247. }
  248. }
  249. //============================================================================
  250. // BinkVideoStream::update
  251. //============================================================================
  252. void BinkVideoStream::update( void )
  253. {
  254. BinkWait( m_handle );
  255. }
  256. //============================================================================
  257. // BinkVideoStream::isFrameReady
  258. //============================================================================
  259. Bool BinkVideoStream::isFrameReady( void )
  260. {
  261. return !BinkWait( m_handle );
  262. }
  263. //============================================================================
  264. // BinkVideoStream::frameDecompress
  265. //============================================================================
  266. void BinkVideoStream::frameDecompress( void )
  267. {
  268. BinkDoFrame( m_handle );
  269. }
  270. //============================================================================
  271. // BinkVideoStream::frameRender
  272. //============================================================================
  273. void BinkVideoStream::frameRender( VideoBuffer *buffer )
  274. {
  275. if ( buffer )
  276. {
  277. void *mem = buffer->lock();
  278. u32 flags;
  279. switch ( buffer->format())
  280. {
  281. case VideoBuffer::TYPE_X8R8G8B8:
  282. flags = BINKSURFACE32;
  283. break;
  284. case VideoBuffer::TYPE_R8G8B8:
  285. flags = BINKSURFACE24;
  286. break;
  287. case VideoBuffer::TYPE_R5G6B5:
  288. flags = BINKSURFACE565;
  289. break;
  290. case VideoBuffer::TYPE_X1R5G5B5:
  291. flags = BINKSURFACE555;
  292. break;
  293. default:
  294. return;
  295. }
  296. if ( mem != NULL )
  297. {
  298. BinkCopyToBuffer ( m_handle, mem, buffer->pitch(), buffer->height(),
  299. buffer->xPos(), buffer->yPos(), flags );
  300. buffer->unlock();
  301. }
  302. }
  303. }
  304. //============================================================================
  305. // BinkVideoStream::frameNext
  306. //============================================================================
  307. void BinkVideoStream::frameNext( void )
  308. {
  309. BinkNextFrame( m_handle );
  310. }
  311. //============================================================================
  312. // BinkVideoStream::frameIndex
  313. //============================================================================
  314. Int BinkVideoStream::frameIndex( void )
  315. {
  316. return m_handle->FrameNum - 1;
  317. }
  318. //============================================================================
  319. // BinkVideoStream::totalFrames
  320. //============================================================================
  321. Int BinkVideoStream::frameCount( void )
  322. {
  323. return m_handle->Frames;
  324. }
  325. //============================================================================
  326. // BinkVideoStream::frameGoto
  327. //============================================================================
  328. void BinkVideoStream::frameGoto( Int index )
  329. {
  330. BinkGoto(m_handle, index, NULL );
  331. }
  332. //============================================================================
  333. // VideoStream::height
  334. //============================================================================
  335. Int BinkVideoStream::height( void )
  336. {
  337. return m_handle->Height;
  338. }
  339. //============================================================================
  340. // VideoStream::width
  341. //============================================================================
  342. Int BinkVideoStream::width( void )
  343. {
  344. return m_handle->Width;
  345. }