CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

MCIMOVIE.CPP (6823B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 
     16 #include "function.h"
     17 
     18 #ifdef MCIMPEG
     19 #include "mcimovie.h"
     20 #include <memory.h>
     21 
     22 /****************************************************************************
     23 *
     24 *	NAME
     25 *     MCIMovie - Constructor
     26 *
     27 * DESCRIPTION
     28 *
     29 * INPUTS
     30 *     HInstance - Application instance handle
     31 *
     32 * RESULT
     33 *     NONE
     34 *
     35 ****************************************************************************/
     36 
     37 MCIMovie::MCIMovie(HWND mainWindow)
     38 	: mMainWindow(mainWindow), mMCIWindow(NULL), mName(NULL), mDeviceID(0)
     39 	{
     40 	mWidth = mHeight = 0;
     41 	}
     42 
     43 
     44 /****************************************************************************
     45 *
     46 * NAME
     47 *     ~MCIMovie - Destructor
     48 *
     49 * DESCRIPTION
     50 *
     51 * INPUTS
     52 *
     53 * RESULT
     54 *
     55 ****************************************************************************/
     56 
     57 MCIMovie::~MCIMovie()
     58 	{
     59 	// Stop any playing movie
     60 	Close();
     61 
     62 	// Free name
     63 	if (mName != NULL)
     64 		free(mName);
     65 	}
     66 
     67 
     68 /****************************************************************************
     69 *
     70 * NAME
     71 *     Open()
     72 *
     73 * DESCRIPTION
     74 *     Open the media file in preparation for playback.
     75 *
     76 * INPUTS
     77 *     NONE
     78 *
     79 * RESULT
     80 *     Success - Success/Failure flag
     81 *
     82 ****************************************************************************/
     83 
     84 bool MCIMovie::Open(const char* name, const char* device)
     85 	{
     86 	MCIERROR rc;
     87 	MCI_DGV_RECT_PARMS rectParm;
     88 	MCI_BREAK_PARMS breakParm;
     89 
     90 	// Stop any currently playing movie
     91 	Close();
     92 
     93 	// Copy the movie name for our use
     94 	if (mName != NULL)
     95 		free(mName);
     96 
     97 	mName = strdup(name);
     98 
     99 	if (device == NULL)
    100 		device = "mpegvideo";
    101 		
    102 	// Setup open parameters
    103 	memset((void*)&mOpenParm, 0, sizeof(mOpenParm));
    104 	mOpenParm.dwCallback = NULL;
    105 	mOpenParm.lpstrDeviceType = device;
    106 	mOpenParm.lpstrElementName = name;
    107 
    108 	rc = mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE |
    109 		MCI_OPEN_ELEMENT, (DWORD)&mOpenParm);
    110 			
    111 	if (rc)
    112 		{
    113 		char buffer[512];
    114 		mciGetErrorString(rc, buffer, 512);
    115 		return false;
    116 		}
    117 
    118 	// Set device ID
    119 	mDeviceID = mOpenParm.wDeviceID;
    120 
    121 	// Retrieve movie dimensions
    122 	rectParm.dwCallback = NULL;
    123 
    124 	rc = mciSendCommand(mDeviceID, MCI_WHERE, MCI_WAIT | MCI_DGV_WHERE_SOURCE,
    125 			(DWORD)&rectParm);
    126 
    127 	if (rc)
    128 		{
    129 		char buffer[512];
    130 		mciGetErrorString(rc, buffer, 512);
    131 		return false;
    132 		}
    133 
    134 	mWidth = rectParm.rc.right - rectParm.rc.left;
    135 	mHeight = rectParm.rc.bottom - rectParm.rc.top;
    136 
    137 	// Set break key to escape
    138  	breakParm.dwCallback = NULL;
    139 	breakParm.nVirtKey = VK_ESCAPE;
    140 	breakParm.hwndBreak = mMainWindow;
    141 	
    142 	rc = mciSendCommand(mDeviceID, MCI_BREAK, MCI_WAIT | MCI_BREAK_HWND |
    143 		MCI_BREAK_KEY, (DWORD)&breakParm);
    144 
    145 	if (rc)
    146 		{
    147 		char buffer[512];
    148 		mciGetErrorString(rc, buffer, 512);
    149 		}
    150 	
    151 	return true;
    152 	}
    153 
    154 
    155 /****************************************************************************
    156 *
    157 * NAME
    158 *     Play - Play the specified movie.
    159 *
    160 * DESCRIPTION
    161 *
    162 * INPUTS
    163 *
    164 * RESULT
    165 *     Success - Success/Failure flag
    166 *
    167 ****************************************************************************/
    168 
    169 bool MCIMovie::Play(HWND window)
    170 	{
    171 	MCIERROR rc;
    172 	
    173 	if (mDeviceID == 0)
    174 		return false;
    175 		
    176 	// Provide window for playback
    177 	if (AttachWindow(window))
    178 		{
    179 		// Size the video area
    180 		if (SizeDestination())
    181 			{
    182 			// Start playing
    183 			memset((void*)&mPlayParm, 0, sizeof(mPlayParm));
    184 			mPlayParm.dwCallback = NULL;
    185 
    186 			rc = mciSendCommand(mDeviceID, MCI_PLAY, MCI_WAIT, (DWORD)&mPlayParm);
    187 
    188 			if (rc)
    189 				{
    190 				char buffer[512];
    191 				mciGetErrorString(rc, buffer, 512);
    192 				return false;
    193 				}
    194 				
    195 			Close();
    196 			}
    197 		}
    198 
    199 	return true;
    200 	}
    201 
    202 
    203 /****************************************************************************
    204 *
    205 * NAME
    206 *     Pause
    207 *
    208 * DESCRIPTION
    209 *
    210 * INPUTS
    211 *
    212 * RESULT
    213 *     Success - Success/Failure flag
    214 *
    215 ****************************************************************************/
    216 
    217 bool MCIMovie::Pause(void)
    218 	{
    219 	if (mDeviceID == 0)
    220 		return false;
    221 
    222 	if (mciSendCommand(mDeviceID, MCI_PAUSE, 0, (DWORD)NULL))
    223 		return false;
    224 
    225 	return true;
    226 	}
    227 
    228 
    229 /****************************************************************************
    230 *
    231 * NAME
    232 *     Stop
    233 *
    234 * DESCRIPTION
    235 *
    236 * INPUTS
    237 *
    238 * RESULT
    239 *     Success - Success/Failure flag
    240 *
    241 ****************************************************************************/
    242 
    243 bool MCIMovie::Close(void)
    244 	{
    245 	MCIERROR rc;
    246 	
    247 	if (mDeviceID == 0)
    248 		return false;
    249 
    250 	rc = mciSendCommand(mDeviceID, MCI_CLOSE, 0, (DWORD)NULL);
    251 	mDeviceID = 0;
    252 
    253 	if (rc)
    254 		{
    255 		char buffer[512];
    256 		mciGetErrorString(rc, buffer, 512);
    257 		return false;
    258 		}
    259 
    260 	return true;
    261 	}
    262 
    263 
    264 /****************************************************************************
    265 *
    266 * NAME
    267 *     SizeDestination
    268 *
    269 * DESCRIPTION
    270 *
    271 * INPUTS
    272 *
    273 * RESULT
    274 *
    275 ****************************************************************************/
    276 
    277 bool MCIMovie::SizeDestination(void)
    278 	{
    279 	MCIERROR rc;
    280 	MCI_DGV_PUT_PARMS putParm;
    281 	RECT rect;
    282 
    283 	if (mMCIWindow == NULL)
    284 		return false;
    285 
    286 	GetClientRect(mMCIWindow, &rect);
    287 	ClientToScreen(mMCIWindow, (LPPOINT)&rect);
    288 	ClientToScreen(mMCIWindow, (LPPOINT)&rect + 1);
    289 
    290 	putParm.dwCallback = NULL;
    291 	putParm.rc.left = rect.left;
    292 	putParm.rc.top = rect.top;
    293 	putParm.rc.right = rect.right;
    294 	putParm.rc.bottom = rect.bottom;
    295 
    296 	rc = mciSendCommand(mDeviceID, MCI_PUT, MCI_WAIT | MCI_DGV_RECT |
    297 		MCI_DGV_PUT_DESTINATION, (DWORD)&putParm);
    298 
    299 	if (rc)
    300 		{
    301 		char buffer[512];
    302 		mciGetErrorString(rc, buffer, 512);
    303 		return false;
    304 		}
    305 
    306 	return true;
    307 	}
    308 
    309 
    310 /****************************************************************************
    311 *
    312 * NAME
    313 *     AttachWindow
    314 *
    315 * DESCRIPTION
    316 *
    317 * INPUTS
    318 *
    319 * RESULT
    320 *
    321 ****************************************************************************/
    322 
    323 bool MCIMovie::AttachWindow(HWND window)
    324 	{
    325 	MCIERROR rc;
    326 	MCI_DGV_WINDOW_PARMS winParm;
    327 
    328 	mMCIWindow = window;
    329 
    330 	memset((void*)&winParm, 0, sizeof(winParm));
    331 	winParm.dwCallback = NULL;
    332 	winParm.hWnd = window;
    333 	winParm.nCmdShow = SW_SHOW;
    334 
    335 	rc = mciSendCommand(mDeviceID, MCI_WINDOW, MCI_WAIT| MCI_DGV_WINDOW_HWND |
    336 			MCI_DGV_WINDOW_STATE, (DWORD)&winParm);
    337 
    338 	if (rc)
    339 		{
    340 		char buffer[512];
    341 		mciGetErrorString(rc, buffer, 512);
    342 		return false;
    343 		}
    344 
    345 	return true;
    346 	}
    347 
    348 #endif // MCIMPEG
    349