UiPlayVideo.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:9k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /*****************************************************************************************
  2. // 播放video窗口
  3. // Copyright : Kingsoft 2003
  4. // Author :   Wooy(Wu yue)
  5. // CreateTime: 2003-7-14
  6. *****************************************************************************************/
  7. #include "KWin32.h"
  8. #include "KWin32Wnd.h"
  9. #include "KIniFile.h"
  10. #include "../Elem/Wnds.h"
  11. #include "../Elem/WndMessage.h"
  12. #include "UiPlayVideo.h"
  13. #include "crtdbg.h"
  14. #include "../../../Represent/iRepresent/iRepresentShell.h"
  15. #include "../../../Represent/iRepresent/KRepresentUnit.h"
  16. extern iRepresentShell* g_pRepresentShell;
  17. KUiPlayVideo* KUiPlayVideo::m_pSelf = NULL;
  18. KUiPlayVideo::KUiPlayVideo()
  19. {
  20. m_bProcessInput = true;
  21. m_bCloseWhenOver = true;
  22. m_bInitialized = false;
  23. m_pCaller = NULL;
  24. m_bnk = NULL;
  25. m_hVideoDll = NULL;
  26. m_KLVideoSoundFn = NULL;
  27.     m_KLVideoSndtrackFn = NULL;
  28.     m_KLVideoOpen = NULL;
  29.     m_KLVideoClose = NULL;
  30.     m_KLVideoWait = NULL;
  31.     m_KLVideoToBuffer = NULL;
  32.     m_KLVideoDoFrame = NULL;
  33.     m_KLVideoNextFrame = NULL;
  34. m_KLVideoGetCurrentFrame = NULL;
  35. m_KLVideoSetSoundVolume = NULL;
  36. m_szBitmapName[0] = 0;
  37. m_uBitmapId = 0;
  38. m_uBitmapIsPosition = IMAGE_IS_POSITION_INIT;
  39. m_nBlackHeight = m_nBlackWidth = 0;
  40. m_nbAlreadyPaintBlack = false;
  41. }
  42. //--------------------------------------------------------------------------
  43. // 功能:打开窗口,返回唯一的一个类对象实例
  44. //--------------------------------------------------------------------------
  45. KUiPlayVideo* KUiPlayVideo::OpenWindow()
  46. {
  47. if (m_pSelf == NULL)
  48. {
  49. m_pSelf = new KUiPlayVideo;
  50. if (m_pSelf)
  51. m_pSelf->Initialize();
  52. }
  53. if (m_pSelf)
  54. m_pSelf->Show();
  55. return m_pSelf;
  56. }
  57. //--------------------------------------------------------------------------
  58. // 功能:输入处理消息以及一些特定窗口消息的响应
  59. //--------------------------------------------------------------------------
  60. int KUiPlayVideo::WndProc(unsigned int uMsg, unsigned int uParam, int nParam)
  61. {
  62. int nRet = 0;
  63. switch(uMsg)
  64. {
  65. case WM_KEYDOWN:
  66. if (m_bProcessInput && m_bnk &&
  67. (uParam == VK_ESCAPE || uParam == VK_RETURN || uParam == VK_SPACE))
  68. {
  69. CloseVideo();
  70. }
  71. break;
  72. case WM_LBUTTONDOWN:
  73. if (m_bProcessInput && m_bnk)
  74. CloseVideo();
  75. break;
  76. default:
  77. nRet = KWndWindow::WndProc(uMsg, uParam, nParam);
  78. break;
  79. }
  80. return nRet;
  81. }
  82. //关闭窗口,同时可以选则是否删除对象实例
  83. void KUiPlayVideo::CloseWindow(bool bDestroy)
  84. {
  85. if (m_pSelf)
  86. {
  87. m_pSelf->CloseVideo();
  88. if (bDestroy == false)
  89. m_pSelf->Hide();
  90. else
  91. {
  92. m_pSelf->Terminate();
  93. m_pSelf->Destroy();
  94. m_pSelf = NULL;
  95. }
  96. }
  97. }
  98. //载入界面方案
  99. void KUiPlayVideo::LoadScheme(const char* pszSettingFile, const char* pszSection)
  100. {
  101. if (m_pSelf && pszSettingFile && pszSection && pszSettingFile[0] && pszSection[0])
  102. {
  103. KIniFile Ini;
  104. if (Ini.Load(pszSettingFile))
  105. m_pSelf->Init(&Ini, pszSection);
  106. }
  107. }
  108. int KUiPlayVideo::Initialize()
  109. {
  110. if (m_bInitialized)
  111. return true;
  112. m_hVideoDll = LoadLibrary("KLVideo.dll");
  113. if(!m_hVideoDll)
  114. return false;
  115. // connect bink dll functions
  116. m_KLVideoSoundFn         = (KLVideoSoundFn)GetProcAddress(m_hVideoDll,         "KLVideoSetSoundSystem");
  117. m_KLVideoSndtrackFn      = (KLVideoSndtrackFn)GetProcAddress(m_hVideoDll,      "KLVideoSetSoundTrack");
  118. m_KLVideoOpen            = (KLVideoOpenFn)GetProcAddress(m_hVideoDll,          "KLVideoOpen");
  119. m_KLVideoClose           = (KLVideoCloseFn)GetProcAddress(m_hVideoDll,         "KLVideoClose");
  120. m_KLVideoWait            = (KLVideoWaitFn)GetProcAddress(m_hVideoDll,          "KLVideoWait");
  121. m_KLVideoToBuffer        = (KLVideoToBufferFn)GetProcAddress(m_hVideoDll,      "KLVideoCopyToBuffer");
  122. m_KLVideoDoFrame         = (KLVideoDoFrameFn)GetProcAddress(m_hVideoDll,       "KLVideoDoFrame");
  123. m_KLVideoNextFrame       = (KLVideoNextFrameFn)GetProcAddress(m_hVideoDll,     "KLVideoNextFrame");
  124. m_KLVideoGetCurrentFrame = (KLVideoGetCurrentFrameFn)GetProcAddress(m_hVideoDll, "KLVideoGetCurrentFrame");
  125. m_KLVideoSetSoundVolume  = (KLVideoSetSndVolumeFn)GetProcAddress(m_hVideoDll,  "KLVideoSetSoundVolume");
  126. // make sure functions connected //
  127. if( !m_KLVideoSoundFn || !m_KLVideoOpen || !m_KLVideoClose || !m_KLVideoWait ||
  128. !m_KLVideoToBuffer || !m_KLVideoDoFrame || !m_KLVideoNextFrame || !m_KLVideoSndtrackFn) 
  129. {
  130. FreeLibrary(m_hVideoDll);
  131. m_hVideoDll = NULL;
  132. return false;
  133. }
  134. Wnd_AddWindow(this, WL_TOPMOST);
  135. m_bInitialized = true;
  136. return true;
  137. }
  138. void KUiPlayVideo::Terminate()
  139. {
  140. CloseVideo();
  141. if(m_hVideoDll)
  142. {
  143. FreeLibrary(m_hVideoDll);
  144. m_hVideoDll = NULL;
  145. }
  146. m_bInitialized = false;
  147. }
  148. int KUiPlayVideo::OpenVideo(const char* pszVideoFile)
  149. {
  150. if (m_bInitialized == false || m_Width == 0 || m_Height == 0)
  151. return false;
  152. m_nbAlreadyPaintBlack = false;
  153. CloseVideo(false);
  154. m_bnk = m_KLVideoOpen(pszVideoFile, 0, g_GetMainHWnd());
  155. if(!m_bnk)
  156. return false;
  157. if(m_Width < (int)m_bnk->Width || m_Height < (int)m_bnk->Height)
  158. {
  159. CloseVideo();
  160. return false;
  161. }
  162. m_KLVideoSetSoundVolume(m_bnk, 100);
  163. for (int i = 0; i < 10000; i++)
  164. {
  165. sprintf(m_szBitmapName, "VideoBitmap_%d", i);
  166. m_uBitmapId = g_pRepresentShell->CreateImage(m_szBitmapName, m_bnk->Width, m_bnk->Height, ISI_T_BITMAP16);
  167. if (m_uBitmapId)
  168. break;
  169. }
  170. if (m_uBitmapId)
  171. {
  172. m_uBitmapIsPosition = IMAGE_IS_POSITION_INIT;
  173. g_pRepresentShell->ClearImageData(m_szBitmapName, m_uBitmapId, m_uBitmapIsPosition);
  174. m_nBlackHeight = m_bnk->Height  * m_Width / m_bnk->Width;
  175. if (m_nBlackHeight <= m_Height)
  176. {
  177. m_nBlackHeight = (m_Height - m_nBlackHeight) / 2;
  178. }
  179. else
  180. {
  181. m_nBlackHeight = 0;
  182. m_nBlackWidth = m_bnk->Width * m_Height / m_bnk->Height;
  183. if (m_nBlackWidth < m_Width)
  184. {
  185. m_nBlackWidth = (m_Width - m_nBlackWidth) / 2;
  186. }
  187. else
  188. {
  189. m_nBlackWidth = 0;
  190. }
  191. }
  192. return true;
  193. }
  194. CloseVideo();
  195. return false;
  196. }
  197. void KUiPlayVideo::CloseVideo(bool bFinished)
  198. {
  199. m_nbAlreadyPaintBlack = false;
  200. m_nBlackHeight = m_nBlackWidth = 0;
  201. if(m_bnk)
  202. {
  203. m_KLVideoClose( m_bnk );
  204. m_bnk = NULL;
  205. if (m_uBitmapId)
  206. {
  207. g_pRepresentShell->FreeImage(m_szBitmapName);
  208. m_uBitmapId = 0;
  209. }
  210. if (bFinished)
  211. OnPlayFinished();
  212. }
  213. }
  214. void KUiPlayVideo::Breathe()
  215. {
  216. if(!m_bnk)
  217. return;
  218. KBitmapDataBuffInfo bufInfo;
  219. void *pBuf = g_pRepresentShell->GetBitmapDataBuffer(m_szBitmapName, &bufInfo);
  220. if(!pBuf)
  221. return;
  222. unsigned int uFormat;
  223. if (bufInfo.eFormat == DBDF_24BIT)
  224. uFormat = KLVIDEOSURFACE24;
  225. else if (bufInfo.eFormat == BDBF_16BIT_565)
  226. uFormat = KLVIDEOSURFACE565;
  227. else// if (bufInfo.eFormat == BDBF_16BIT_555)
  228. uFormat = KLVIDEOSURFACE555;
  229. m_KLVideoToBuffer(m_bnk, pBuf, 0, 0, bufInfo.nPitch, m_bnk->Height, uFormat);
  230. g_pRepresentShell->ReleaseBitmapDataBuffer(m_szBitmapName,pBuf);
  231. if(m_bnk->FrameNum >= m_bnk->Frames)
  232. {
  233. CloseVideo();
  234. }
  235. }
  236. //--------------------------------------------------------------------------
  237. // 功能:绘制游戏世界
  238. //--------------------------------------------------------------------------
  239. void KUiPlayVideo::PaintWindow()
  240. {
  241. if(m_bnk == NULL || g_pRepresentShell == NULL)
  242. return;
  243. if (m_nbAlreadyPaintBlack == false)
  244. {
  245. m_nbAlreadyPaintBlack = true;
  246. KRUShadow Black;
  247. Black.oPosition.nX = m_nAbsoluteLeft;
  248. Black.oPosition.nY = m_nAbsoluteTop;
  249. Black.Color.Color_dw = 0;
  250. if (m_nBlackHeight)
  251. {
  252. Black.oEndPos.nX = Black.oPosition.nX + m_Width;
  253. Black.oEndPos.nY = Black.oPosition.nY + m_nBlackHeight;
  254. g_pRepresentShell->DrawPrimitives(1, &Black, RU_T_SHADOW, true);
  255. Black.oEndPos.nY = Black.oPosition.nY + m_Height;
  256. Black.oPosition.nY = Black.oEndPos.nY - m_nBlackHeight;
  257. g_pRepresentShell->DrawPrimitives(1, &Black, RU_T_SHADOW, true);
  258. }
  259. else if (m_nBlackWidth)
  260. {
  261. Black.oEndPos.nX = Black.oPosition.nX + m_nBlackWidth;
  262. Black.oEndPos.nY = Black.oPosition.nY + m_Height;
  263. g_pRepresentShell->DrawPrimitives(1, &Black, RU_T_SHADOW, true);
  264. Black.oEndPos.nX = Black.oPosition.nX + m_Width;
  265. Black.oPosition.nX = Black.oEndPos.nX - m_nBlackWidth;
  266. g_pRepresentShell->DrawPrimitives(1, &Black, RU_T_SHADOW, true);
  267. }
  268. }
  269. KRUImageStretch img;
  270. strcpy(img.szImage, m_szBitmapName);
  271. img.nType = ISI_T_BITMAP16;
  272. img.oPosition.nX = m_nAbsoluteLeft + m_nBlackWidth;
  273. img.oPosition.nY = m_nAbsoluteTop + m_nBlackHeight;
  274. img.oEndPos.nX = m_nAbsoluteLeft + m_Width - m_nBlackWidth;
  275. img.oEndPos.nY = m_nAbsoluteTop + m_Height - m_nBlackHeight;
  276. img.bRenderFlag = 0;
  277. img.bRenderStyle = IMAGE_RENDER_STYLE_OPACITY;
  278. img.nFrame = 0;
  279. img.nISPosition = m_uBitmapIsPosition;
  280. img.uImage = m_uBitmapId;
  281. g_pRepresentShell->DrawPrimitives(1, &img, RU_T_IMAGE_STRETCH, true);
  282. m_uBitmapIsPosition = img.nISPosition;
  283. }
  284. //播放结束
  285. void KUiPlayVideo::OnPlayFinished()
  286. {
  287. KWndWindow* pCaller = m_pCaller;
  288. unsigned int uParam = m_uCallerParam;
  289. m_pCaller = NULL;
  290. if (m_bCloseWhenOver)
  291. CloseWindow(false);
  292. if (pCaller)
  293. pCaller->WndProc(WND_M_OTHER_WORK_RESULT, uParam, 0);
  294. }
  295. //设置窗口位置,相对坐标
  296. void KUiPlayVideo::SetPosition(int nLeft, int nTop)
  297. {
  298. KWndWindow::SetPosition(nLeft, nTop);
  299. }
  300. //设置窗口大小
  301. void KUiPlayVideo::SetSize(int nWidth, int nHeight)
  302. {
  303. if (m_bnk == NULL)
  304. KWndWindow::SetSize(nWidth, nHeight);
  305. }
  306. void KUiPlayVideo::Setting(bool bProcessInput, bool bCloseWhenOver,
  307. KWndWindow* pCaller, unsigned int uParam)
  308. {
  309. m_bProcessInput = bProcessInput;
  310. m_bCloseWhenOver = bCloseWhenOver;
  311. m_pCaller = pCaller;
  312. m_uCallerParam = uParam;
  313. }